Servlet Life Cycle

Last modified on July 25th, 2014 by Joe.

The interface javax.servlet.Servlet defines the following three methods known as servlet life cycle methods.
 public void init(ServletConfig config) throws ServletException
 public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException
 public void destroy()

  1. Creation and initialization

    The container first creates the servlet instance and then executes the init() method.
     init() can be called only once in its life cycle by the following ways:
    a) Through the ‘load-on-startup’ tag using the web.xml. This makes the servlet to be loaded and initialized when the server starts.
    b) For the first time only in its life cycle, just before the service() is invoked.
    c) Server administrator can request for the initialization of a servlet directly.

  2. Execution of service

    Whenever a client requests for the servlet, everytime the service() method is invoked during its life cycle. From service() then it is branched to the doGet() or doXx..() methods for a HttpServlet. The service() method should contain the code that serves the Servlet purpose.

  3. Destroy the servlet

    destroy() method is invoked first, then Servlet is removed from the container and then eventually garbage collected. destroy() method generally contains code to free any resources like jdbc connection that will not be garbage collected.

Comments on "Servlet Life Cycle"

  1. Gonzal says:

    comprehensive info on servlet life cycle, thanks.

  2. pavan says:

    good yar but pls drw the figure of srvlet life cycle…….

    thnx….

  3. vema says:

    nice explaination..

  4. Sachin says:

    This can be more elaborated..

  5. nikhila says:

    nice explianation

  6. arunachal says:

    nice yaar

  7. Aruna says:

    Very nice explanation !

    It would be helpful if you publish for struts and hibernate

  8. Karthikeyan K says:

    Hi Joe

    This is Karthikeyan. It is very nice to see about Servlet Lifecycle and Now i am very much confident about Servlet Lifecycle and I am keen interested to know something about Connection Pooling in java. Please update it in your site if it is possible .so that everybody can come know that.

  9. Anonymous says:

    please provide the easy explanation of jdbc driver it always mix up in my mind!

  10. shereef says:

    super da ….

  11. vasu says:

    good da…machi

  12. rajesh says:

    details about servlet life cycle got through scwcd book

  13. Naresh says:

    Your j2ee blogs are not as good as Java blogs. I am keen to see some topics related spring/hibernate

  14. RAJA SEKHAR says:

    its very help full.
    if possible try videos …to easy understanding purpose

  15. suresh says:

    It is good for understanding up to some level about servlet instantiation and initialization
    but need more explanation about ServletContext , ServletConfig , Request and Response objects creation.

    Thanking for that eplanation.

  16. pavan_krishna says:

    i became a fan of this site……great work joe, i am learining many new things.i love java
    can u please post on hibernate and struts…it will be more help full

  17. krishnamurthy says:

    Excelent Joe…….

  18. Nitin says:

    Simple and short but really helpful.

  19. chandu says:

    is good but explain load_on_startup clearly and also explain getconfig()and getinfo().

  20. irshad says:

    this is very nice explanation but are you add the servlet life cycle diagram
    thanks….

  21. Rohit says:

    simply SUPERB …please post on hibernates and struts

  22. Ratna Kanth says:

    very good explanation

  23. madhumati says:

    nice…..!
    it can be elaborated even more with nice figures..

  24. sagar says:

    superb explain man..

  25. kanchana says:

    always,nice explanation on any topic..n liked the way u designed this blog..well done joe

  26. arvind kumar says:

    thanx..

  27. Sandhya says:

    Hi Joe,

    I have a question….. If i login into any web application and after that i bookmarks that url, later if i use the bookmarks url then it directly takes me too login page not to which i give bookmarks url. Please would u explain how it works…
    I mean its going becoz of session timeout or anything else….

    Thanks in Advance
    Sandhya

  28. Anonymous says:

    hey this is ash
    its very nice yaar

  29. sims says:

    sir , i like ur explanation. thank u so much

  30. Harichandana says:

    sooooo nice.becoz of this servlet notes i learned about servlet lifecycle.very awesome explanation.once again thanking you.

  31. Harichandana says:

    awesome explanation

  32. Anonymous says:

    your explanation is very good ,but put diagram whenever is required.

  33. minal says:

    its good bt diagram is required

  34. ramya says:

    Give some examples

  35. Anonymous says:

    thank you sir.it is good.please give some pragrams related

  36. vijaybhupathi says:

    thank you sir.it is good.please give some programs related

  37. Kirti sharma says:

    Very good explanations

  38. KRR!$H says:

    i have some problems?????

    1) for initialization which method will call init() or init(ServletConfig) ??

    2) which service method service(ServletRequest,ServletResponse) or service(HttpServletRequest,HttpServleytResponse);

  39. MAHARSHI RAVAL says:

    nice explanation . :)

  40. Anonymous says:

    could you please elobarate destroy() method some more….

    thank you

  41. Anonymous says:

    elaborate more

  42. Srikanth says:

    Hi Joe,
    Great post, was really helpful Thanks for Sharing !!

  43. vanu says:

    diagrams are required. explaination is nice.

  44. gautam->tara says:

    Plg draw the structure of life cycle

  45. Rajeev says:

    Brief and nice explanation for a simple but tricky Topic.
    Thanks a lot for this blog :)

  46. prem says:

    nice bt draw diagram for life cycle

  47. karthi says:

    nice explanation

  48. Joe says:

    Thanks Karthi.

Comments are closed for "Servlet Life Cycle".