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

    j2ee question available in site find out please

  2. robin

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

  3. Anonymous

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

  4. one & only Hiren Modi

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

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

    Your site is superb ..complete knowledge provider.

  8. B.Sreenivasarao

    What is the Basic difference between Servlet and Jsp.?

    • Anonymous

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

  9. Satish

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

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

  11. Sushit Kumar

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

    Keep it up…….

  12. Chetan Pagar

    Hello,

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

    Regards
    Chetan Pagar

  13. PAWAN DWIVEDI

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

  14. sv

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

    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

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

    all questions are not truely……..

  18. vickky

    thanks buddy thats really helpfull 4 me

  19. good article
    its is a very important interview question in servlets

  20. Vikas

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

  21. Nagraj

    Thanks for dis also

  22. Sachin

    This site is really great and authentic too…..

  23. mahesh

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

  24. pankaj

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

  25. Joe

    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.

  26. Apurv

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

  27. sridhar

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

  28. laxminarayana

    bad

  29. chandara

    I am not satiafied

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

  31. neh

    nice one with good explanation!

  32. swopneel

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

    • kiran

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

  33. sunil

    its nice…….

  34. Kavin

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

  35. AAR

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

  36. AAR

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

  37. Mallikarjun

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

  38. Anonymous

    hello
    please tell me

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

  39. Anonymous

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

  40. Anonymous

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

  41. pranavi

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

  42. rahul

    great work joe..keep it up man

  43. Sonu Singh

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

  44. Rafi

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

  45. Bhakti

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

    Thanks.

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

  47. lavanya

    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

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

  49. Anonymous

    i got lots of information…really very good site.

  50. Navin Prakash

    good for knowledge……

  51. ashok

    thnq bro..

  52. DPReddy

    why HttpServlet is declared as abstract?

  53. DPReddy

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

  54. Anonymous

    please upload that httpservlet class definition

  55. Rafi

    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

  56. Anonymous

    why http servlet handle only http protocals?

    why genaric servlet handle any type of protocals?

    but differentiated of inheritances concept why?

  57. Anonymous

    give some more pointss

  58. anbu

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

  59. Anonymous

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

  60. srinivas

    difference between protocol independent and protocol dependent

  61. shital bhayani

    i am satisfied with your comment.
    thank you.

  62. Anonymous

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

  63. nn

    is genericServlet suppport http protocol

  64. anil

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

  65. sai

    awsome your site is suprb matter also gd..

  66. Rajender

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

      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

  67. Anonymous

    thanks joe good one

  68. pankaj jain

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

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

  69. Rajeesh

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

Your Comment