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.

«

»

Comments on "Difference between HttpServlet and GenericServlet"

  1. Rajeesh says:

    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 :)

  2. upendra says:

    u r right saying bro
    I am agree with u

  3. Anonymous says:

    difference between inner class and outer class

  4. pankaj jain says:

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

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

    • manoranjan mishra says:

      Yes genericservlet can handle the http protol because it is protocl independent

      It can also handle smtp, ftp protol also

  5. Anonymous says:

    thanks joe good one

  6. gopi says:

    ya u r given good explanation man.

  7. Rajender says:

    —> 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.

    • Vjosh says:

      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

  8. sai says:

    awsome your site is suprb matter also gd..

  9. gowtam says:

    what is given in above article is correct.what you have posted is wrong

  10. anil says:

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

  11. nn says:

    is genericServlet suppport http protocol

  12. Anonymous says:

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

  13. shital bhayani says:

    i am satisfied with your comment.
    thank you.

  14. srinivas says:

    difference between protocol independent and protocol dependent

  15. Anonymous says:

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

  16. anbu says:

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

  17. Anonymous says:

    give some more pointss

  18. Anonymous says:

    why http servlet handle only http protocals?

    why genaric servlet handle any type of protocals?

    but differentiated of inheritances concept why?

  19. Rafi says:

    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

  20. Anonymous says:

    please upload that httpservlet class definition

  21. DPReddy says:

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

  22. DPReddy says:

    why HttpServlet is declared as abstract?

  23. ashok says:

    thnq bro..

  24. Navin Prakash says:

    good for knowledge……

  25. Anonymous says:

    i got lots of information…really very good site.

  26. riaz Molwani says:

    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

  27. lavanya says:

    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

  28. ankita says:

    thank foe information that u had given in this blog….

  29. Bhakti says:

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

    Thanks.

  30. Rafi says:

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

  31. Sonu Singh says:

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

  32. rahul says:

    great work joe..keep it up man

  33. pranavi says:

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

  34. Anonymous says:

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

  35. Anonymous says:

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

  36. Anonymous says:

    hello
    please tell me

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

  37. Mallikarjun says:

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

  38. AAR says:

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

  39. AAR says:

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

  40. Kavin says:

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

  41. sunil says:

    its nice…….

  42. swopneel says:

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

    • kiran says:

      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().

  43. neh says:

    nice one with good explanation!

  44. nagu says:

    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

  45. chandara says:

    I am not satiafied

  46. laxminarayana says:

    bad

  47. sridhar says:

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

  48. danie says:

    Nice one:-)

  49. Apurv says:

    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()

  50. Joe says:

    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.

  51. pankaj says:

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

  52. mahesh says:

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

  53. Sachin says:

    This site is really great and authentic too…..

  54. Nagraj says:

    Thanks for dis also

  55. Vikas says:

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

  56. captainportal says:

    good article
    its is a very important interview question in servlets

  57. vickky says:

    thanks buddy thats really helpfull 4 me

  58. Anonymous says:

    all questions are not truely……..

  59. tuturanjan sahoo,ABA,Balasore,Odisha says:

    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.

  60. tuturanjan sahoo says:

    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

  61. sv says:

    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 .

  62. PAWAN DWIVEDI says:

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

  63. Chetan Pagar says:

    Hello,

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

    Regards
    Chetan Pagar

  64. ronak says:

    good

  65. Sushit Kumar says:

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

    Keep it up…….

  66. Rahul says:

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

  67. Satish says:

    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.

  68. B.Sreenivasarao says:

    What is the Basic difference between Servlet and Jsp.?

    • Anonymous says:

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

  69. Sheetal says:

    Your site is superb ..complete knowledge provider.

  70. SREEDHAR says:

    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..,

  71. Pawan says:

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

  72. Anonymous says:

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

  73. robin says:

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

  74. parmar shaktisinh says:

    j2ee question available in site find out please

Your Comment