Home > Articles

  • Print
  • + Share This

The HttpServlet Class

You've seen that two prebuilt servlet classes are available for you to subclass, but so far you don't 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 the HTTP protocol. The HttpServlet provides separate methods for handling the different type of HTTP requests (GET, POST, PUT, and so on). The two most common types of HTTP request are GET and POST, which are handled by the doGet and doPost methods:

 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. 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 the HTTP protocol. These classes are introduced more fully in the next few chapters and discussed in detail in Chapter 13.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.