Datasheet
Patrick c01.tex V3 - 09/18/2009 12:15pm Page 2
Chapter 1: Building Web Applications in WebLogic
Servlets Use the Request/ResponseModel
Java servlets are a request/response mechanism: a programming construct designed to respond to
a particular request with a dynamic response generated by the servlet’s specific Java implementa-
tion. Servlets may be used for many types of request/response scenarios, but they are most often
employed in the creation of HyperText Transfer Protocol (HTTP) responses in a web application.
In this role, servlets replace other HTTP request/response mechanisms such as Common Gateway
Interface (CGI) scripts.
The simple request/response model becomes a little more complex once you add chaining and
filtering capabilities to the servlet specification. Servlets may now participate in the overall
request/response scenario in additional ways, either by preprocessing the request and passing it
on to another servlet to create the response or by postprocessing the response before returning it
to the client. Later in this chapter, we discuss servlet filtering as a mechanism for adding auditing,
logging, and debugging logic to your web application.
ServletsArePureJavaClasses
Simply stated, a Java servlet is a pure Java class that implements the
javax.servlet.Servlet
inter-
face. The application server creates an instance of the servlet class and uses it to handle incoming
requests. The
Servlet
interface defines the set of methods that should be implemented to allow the
application server to manage the servlet life cycle (discussed later in this chapter) and pass requests
to the servlet instance for processing. Servlets intended for use as HTTP request/response mecha-
nisms normally extend the
javax.servlet.http.HttpServlet
class, although they may implement
and use the
Servlet
interface methods if desired. The
HttpServlet
class implements the
Servlet
interface and implements the
init()
,
destroy()
,and
service()
methods in a default manner. For
example, the
service()
method in
HttpServlet
interrogates the incoming
HttpServletRequest
object and forwards the request to a series of individual methods defined in the
HttpServlet
class
based on the type of request. These methods include the following:
❑
doGet()
for handling
GET
, conditional
GET
,and
HEAD
requests
❑
doPost()
for
POST
requests
❑
doPut()
for
PUT
requests
❑
doDelete()
for
DELETE
requests
❑
doOptions()
for
OPTIONS
requests
❑
doTrace()
for
TRACE
requests
The
doGet()
,
doPost()
,
doPut()
,and
doDelete()
methods in
HttpServlet
return a
BAD_REQUEST
(400) error as their default response. Servlets that extend
HttpServlet
typically override and
implement one or more of these methods to generate the desired response. The
doOptions()
and
doTrace()
methods are typically not overridden in the servlet. Their implementations
in the
HttpServlet
class are designed to generate the proper response, and they are usually
sufficient.
A minimal HTTP servlet capable of responding to a
GET
request requires nothing more than
extending the
HttpServlet
class and implementing the
doGet()
method.
2