The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search

Trail: Servlets

Lesson: 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.

 

Requests and Responses

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.

 

Handling GET and POST Requests

The methods to which the service method delegates HTTP requests include,
 

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.

 

Threading Issues

HTTP servlets are typically capable of serving multiple clients concurrently. If the methods in your servlet do work for clients by accessing 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.)

 

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.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search