Difference between HttpServlet and GenericServlet

javax.servlet.GenericServlet
Signature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable

  • GenericServlet defines a generic, protocol-independent servlet.
  • GenericServlet gives a blueprint and makes writing servlet easier.
  • GenericServlet provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface.
  • GenericServlet implements the log method, declared in the ServletContext interface.
  • To write a generic servlet, it is sufficient to override the abstract service method.

javax.servlet.http.HttpServlet
Signature: public abstract class HttpServlet extends GenericServlet implements java.io.Serializable

  • HttpServlet defines a HTTP protocol specific servlet.
  • HttpServlet gives a blueprint for Http servlet and makes writing them easier.
  • HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.

This Servlet tutorial was added on 14/05/2008.

previous post: What is preinitialization of a java servlet?

next post: What is servlet mapping?

81 comments on “Difference between HttpServlet and GenericServlet

  1. j2ee question available in site find out please

  2. the method that u have defined for servlet mapping is not correct. i am not satisfied with it.

  3. aam voi saav tamne kai bhaan pade che ke nai su magaj nu dahi karo cho

  4. you are rong buddy the above method which definded by you that all the method are rong yaar

    firt diffrance

    http is protocol dependent
    genric is protocol indipendent

    http use service() method to handle request
    genric use doGet() or doPost() method to handle request

    http support cookie session url Rewriting while
    genric does not support

    so pls change above method which is definded by you ok

  5. Your site is vary good.i am get more knowledge of your exa. Gernic and http servlet.

  6. When httpservlet extends genericservlet——–> why httpservlet is not providing support for protocols other than http as parent class(genericServlet) supports FTP,SMTP….e.t.c..,

  7. Your site is superb ..complete knowledge provider.

  8. What is the Basic difference between Servlet and Jsp.?

    • Servlet is basically a java code which can include html where as jsp is combination of java code and html.

  9. Again another article explained in a simple and sophisticated way.
    However, I would like to see details on service(). GenericServlet implements service() method from Servlet interface. HttpServlet extends that method. It first checks the method of form post in HTML form (GET/POST) and then delegates the processing to doGet() and doPost() methods. These can be (usually are) overriden. Am I correct?

    Still, great work Joe. I like your blog a lot.

  10. your faculty having less knowleadge about servlet and provides the basic knowleadge.

  11. Hey, this site is good.
    I m gaining more about java from this site.

    Keep it up…….

  12. Hello,

    It’s good content on your site… Specially I like the arrangement of content and website look..

    Regards
    Chetan Pagar

  13. thanks this is really a good effort i’m satisfied with your answer

  14. Generic servlet is a protocal independent, http is the propthe
    Protocal dependent(HTTP protocol), httpServlet is a
    sub class of Generic servlet, generic servlrt class
    is a abstract class because of service() is not not implemented in the generic servlet.IN httpServlet
    service methods has been implemented. 2 service
    methods are implemented in HTTP servlet one is

    public void service(ServletRequest,Servlet Response)
    another is
    protected void service(HTTPServletRequest,
    HttpServletResponse).

    So with out having the service() method, we can
    deploy the servlet in case of httpServlet.

    in case of GenericServlet with out having the service() method, we can not
    deploy the servlet .

  15. Generic servlet is a protocal independent, http is the propthe
    Protocal dependent(HTTP protocol), httpServlet is a
    sub class of Generic servlet, generic servlrt class
    is a abstract class because of service() is not not implemented in the generic servlet.IN httpServlet
    service methods has been implemented. 2 service
    methods are implemented in HTTP servlet one is

    public void service(ServletRequest,Servlet Response)
    another is
    protected void service(HTTPServletRequest,
    HttpServletResponse).

    So with out having the service() method, we can
    deploy the servlet in case of httpServlet.

    in case of GenericServlet with out having the service() method, we can not
    deploy the servlet .

    sv on June 15th, 2011 7:00 am

  16. tuturanjan sahoo,ABA,Balasore,Odisha said on ,

    What is the difference between GenericServlet and HttpServlet?
    GenericServlet is for servlets that might not use HTTP, like for instance FTP service.As of only Http is implemented completely in HttpServlet. The GenericServlet has a service() method that gets called when a client request is made. This means that it gets called by both incoming requests and the HTTP requests are given to the servlet as they are.

  17. Thanx dear…..
    Its a very impotant interview question and its very easy to set in my mind this topics……

  18. This site is really great and authentic too…..

  19. this is helping me a lot thank you for keeping all these

  20. your solution is good but not the best as we expect from your side. please donot give us bookish knowledge tell what are happening inside the httpservlet and generic servlet.please sir elaborate the topic..

  21. Pankaj,

    Agree that this is book type article. Still lot of people are finding it useful :)

    Let me see if I can add some more info to it.

  22. GenericServlet belongs to javax.servlet package
    GenericServlet is an abstract class which extends Object and implements Servlet, ServletConfig and java.io.Serializable interfaces.
    The direct subclass to GenericServlet is HttpServlet.It is a protocol-independent servlet.
    To write a GenericServlet you need abstract service() to be overridden.

    HttpServlet:

    HttpServlet belongs to javax.servlet.http package
    This is an abstract class which extends GenericServlet and implements java.io.Serializable
    A subclass of HttpServlet must override at least one method of doGet(), doPost(),doPut(), doDelete(), init(), destroy(), getServletInfo()

  23. Its really fantastic ….now i got a clear idea about the Genric and HttpServlet …thanks for your explanation…..:

  24. 1(a).this generic servlet class implements from servlet and servletconfig interfaces
    1(b).http servlet extended from generic servlet
    2(a)generic servlet overrides service()
    2(b)http is overrides anyone of 7 methods that are
    doGet(),doPost(),doPut(),delete(),init(),Destroy(),modify()
    3(a)session tracking possible from http only
    4(a)http is protocol dependent i.e only supports http
    4(b)generic is protocol independent

  25. what is the use of Generic servlet class even though we have servlet interface

    • if you develop your servlet useing servlet interface we must override all 5 methods in our servlet if these necessary are not.

      if you develop your servlet useing Genaric servlet we override only one sevice().

  26. That Cool…But needs some examples to explain that will be Good….

  27. How To achive(I know session cant be used in Generic ) session in Generic servlet by using ftp protocal

  28. How To achive(I know session cant be used in Generic ) session in Generic servlet by using ftp protocal

  29. when GenericServlet implements Serializable, then what is the need to re-implement it in HttpServlet class?

  30. hello
    please tell me

    when we send the request to server for servlet then is it http request or https request..?

  31. please tell me the complete flow of http and htts ….

  32. please tell me the complete flow of http and htts ….and etc

  33. ya this site is really good this site containing very useful information

  34. Your site is very good, it describe real language of servlet.

  35. So as per previous discussion,we can write a program with out having service() method,with using HTTP.can anyone explain clearly please

  36. Thanks, Through this informations,
    students taking these informations &
    it is easy to write the Diff. bet.
    HttpSevlet & GenericServlet

    Thanks.

  37. sir.it’s very useful.sir i have a problem that i am unable to insert data into the database by using jsp.can u help me out.i am using oracle.exe graphical mode for data base.can u help me out

  38. http request is stateless when u sending request to server for servlet and server generate that response send back to the client then it break the connection btw client and server . ..

    the differnce is only only one S https what it does it secure that website means when eva u filling the form and entering the credit card # so plz first u look at the browser tht is there https if yes then it means its secure website
    thanks

  39. i got lots of information…really very good site.

  40. why HttpServlet is declared as abstract?

  41. There is no abstract methods in httpservlet then y it is abstract?

  42. please upload that httpservlet class definition

  43. Hi,
    there are two methods in httpservlet..
    1.public void service(ServletRequest,Servlet Response)
    another is
    2.protected void service(HTTPServletRequest,
    HttpServletResponse)

    why do we require two methods and if there is a service method in HTTPSERVLET why we need of doxxx() methods.please let me know

  44. why http servlet handle only http protocals?

    why genaric servlet handle any type of protocals?

    but differentiated of inheritances concept why?

  45. hii guys,
    why all user defined servlet classes extends HttpServlet? if anyone knows kindly reply?

  46. thanks this type of information. we can know that diff. between generic servlet and http servlet…

  47. difference between protocol independent and protocol dependent

  48. i am satisfied with your comment.
    thank you.

  49. sir,
    really it is very easy to understand thankyou sir…

  50. Yes GenericServlet does support HTTP protocol.
    HttpServlet is protocol specific.

  51. —> GenericServlet can process multiple clients request from a single form. Whereas HttpServlet can process multiple clients requesting from multiple HTML forms.
    –> GenericServlet is Stateless and HttpServlet is Stateful.

    • Can you please check your last sentence ?
      GenericServlet is Stateless and HttpServlet is Stateful

      I think it should be Generic Servlet is stateful and http servlet is stateless.

      Correct me if i am wrong?

      looking forward for your reply ASAP

  52. Q. 1. – Can Generic servlet handle HTTP request also ?

    Q 2 . – Is Generic Servlet stateful if getting the non HTTP request ?

  53. Hi Joe,

    I have been using javapapers for long time. Today, for a reference to Servlet/JSP related stuffs I came here. But with the new navigation/tree, I couldnt find out those topics anywhere in any of the folders here. At last I reached here by trying the topic with ‘javapapers’ on google. Could you please add all those missing ones from the old list(may be u have excluded it purposefully ) as well in tree of category so that we can ensure that we would not miss anything important like this :)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>