Java67 - Java Program Example Tutorial Blog

Java67 - Java Program Example Tutorial Blog


Difference between GenericServlet vs HttpServlet in Servlet JSP - J2EE question

Posted: 16 Dec 2012 04:17 AM PST

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 forms 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

Difference between GenericServlet and HTTPServlet in JSP
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 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 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().

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


Post a Comment