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


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