Thursday, April 20, 2023

Difference between GenericServlet vs HttpServlet in Servlet JSP - J2EE question

Difference between GenericServlet and HttpServlet is one of the classic Servlet Interview Question, asked on many Servlet and JSP Interviews on 2 to 4 years experience developers. Since both GenericServlet and HttpServlet form the basis of Servlets its important to know What are they and What is main difference between them. From common sense and there names, its obvious that GenericServlet is a generic and protocol-independent implementation of Servlet interface while HttpServlet implements HTTP protocol specifics. If you are working in Java web application or J2EE projects, you are most likely to deal with HttpServlet all time as HTTP is main communication protocol of web. In this Servlet JSP article we will outline some important difference between HttpServlet and GenericServlet which is worth knowing and remembering.

GenericServlet vs HttpServlet

Here is my list of difference between HttpServlet and GenericServlet in Java Servlet API :

1) GenericServlet provides abstract service(ServletRequest, ServletResponse) method to implement which gets called by the container whenever it receives request for processing, On the other hand, HttpServlet overrides service method and provides callback on doGet(HttpServletRequest request, HttpServletResponse) and doPost(HttpServletRequest request, HttpServletResponse response) whenever it receives an HTTP request from GET or POST method. It also provides several other method based upon various HTTP methods of sending request e.g. doPut() or doDelete() to handle HTTP PUT and HTTP DELETE request.



2) Another difference between GenericServlet and HttpServlet is that later is a subclass of GenericServlet and inherit properties of GenericServlet.

3) Generic servlet provides a rather easier way to extend Servlet, its enough to override service method to implement GenericServlet. Apart from extending Servlet interface, it also implements ServletConfig interface and provides way to accept initialization parameter passed to Servlet from web.xml e.g. by using getInitParamter().

Difference between GenericServlet and HTTPServlet in JSP
That's all on difference between GenericServlet and HttpServlet in Servlet API. Its very rare you are going to use GenericServlet but its good to know basics as its mostly asked in various Servlet interviews as well. HttpServlet is primary Servlet class which is used in Java web application from handling client request.


Other Servlet and JSP questions you like to prepare

3 comments:

  1. though GenericServlet is protocol-independent servlet, so can we call a GenericServlet using SMTP or FTP, etc protocalls..?

    ReplyDelete
    Replies
    1. why GenericServlet is protocol-independent servlet

      Delete
    2. bcz it is kind of adapter class, came to make us free from overriding all 5 methods of servlet interface while u make your own servlet class.As its name tells its a general kind of servlet and it is not specific to any protocol but HttpServlet class is http protocol specific i.e. protocol-dependent. So GenericServlet is prortocol-independent. I hope u got it.

      Delete

Feel free to comment, ask questions if you have any doubt.