Difference GenericServlet and HttpServlet

Difference GenericServlet and HttpServlet

In the Java Servlet API, both GenericServlet and HttpServlet are classes that provide a structure for handling client requests and generating responses in web applications. However, they serve different purposes and have distinct functionalities.

GenericServlet

  • Purpose:  GenericServlet is an abstract class that implements the Servlet and ServletConfig interfaces. It provides a framework for creating general-purpose servlets.
  • Protocol-Independent:  It is protocol-independent, meaning it can be used for servlets that might use protocols other than HTTP.
  • Method to Implement:  Developers need to override the service() method to handle requests.

HttpServlet

  • Purpose:  HttpServlet is a subclass of GenericServlet that is specifically designed for handling HTTP requests.
  • HTTP Protocol-Specific:  It is tailored for the HTTP protocol, providing methods to handle various HTTP methods (GET, POST, PUT, DELETE, etc.).
  • Methods to Implement:  Developers typically override methods like doGet(), doPost(), doPut(), and doDelete() to handle specific types of HTTP requests.

GenericServlet and HttpServlet

Detailed Comparison GenericServlet and HttpServlet

  • 1.  Base Class:
    • GenericServlet: Extends java.lang.Object and implements Servlet, ServletConfig.
    • HttpServlet: Extends GenericServlet and is protocol-specific to HTTP.
  • 2.  Method to Override:
    • GenericServlet: Requires overriding the service() method.
    • HttpServlet: Typically overrides methods like doGet(), doPost(), etc.
  • 3.  Use Case:
    • GenericServlet: Used for creating protocol-independent servlets.
    • HttpServlet: Used for creating HTTP-specific servlets.
  • 4.  Convenience Methods:
    • GenericServlet: Does not provide any HTTP-specific convenience methods.
    • HttpServlet: Provides numerous methods for handling HTTP-specific tasks such as doGet(), doPost(), doPut(), doDelete(), doHead(), doOptions(), and doTrace().

GenericServlet Implementation:
1.  GenericServlet Implementation: 

java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ExampleGenericServlet extends GenericServlet {
    @Override
    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("<html><body>");
        out.println("<h2>Example of GenericServlet</h2>");
        out.println("</body></html>");
    }
}

Example 2: Using HttpServlet
2.  HttpServlet Implementation: 

java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/exampleHttp")
public class ExampleHttpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<html><body>");
        response.getWriter().println("<h2>Example of HttpServlet</h2>");
        response.getWriter().println("</body></html>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<html><body>");
        response.getWriter().println("<h2>Handling POST Request in HttpServlet</h2>");
        response.getWriter().println("</body></html>");
    }
}

Explanation of the GenericServlet and HttpServlet

  • GenericServlet Example:
  • The ExampleGenericServlet class extends GenericServlet and overrides the service() method to handle all types of requests. It generates a simple HTML response indicating the use of GenericServlet.
  • HttpServlet Example:
  • The ExampleHttpServlet class extends HttpServlet and overrides the doGet() and doPost() methods to handle HTTP GET and POST requests, respectively. Each method generates a simple HTML response indicating the use of HttpServlet and the specific HTTP method handled.

Homepage

Readmore