HttpServlet Class in Java

Spread the love

Servlet can be created by ex tending the HttpServlet class and it is the preferred way of creating Servlet.

It is preferred because the HttpServlet class provides many methods to work with unlike the Generic Servlet.

HttpServlet class doesn’t have the init and destroy method unlike the Servlet interface.So the Servlet extending the HttpServlet class needs to provide the concrete implementations of those methods.

One important point related to the HttpServlet class is that it has an service method which redirects the request to the doGet or doPost method as per the client request. So no need for the Servlet class to define the service method as it is already implemented in the parent HttpServlet Class.

Let’s take a look into the implementation below.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Creating Servlet by Extending HttpServlet class
public class ServletSample extends HttpServlet 
{    
    private String msg;

    public void init() throws ServletException 
    {      
       msg = "Implementing Servlet using HttpServlet";   
    }
 
    @Override
    public void doGet(HttpServletRequest request, 
        HttpServletResponse response) throws ServletException, 
        IOException 
    {            
        // Setting up the content type of web page      
        response.setContentType("text/html");
        // Writing the message on the web page      
        PrintWriter out = response.getWriter();      
        out.println("<h1>" + msg + "</h1>");      
        out.println("<p>" + "Hello World. You received an response from an Http Servlet" + "</p>");   
    }

    @Override
    public void doPost(HttpServletRequest request, 
        HttpServletResponse response) throws ServletException, 
        IOException 
    {            
          .............
          .............
          .............
    }

    public void destroy() 
    {      
       // Implement the steps here if you want to perform  
       //something at the end of Servlet life cycle.   
    }
}

As seen above, the servlet implemented has an Overriden doGet and a doPost method which are used to service the client requests. If it is GET request from the client, then the web server forwards that request to the Servlet which then sends it to the service method. Service method then on the basis of the type of the request calls the doGet or doPost method.

Similarly there are provisions for Head, PUT and Delete requests as well.

1. protected void doGet(HttpServletRequest req, HttpServletResponse resp)

2. protected long getLastModified(HttpServletRequest req)

3. protected void doHead(HttpServletRequest req, HttpServletResponse resp)

4. protected void doPost(HttpServletRequest req, HttpServletResponse resp)

5. protected void doPut(HttpServletRequest req, HttpServletResponse resp)

6. protected void doDelete(HttpServletRequest req, HttpServletResponse resp)

7. protected void doOptions(HttpServletRequest req, HttpServletResponse resp)

8. protected void doTrace(HttpServletRequest req, HttpServletResponse resp)

9. protected void service(HttpServletRequest req, HttpServletResponse resp)

10. public void service(ServletRequest req, ServletResponse res)