Difference between GenericServlet and HttpServlet

GenericServlet and HttpServlet


javax.servlet.GerericServlet
javax.servlet.http.HttpServlet


Servlet is a server-side technology used to implement dynamic and robust web applications. There are three ways to implement Servlet in Java.
By implementing the servlet interface. By extending the GenericServlet or HttpServlet.

  • Servlet(I)
  • GenericServlet(AC)
  • HttpServlet(AC)


Servlet

Servlet interface is the parent interface for both GenericServlet and HttpServlet. There are five methods in servlet interface including three lifecycle methods init(), service(), and destroy().

GenericServlet

GenericServlet is generic and protocol independent we can use with HTTP, SMTP, FTP, and CGI. 
GenericServlet is the immediate child class for the Servlet interface. 
The service() method is the abstract class in GenericServlet. Anyone extending the GenericServlet should implement in the service method.
It extends Object class and implements Servlet, ServletConfig and Serializable interface. 
GenericServlet is not a commonly used class for creating servlet.

HttpServlet

HttpServlet is protocol dependent we can use only with HTTP. HttpServlet is the immediate child class for GenericServlet. 
The service method is non-abstract in HttpServlet. It overrides the service method in GenericServlet. 
We can replace the service() with goGet() or doPost() methods parameters are the same for both of them. HttpServlet extends GenericServlet and implements Serializable interface.
It is the most commonly used class for creating servlet.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top