Contents |
An HTTP Servlet handles client requests through its
service
method. Theservice
method supports standard HTTP client requests by dispatching each request to a method designed to handle that request. For example, theservice
method calls thedoGet
method shown earlier in the simple example servlet.
Requests and Responses
This section discusses using the objects that represent the client's request (anHttpServletRequest
object) and the servlet's response (anHttpServletResponse
object). These objects are provided to theservice
method and to the methods thatservice
calls to handle HTTP requests.
Handling GET and POST Requests
The methods to which theservice
method delegates HTTP requests include,
doGet
, for handling GET, conditional GET, and HEAD requests
doPost
, for handling POST requests
doPut
, for handling PUT requests
doDelete
, for handling DELETE requests
By default, these methods return a
BAD_REQUEST (400)
error. Your servlet should override the method or methods designed to handle the HTTP interactions that it supports. This section shows you how to implement methods that handle the most common HTTP requests: GET and POST.The
HttpServlet
'sservice
method also calls thedoOptions
method when the servlet receives an OPTIONS request, anddoTrace
when the servlet receives a TRACE request. The default implementation ofdoOptions
automatically determines what HTTP options are supported and returns that information. The default implementation ofdoTrace
causes a response with a message containing all of the headers sent in the trace request. These methods are not typically overridden.
Threading Issues
HTTP servlets are typically capable of serving multiple clients concurrently. If the methods in your servlet that do work for clients that access a shared resource, then you must either:
This lesson shows you how to implement your second option. (The first is covered in the tutorial's lesson on threads.)
- Synchronize access to that resource, or
- Create a servlet that handles only one client request at a time
Servlet Descriptions
In addition to handling HTTP client requests, servlets are also called upon to supply descriptions of themselves. This section shows you how to provide a description by overriding the method,
getServletInfo
, that supplies the servlet description.
Contents |