Chapter 10. Servlet
ITSS Java Programming NGUYEN Hong Quang, HUT
Setting of Tomcat plugins (1)
Setting of Tomcat plugins (2)
To compile servlet based program
Start and stop of Tomcat
Start Tomcat
Stop Tomcat
Restart Tomcat
Servlet execution procedure
Creating a project (Tomcat project)
Creating a Servlet
Registering the Servlet
Starting the Servlet
Servlet execution procedure
Tomcat project
Servlet program
Libraries
Web.xml : configuration file
Servlet execution procedure
Making of Tomcat project (1)
Servlet execution procedure
Making of Tomcat project (2)
Making of Tomcat project (3)
Servlet execution procedure
Test Tomcat project by HTML file (1)
Create “test.htm”
Servlet execution procedure
Test Tomcat project by HTML file (2)
Input file name : “test.htm”
Servlet execution procedure
Test Tomcat project by HTML file (3)
Open HTML file : “test.htm”
Servlet execution procedure
Test Tomcat project by HTML file (4)
Open HTML file :
“test.htm”
Servlet execution procedure
Test Tomcat project by HTML file (5)
Input HTML code for “test.htm”
Servlet execution procedure
Test Tomcat project by HTML file (6)
View “test.htm” in Eclipse
Servlet execution procedure
Test Tomcat project by HTML file (7)
Start Tomcat in Eclipse Use a Web Browser to view “test.htm”
Servlet execution procedure
Creating a Servlet class
Registration of Servlet Web.xml and its creation
Create web.xml in WEBINF directory
Servlet execution procedure
Servlet execution procedure
Run example
When rename URL of a servlet, it must be
restart Tomcat
Servlet execution procedure
Detail of HelloWorldServlet.java (1)
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
// Initialization of Servlet public void init() throws ServletException {
System.out.println("init");
}
Servlet execution procedure
Detail of HelloWorldServlet.java (2)
// Service of Servlet public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.println("doGet");
res.setContentType("text/html; charset=utf8"); PrintWriter pw = res.getWriter(); pw.println("
}
// Termination of Servlet public void destroy() {
System.out.println("destroy");
} }
Servlet execution procedure
Detail of web.xml (1)
Servlet execution procedure
Detail of web.xml (2)
Servlet Outline
Servlet API, Servlet interface method, Operation principle and lifecycle of Servlet
Servlet outline
Servlet API
servlet configuration information
javax.servlet Interface Servlet
javax.servlet Interface ServletConfig
public abstract class GenericServlet
public abstract class HttpServlet
Servlet outline
javax.servlet Interface Servlet
Defines methods that all servlets must
implement : initialize a servlet, implement service requests, remove a servlet from the server
Servlet outline
Lifecycle methods of a servlet
The servlet is constructed, then initialized with the
init method.
Any calls from clients to the service method are
handled.
The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.
Servlet outline
GenericServlet
GenericServlet implements the Servlet and
ServletConfig interfaces.
It provides simple versions of the lifecycle methods
init and destroy and of the methods in the ServletConfig interface.
To write a generic servlet, you need only override
the abstract service method.
Servlet outline
HttpServlet (1)
An HTTP servlet is using suitable for a Web site A subclass of HttpServlet override usually one of
these methods: doGet, if the servlet supports HTTP GET requests doPost, for HTTP POST requests init and destroy, to manage resources that are held for
the life of the servlet
getServletInfo, which the servlet uses to provide
information about itself
getServletConfig, which the servlet uses to store the
configuration parameters
Servlet outline
HttpServlet (2)
There's almost no reason to override the service
method: service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).
Implementation of a HttpServlet
Definition of Servlet class Specification of encoding method, Get a parameter from the request, Specification of contents type, Acquisition of steam to client Output to the client.
Implementation of a HttpServlet
Definition of HttpServlet class
import javax.servlet.*; public class MyHttpServlet extends HttpServlet {
// Initialization of Servlet
public void init() throws ServletException { … } // Termination of Servlet public void destroy() { … }
// Service of Servlet public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { … }
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { … }
}
Implementation of a HttpServlet
HTTP method : GET and POST
GET
Add transmission information to URL. There is a limitation in the length of the
transmission character string.
Example: http://localhost:8080/examples/hello?
NAME=Billy
POST
There is not a limitation in the length of the
transmission character string.
It is possible to transmit only by
Implementation of a HttpServlet
Interface with HttpServlet (2)
HttpServletRequest
Provide client request information to a servlet
HttpServletResponse
Assist a servlet in sending a response to the client
Implementation of a HttpServlet
Specification of encoding method
public void setCharacterEncoding (String charset) Sets the character encoding (MIME charset) of the
response being sent to the client
Or specify the character encoding used in the body
of this request
Using “utf8” for Vietnamese Example : public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setCharacterEncoding("utf8"); …
}
Implementation of a HttpServlet
Get a parameter from the request
public String getParameter(String name)
Example :
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
}
String name = req.getParameter("name"); int age = Integer.parseInt(req.getParameter(“age")); …
Implementation of a HttpServlet
Specification of contents type
public void setContentType(String type)
Example :
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
}
res.setContentType("text/html"); …
Acquisition of data stream to client
Syntax : PrintWriter
javax.servlet.ServletResponse.getWriter() throws IOException
Example :
Implementation of a HttpServlet
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
}
PrintWriter pw = res.getWriter(); …
Output to client
Syntax : void java.io.PrintWriter.println (String x) Example :
Implementation of a HttpServlet
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter pw = res.getWriter(); pw.println("
Client : " + req.getRemoteHost()); pw.println("