|
|
Interacting with Clients
An HTTP Servlet handles client requests through its service
method. The service method supports standard HTTP client
requests by dispatching each request to a method designed to handle that
request. For example, the service method calls the
doGet method shown earlier in the simple example servlet.
This section discusses using the objects that represent the client's
request (an HttpServletRequest object) and the servlet's
response (an HttpServletResponse object). These objects are
provided to the service method and to the methods that
service calls to handle HTTP requests.
The methods to which the service 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's service method also calls
the doOptions method when the servlet receives an OPTIONS
request, and doTrace when the servlet receives a TRACE
request. The default implementation of doOptions
automatically determines what HTTP options are supported and returns that
information. The default implementation of doTrace causes a
response with a message containing all of the headers sent in the trace
request. These methods are not typically overridden.
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:
- Synchronize access to that resource, or
- Create a servlet that handles only one client request at a time
This lesson shows you how to implement your second option. (The first is
covered in the tutorial's lesson
on threads.)
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.
|