[ Team LiB ] Previous Section Next Section

The HttpServlet Class

You've seen that two prebuilt servlet classes are available for you to subclass, but so far you may not know why you would choose HttpServlet over GenericServlet. The main difference between the two is that HttpServlet has extra methods and special request-and-response objects that are geared toward HTTP. HttpServlet provides separate methods for handling the different types of HTTP requests (GET, POST, PUT, and so on). The two most common types of HTTP requests are GET and POST, which are handled by the doGet and doPost methods, like so:


protected void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, java.io.IOException;

protected void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, java.io.IOException;

As you can see, the doGet and doPost methods are of the same general form as the service method (you saw the doGet method in an earlier example). The service method of HttpServlet looks at the type of the HTTP request and then calls the appropriate handler methods. The HttpServletRequest and HttpServletResponse classes contain extra methods for dealing with HTTP. These classes are introduced more fully in the next few hours and discussed in detail in Hour 6.

    [ Team LiB ] Previous Section Next Section