An HTTP Servlet handles client requests through its
servicemethod supports standard HTTP client requests by dispatching each request to a method designed to handle that request. For example, the
servicemethod calls the
doGetmethod shown earlier in the simple example servlet.
Requests and ResponsesThis section discusses using the objects that represent the client's request (an
HttpServletRequestobject) and the servlet's response (an
HttpServletResponseobject). These objects are provided to the
servicemethod and to the methods that
servicecalls to handle HTTP requests.
Handling GET and POST RequestsThe methods to which the
servicemethod 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.
servicemethod also calls the
doOptionsmethod when the servlet receives an OPTIONS request, and
doTracewhen the servlet receives a TRACE request. The default implementation of
doOptionsautomatically determines what HTTP options are supported and returns that information. The default implementation of
doTracecauses 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:
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
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.