HTTP Request Handlers

The HttpServlet class extends the GenericServlet class and therefore inherits all of the standard servlet capabilities. In addition to the basic servlet life cycle methods and convenience method, the more complex HttpServlet class adds methods to aid in the processing of HTTP requests. These commonly used handler methods are

  • doGet: Handles HTTP GET requests

  • doPost: Handles HTTP POST requests

In the case of doGet, there is an additional method used for conditional HTTP GET support (the different HTTP request types are explained later in this section). The getLastModified method is like HTTP GET, but only returns content if it has changed since a specified time. This method can only be used if doGet has also been overridden and is intended to be used in cases where you are dealing with content that does not change much from request to request.

Advanced Handler Methods

There are several advanced handler methods that are defined as well:

  • doPut: Handles HTTP PUT requests

  • doDelete: Handles HTTP DELETE requests

  • doOptions: Handles HTTP OPTIONS requests

  • doTrace: Handles HTTP TRACE requests

Unlike the GenericServlet class, servlets based on HttpServlet have almost no valid reason to override the service method. Instead, you typically override these request handlers, which the base service method implementation calls when appropriate. The doOptions and doTrace methods also have virtually no valid reason to be overridden and are present only for full HTTP support. An HttpServlet must override at least one method, which usually means one of the remaining life cycle methods or request handlers.

Quick Guide to HTTP Requests

For the most commonly used request handler methods, the following list provides a quick guide of what the HTTP requests are for:

  • GET: A call to get information from the server and return it in a response to the client. The method processing this call must not have any side effects, so it can be repeated safely again and again. A GET call is typically used when a servlet URL is accessed directly from a Web browser or via a forward from a form on an HTML or JSP page. A GET call shows the data being passed to the servlet as part of the displayed URL on most Web browsers. In certain cases, this might not be very desirable from a security perspective.

  • POST: A call to allow the client to send data to the server. The method processing this call is allowed to cause side effects, such as updating of data stored on the server. A POST call can be used instead of a GET when forwarding from a form on an HTML or JSP page. Unlike GET, the use of POST hides from view any data being passed to the servlet. Some developers choose to process GET and POST exactly the same, or simply ignore one or the other if they do not want that particular call to be supported.

  • PUT: This call is similar to POST, but allows the client to place an actual file on a server instead of just sending data. It is also allowed to cause side effects, just like POST. Although available, the use of a PUT call is not very common.

  • DELETE: This call is similar to PUT, but allows the client to remove a file or Web page from the server. It is also allowed to cause side effects in the same way as PUT. Although available, the use of a DELETE call is not very common.

There is another request not specifically mentioned in the preceding list called HTTP HEAD. This request, although valid in the context of the HttpServlet class itself, is actually handled internally by making a call to the doGet method, which you might have overridden. It differs in that it only returns the response headers that result from processing doGet and none of the actual response data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.27.29