@2008 huynh ngoc tin chapter #2 java servlet prgramming
TRANSCRIPT
@2008 Huynh Ngoc Tin
Chapter #2
JAVA SERVLET
PRGRAMMING
@2008 Huynh Ngoc Tin
Objective
Basic concepts
Why?
What is java servlet?
Servlet container
Servlet life circle
Servlet API, servlet interface
…
Build, deploy, run a java servlet
@2008 Huynh Ngoc Tin
References
[1] Chapter 3. Prentice Hall Ptr Java(Tm) Ee 5
Tutorial, The (3Rd Edition) (The Java Series)
[2] Marty Hall. Core Servlet and Java Server Page.
Sun Micro System. Prentice Hall PTR; 1 edition 2000.
[3] http://java.sun.com/products/servlet
@2008 Huynh Ngoc Tin
Basic concepts
Why?
What is java servlet?
What is servlet container?
Servets on J2EE/JEE Platform
Servlet Interface
Servlet Life Circle
Servlet context
Request/Response
The need for dynamic content.
Applets, one of the earliest attempts toward this goal
(the client side)
At the same time, (CGI) scripts were the main
technology used to generate dynamic content
Limitation: Lack of scalability, Java Servlet
technology was created as a portable way to
provide dynamic, user-oriented content.
@2008 Huynh Ngoc Tin
Why?
@2008 Huynh Ngoc Tin
What is java Servlet?
One of technologies of J2EE/JEE Platform.
Java Servlet is Java component that runs on web
container.
Java Servlets “talks” to Web client base on request/
response that is managed by Servlet Container.
@2008 Huynh Ngoc Tin
What is servlet container (Web container)?
A Web container is essentially the component of a Web
server that interacts with the servlet.
The Web container is responsible for managing the life
cycle of servlets
Mapping a URL to a particular servlet and ensuring that
the URL requester has the correct access rights.
@2008 Huynh Ngoc Tin
What is servlet container (Web container)?
@2008 Huynh Ngoc Tin
Java Servlet on JEE Platform
The javax.servlet and javax.servlet.http packages
provide interfaces and classes for writing servlets.
The Java Servlet API allows a software developer to
add dynamic content to a Web server using the Java
platform. The generated content is commonly HTML, but
may be other data such as XML.
The HttpServlet class provides methods, such as doGet and
doPost, for handling HTTP-specific services.
@2008 Huynh Ngoc Tin
Servlet API
@2008 Huynh Ngoc Tin
Servlet API historyPackages: javax.servlet & javax.servlet.http
@2008 Huynh Ngoc Tin
Servlet Interface
@2008 Huynh Ngoc Tin
Servlet Interface
Contains methods which are implemented by your
servlets
Define methods to:
Init a java servlet.
Handle requests from the client side.
Remove servlet from the server.
Your Servlets can implements Servlet Interface or
extends from a other class that implemented Servlet
Interface such as GenericServlet & HttpServlet.
@2008 Huynh Ngoc Tin
Servlet Interface
public abstract class GenericServlet
extends Object implements Servlet,
ServletConfig, Serializable
public abstract class HttpServlet
extends GenericServlet implements
Serializable
GenericServlet định nghĩa 1 servlet
độc lập protocol, để định nghĩa 1
servlet dùng trên Web ta có thể cho
kế thừa lớp HttpServlet.
@2008 Huynh Ngoc Tin
Servlet Interface
Handle request service method: servlet Interface define
service method to handle requests from the client side.
Servlet Container will call service method to handle the request from the client side.
@2008 Huynh Ngoc Tin
Servlet Interface
HttpRequest methods• HttpServlet have some more methods to handle
HttpRequest: doGet for handling HTTP GET requests doPost for handling HTTP POST requests …
• The above methods can are called automatically by service method
@2008 Huynh Ngoc Tin
Servlet life circle
Servlet container have only one instance for each servlet.
Instance is handle by init, service, destroy in Servlet
Interface (JEE API).
Servlet Container load and init servlets.
Servlet Container have init servlets before the request
come from the client.
Servlet Container have to choose a servlet for the
request and make decision what servlet will be removed
from the server.
@2008 Huynh Ngoc Tin
Request & Reponse
@2008 Huynh Ngoc Tin
Request & Reponse
Request Package all information that comes from the client. Request from Client -> Server follows the HTTP
protocol HttpServletRequest methods can summarize, read
the request’s information.
@2008 Huynh Ngoc Tin
Request & Response
Response Package all information that will response for the
client. Information in response object are sent to client by
HTTT protocol HttpServletReponse methods can summarize,
package information into response object.
Request & Response
Hypertext Transfer Protocol reference to:
http://djce.org.uk/dumprequest
http://www.w3.org/Protocols/rfc2616/rfc2616.html
http://www.faqs.org/rfcs/rfc2616.html
@2008 Huynh Ngoc Tin
Example – Login Servlet
Step by step Define a class name LoginServlet extends from
GenericServlet or HttpServlet class Implement/override the following methods
init() service() destroy()
Compile and deploy on webserver. Call to run by URL from the client side
@2008 Huynh Ngoc Tin
Example – Login Servlet
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class LoginServlet extends HttpServlet {
public void service(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// ….
}
}
@2008 Huynh Ngoc Tin
Example – Login Servlet
// inside the service method
…
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
String strUsername = request.getParameter("Username");
String strPassword = request.getParameter("Password");
…
@2008 Huynh Ngoc Tin
Example – Login Servlet
Deploy the servlet on Apache Tomcat Web Server
Web Server Apache Tomcat (read the document of other web servers to know how to deploy a servlet) Step 1: Copy servlet file class into WEB-INF\classes
of your web application. Step 2:
Add information about your servlet into web.xml file to notify with the web server or application server.
WEB-INF\web.xml
@2008 Huynh Ngoc Tin
Example – Login Servlet
web.xml file (configuration)
<web-app>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlets/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
@2008 Huynh Ngoc Tin
Example – Login Servlet
• Call a servlet from client side
• Call a servlet when user clicks submit or login button from the LoginForm/client side
• …• <Form action="../../servlets/LoginServlet"
method="post">• …
Directly Using the connection pool
Connecting to Database
Connecting to Database
Connecting to Database
Connecting to Database
Connecting to Database
Connecting to Database
@2008 Huynh Ngoc Tin
Deploy a web app on Tomcat webserver
@2008 Huynh Ngoc Tin
Exercises
Study by yourself client side programming such as HTML, Java Script.
Build, deploy and run servlets which do the below functions using NetBean 6.1 (include JBoss, GlassFish Application Server) Registration (RegisterServlet) Login (LoginServlet) Change password, registered information
(UpdateUserInformationServlet) Read more about JBoss, GlassFish, Tomcat document to
deploy a java servlet