Chapter 24: Servlets: 2003 Prentice Hall, Inc. All Rights Reserved
Chapter 24: Servlets: 2003 Prentice Hall, Inc. All Rights Reserved
Chapter 24: Servlets: 2003 Prentice Hall, Inc. All Rights Reserved
Outline
24.1 Introduction
24.2 Servlet Overview and Architecture
24.2.1 Interface Servlet and the Servlet Life Cycle
24.2.2 HttpServlet Class
24.2.3 HttpServletRequest Interface
24.2.4 HttpServletResponse Interface
24.3 Handling HTTP get Requests
24.3.1 Setting Up the Apache Tomcat Server
24.3.2 Deploying a Web Application
24.4 Handling HTTP get Requests Containing Data
24.5 Handling HTTP post Requests
24.6 Redirecting Requests to Other Resources
24.7 Multi-Tier Applications: Using JDBC from a Servlet
24.8 Internet and World Wide Web Resources
• Servlets
– Thin clients
– Request/response mechanism
– redirection
• Tomcat
– Jakarta project
– Official reference implementation of the JSP and servlet
standards
void destroy()
This “cleanup” method is called when a servlet is terminated by its servlet container. Resources
used by the servlet, such as an open file or an open database connection, should be deallocated here.
Fig. 24.1 Methods of interface Servlet (package javax.servlet).
Method Description
doDelete Called in response to an HTTP delete request. Such a request is normally used
to delete a file from a server. This may not be available on some servers, because
of its inherent security risks (e.g., the client could delete a file that is critical to
the execution of the server or an application).
doHead Called in response to an HTTP head request. Such a request is normally used
when the client only wants the headers of a response, such as the content type and
content length of the response.
doOptions Called in response to an HTTP options request. This returns information to the
client indicating the HTTP options supported by the server, such as the version of
HTTP (1.0 or 1.1) and the request methods the server supports.
doPut Called in response to an HTTP put request. Such a request is normally used to
store a file on the server. This may not be available on some servers, because of
its inherent security risks (e.g., the client could place an executable application on
the server, which, if executed, could damage the server—perhaps by deleting
critical files or occupying resources).
doTrace Called in response to an HTTP trace request. Such a request is normally used
for debugging. The implementation of this method automatically returns an
HTML document to the client containing the request header information (data
sent by the browser as part of the request).
Fig. 24.2 Other methods of class HttpServlet.
• Web server
– creates an HttpServletRequest object
– passes it to the servlet’s service method
• HttpServletRequest object contains the
request from the client
String[]
getParameterValues
( String name )
For a parameter with multiple values, this method returns an array of strings containing the
values for a specified servlet parameter.
Cookie[]
getCookies()
Returns an array of Cookie objects stored on the client by the server. Cookie objects can
be used to uniquely identify clients to the servlet.
HttpSession
getSession(
boolean create )
Returns an HttpSession object associated with the client’s current browsing session.
This method can create an HttpSession object (true argument) if one does not already
exist for the client. HttpSession objects are used in similar ways to Cookies for
uniquely identifying clients.
Fig. 24.3 Some methods of interface HttpServletRequest.
• Web server
– creates an HttpServletResponse object
– passes it to the servlet’s service method
• get request
– Retrieve the content of a URL
• Example: WelcomeServlet
– a servlet handles HTTP get requests
Program output
Fig. 24.7 Tomcat documentation home page. (Courtesy of The Apache Software Foundation.)
2003 Prentice Hall, Inc. All rights reserved.
24.3.2 Deploying a Web Application
• Web applications
– JSPs, servlets and their supporting files
• Deploying a Web application
– Directory structure
• Context root
– Web application archive file (WAR file)
– Deployment descriptor
• web.xml
Directory Description
context root This is the root directory for the Web application. All the
JSPs, HTML documents, servlets and supporting files such
as images and class files reside in this directory or its
subdirectories. The name of this directory is specified by the
Web application creator. To provide structure in a Web
application, subdirectories can be placed in the context root.
For example, if your application uses many images, you
might place an images subdirectory in this directory. The
examples of this chapter use jhtp5 as the context root.
WEB-INF This directory contains the Web application deployment
descriptor (web.xml).
WEB-INF/classes This directory contains the servlet class files and other
supporting class files used in a Web application. If the
classes are part of a package, the complete package directory
structure would begin here.
WEB-INF/lib This directory contains Java archive (JAR) files. The JAR
files can contain servlet class files and other supporting class
files used in a Web application.
Fig. 24.8 Web application standard directories.
HTML document
in which the
form’s action
invokes
WelcomeServlet2
through the
alias welcome2
specified in
web.xml.
Program output
HTML document
in which the
form’s action
invokes
WelcomeServlet3
through the
alias welcome3
specified in
web.xml.
Program output
Descriptor Value
element
servlet element
servlet-name welcome3
description Handling HTTP post requests with
data.
servlet-class WelcomeServlet3
servlet-
mapping element
servlet-name welcome3
url-pattern /welcome3
Fig. 24.16 Deployment descriptor information for servlet
WelcomeServlet3.
RedirectServlet
.html document
to demonstrate
redirecting
requests to
other
resources.
Program output
Survey.html
document that
allows users to
submit survey
responses to
SurveyServlet.