Saturday, April 28, 2012

J2EE: SERVLET: Class GenericServlet vs Class HttpServlet

Class GenericServlet : 

·          It is an abstract class which defines a generic and protocol independent servlet. Here, protocol independent means, by default, it doesn’t contain inherent support for any particular type of protocol, but it can be extended to provide implementation of any protocol.

·        Class GenericServlet implements Interface Servlet and Interface ServletConfig  and it belongs to javax.servlet package.

·          It offers simpler version of the servlet life cycle methods init() and destroy() and the methods of ServletConfig. That’s why, it makes writing a servlet easier.

·        Here, the method service(ServletRequest req, ServletResponse res) isabstract, so the subclasses must override it. And this is also the reason why theGenericServlet is an abstract class.

Class HttpServlet :

·        It is also an abstract class which defines HTTP prototype dependent servlet. That means we need to extend it to write a HTTP servlet for the use of the web.

·        Class HttpServlet is a subclass of Class GenericServlet and it belongs tojavax.servlet.http package.

·        As it’s an abstract class, a subclass of it must override at least 1 method, generally one of these doGet(), doPost(), doPut(), doDelete(), init(), destroy(), getServletInfo().

·        Class HttpServlet has two service() methods - one is public void service(ServletRequest req, ServletResponse res) which dispatches client request to protected void service(ServletRequest req, ServletResponse resp) which again dispaches the request to the doXXX methods (like doGet(), doPost(), etc.).


                                                      A servlet hierarchy

No comments:

Post a Comment