Java Reference
In-Depth Information
GenericServlet
Request
Client
Response
service()*
Web Server
* abstract method
F
IGURE
2.4
A
GenericServlet
Request.
Unlike the
GenericServlet
, when you extend
HttpServlet
, you don't usually implement the
service()
method. The
HttpServlet
class has already implemented it for you. The following
is the prototype:
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException;
When the
HttpServlet.service()
method is invoked, it reads the method type stored in the
request and determines which method to invoke based on this value. These are the methods
that you will want to override. If the method type is
GET
, the
service()
method will call
doGet()
. If the method type is
POST
, it will call
doPost()
. Five other method types exist; they
are discussed in Chapter 3, “Servlet Basics.” All these methods have the same parameter list as
the
service()
method.
You might have noticed the different request/response types in the parameter list of the
HttpServlet
and the
GenericServlet
classes. The
HttpServletRequest
and
HttpServletResponse
classes are just extensions of
ServletRequest
and
ServletResponse
with HTTP-specific information stored in them. Figure 2.5 diagrams the flow of a
HttpServlet
request.
HttpServlet
doDelete()
doGet()
doOptions()
doPost()
doPut()
doTrace()
Request
Client
service()
Response
Web Server
F
IGURE
2.5
A HttpServlet Request.