HttpServlet class in Servlet

As of now, we have discussed what is GenericServlet. In this article, we will discuss what is HttpServlet.In order to create a servlet, we have first seen by implementing ServletInterface Interface, then we saw By extending GenericServlet class. Now we will see by extending HttpServlet class.

What is HttpServlet?

HttpServlet is a class that extends the GenericServlet and Serializable interface. It provides the framework for handling the HTTP based protocol request. As we know, in GenericServlet, we need to override the service() method but in the case of HttpServlet we don’t need to do that instead it overrides specific methods like doGet(),doPost(), etc based on the request.

How HttpServlet Works?

Here the client(web browser) requests the server. These requests can be of any type like- GET, POST, etc. Now Web Server will dispatch these requests to the service() method for handling the request. Now service() will see if it is a GET request it will dispatch to doGet()  method or if it is a post request it will dispatch to doPost() and likewise.

Advantages of HttpServlet class

  • It is convenient to use HttpServlet over GenericServlet.
  • If a programmer uses HttpServlet, he can use the functionalities of both the classes (itself & GenericServlet).

Disadvantages of HttpServlet class

  • It is protocol dependent.

Methods in HttpServlet class

  • public void service(ServletRequest req, ServletResponse res): This method dispatches the request to any HTTP type.
    • Parameters:
      • req: request from the client.
      • res: response to the client.
  • protected void service(HttpServletRequest req, HttpServletResponse res):This is the HTTP version of the service() method which accepts HTTP specific request.
  • protected void doGet(HttpServletRequest req, HttpServletResponse res):This method handle the GET request.It is used to get information from the server.
  • protected void doPost(HttpServletRequest req, HttpServletResponse res):This method handle the POST request.It is used for sending information to the server.
  • protected void doHead(HttpServletRequest req, HttpServletResponse res):This method handles the HEAD requests.It is used to send header information with no entity-body.
  • protected void doOptions(HttpServletRequest req, HttpServletResponse res): This method handles the option request.
  • protected void doPut(HttpServletRequest req, HttpServletResponse res): This method handles the PUT request.It allows a client to store information on the server.
  • protected void doTrace(HttpServletRequest req, HttpServletResponse res): This method handles the TRACE request.A TRACE returns the headers sent with the TRACE request to the client so that they can be used for debugging.
  • protected void doDelete(HttpServletRequest req, HttpServletResponse res): This method handles the DELETE request.It allows the client to delete information from the server.
  • protected long getLastModified(HttpServletRequest req): This method return the time when the request was last modified.

Let’s see the Example for HttpServlet

In this example, We have created our Servlet(HttpServletExample.java) which extends HttpServlet class. Create an index.html file inside WebContent/index.html.

Now we have a web.xml file (it is a deployment descriptor file). We will see the explanation ahead in the tutorial.

Difference between GenericServlet & HttpServlet class

 

GenericServlet

HttpServlet

A class that implements Servlet, ServletConfig, and Serializable Interface. A class that extends the GenericServlet class and implements the Serializable interface.
Protocol Independent. Protocol dependent.
Immediate subclass of ServletInterface. Immediate subclass of GenericServlet.
Defined in javax.servlet package. Defined in javax.servlet.http package.
service() method is abstract here. service() method is not abstract here.
Not used commonly It is used more often.