Servlet Life Cycle

05/05/2008

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.

good yaar…

Ammu on December 29th, 2009 9:09 am

comprehensive info on servlet life cycle, thanks.

Gonzal on March 15th, 2010 5:10 pm

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

thnx….

pavan on November 18th, 2010 4:23 pm

nice explaination..

vema on April 11th, 2011 12:47 pm

This can be more elaborated..

Sachin on April 13th, 2011 4:44 pm

nice explianation

nikhila on April 24th, 2011 2:42 pm

nice yaar

arunachal on May 1st, 2011 5:22 pm

Very nice explanation !

It would be helpful if you publish for struts and hibernate

Aruna on August 3rd, 2011 3:40 pm

[...] a look at this java servlet interview question: Servlet Life Cycle – Explain, it might help you to understand better. Share this:TwitterFacebookLike this:LikeBe [...]

What happens if you call &hellip on August 14th, 2011 7:23 pm

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.

Karthikeyan K on November 17th, 2011 12:52 pm

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

Anonymous on November 23rd, 2011 5:11 pm

super da ….

shereef on January 19th, 2012 2:26 pm

good da…machi

vasu on February 1st, 2012 10:09 am

details about servlet life cycle got through scwcd book

rajesh on February 15th, 2012 3:38 pm

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

Naresh on February 16th, 2012 11:09 pm

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

RAJA SEKHAR on April 14th, 2012 11:54 am

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.

suresh on April 21st, 2012 9:27 pm

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

pavan_krishna on May 2nd, 2012 6:35 pm

Excelent Joe…….

krishnamurthy on May 13th, 2012 12:12 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home