What is difference between HttpServlet and GenericServlet. and when to use and why. what is the importance of each.

Showing Answers 1 - 6 of 6 Answers

nag

  • Oct 28th, 2006
 

GenericServlet:

GenericServlet class implements the Servlet interface. 

GenericServlet can be  used to handle any type of requests and reponses, such as FTP..

GenericServlet has service() method to handle requests..and init and destroy methods(life cycle methods) .

it does not maintain client session.

HttpServlet:

HttpServlet is the subclass of GenericServlet ,so it has all the life cycle methods and also has methods which support cookies, sessions...

HttpServlet is used to handle only Http request and response.

HttpServlet extends GenericServlet and adds support to doGet(),doPost(),doHead() and doPut(), doOptions(), doDelete(), doTrace() methods.

HttpServlet maintains client session.

Every servlet must extend one of this abstract classes. Since servlets do not have main() ,each servlet must implement Servlet interface by using any one of these classes. 

  Was this answer useful?  Yes

Syed

  • Oct 31st, 2006
 

The diffrence between httpservlet & the generic servlet is that httpservlet is subclass of generic servlet & it has methods  that can handle  http specific  request   & responses & that its  easier to implemenet http specific tasks when using http servlet.

  Was this answer useful?  Yes

senthil Kumaravel

  • Nov 4th, 2006
 

  Difference between HttpServlet and GenericServlet :

     Frist both Class is Abstract Class. GenericServelt implement Servlet interface. HttpServlet is a abstact class which is extend GenericServlet.

   Main Difference is Protocol Access. GenericServelt access all type of Protocol but HttpServelt access only Http-Protocol.

  GenericServelt has init().inint(ServeltConfig),service(ServletRequest,ServletResponse),destroy(),getInitParameter(String),getInitParameterNames()..

 HttpServelt has service(HttpServeltRequest,HttpServletResponse),

doGet(),doPost,doHead() ...

  Was this answer useful?  Yes

Rajkumar

  • Nov 7th, 2006
 

Every servlet class must implement the servlet interface.But most of the time it implements this interface by either extending Generic Servlet or HttpServlet class.Both of these classes are abstract classes.Generic Servletclass implements Servlet Interface.This class implements all the methods of the servlet interface except service()method. User is responsible for implemeting this service()method.This Generic Servletclass is independent to any protocol..This class having another version of init()method public void init() throws ServletExceptionHttpServlet class is specific to Http.This class extends the Generic Servlet.This class also abstact class.This class having two types of service()methods.public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException;protected void service(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException;when we a make a request to servlet the first version of service() method is called.This methods convets the request and response objects to Http type then ot calls the protects service method with new objects(i.e,HttpServletRequest,HttpServletResponse).This method is designed in such a way that depending upon the type of the requset method it calls either doGet() or doPost() method...

  Was this answer useful?  Yes

A.Naresh Kumar Reddy

  • Jan 6th, 2007
 

HttpServlet Provides an abstract class to besubclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServletmust override at least one method, usually one of these:

  • doGet, if the servlet supports HTTP GET requests
  • doPost, for HTTP POST requests
  • doPut, for HTTP PUT requests
  • doDelete, for HTTP DELETE requests
  • init and destroy, to manage resources that are held for the life of the servlet
  • getServletInfo, which the servlet uses to provide information about itself

There's almost no reason to override the service method. servicehandles standard HTTP requests by dispatching them to the handler methods foreach HTTP request type (the doXXX methods listed above).Likewise, there's almost no reason to override the doOptions and doTracemethods.

GenericServlet defines a generic, protocol-independent servlet. Towrite an HTTP servlet for use on the Web, extend HttpServletinstead.

GenericServlet implements the Servlet and ServletConfiginterfaces. GenericServlet may be directly extended by a servlet,although it's more common to extend a protocol-specific subclass such as HttpServlet.

GenericServlet makes writing servlets easier. It provides simpleversions of the lifecycle methods init and destroy andof the methods in the ServletConfig interface. GenericServletalso implements the log method, declared in the ServletContextinterface.

To write a generic servlet, you need only override the abstract servicemethod.

  Was this answer useful?  Yes

HttpServlet class is extended from GenericServlet, which in turn implements the Servlet interface. When you are developing your own servlets, you will likely extends one of these two classes.
When extending the GenericServlet class, you must implement the service() method. But in case of extending the HttpServlet you do not need usually implement service() method as HttpServlet has already implemented the service() method for you.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions