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()
-
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. -
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.
-
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 amcomprehensive info on servlet life cycle, thanks.
Gonzal on March 15th, 2010 5:10 pmgood yar but pls drw the figure of srvlet life cycle…….
thnx….
pavan on November 18th, 2010 4:23 pmnice explaination..
vema on April 11th, 2011 12:47 pmThis can be more elaborated..
Sachin on April 13th, 2011 4:44 pmnice explianation
nikhila on April 24th, 2011 2:42 pmnice yaar
arunachal on May 1st, 2011 5:22 pmVery 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 pmHi 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 pmplease provide the easy explanation of jdbc driver it always mix up in my mind!
Anonymous on November 23rd, 2011 5:11 pmsuper da ….
shereef on January 19th, 2012 2:26 pmgood da…machi
vasu on February 1st, 2012 10:09 amdetails about servlet life cycle got through scwcd book
rajesh on February 15th, 2012 3:38 pmYour 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 pmits very help full.
RAJA SEKHAR on April 14th, 2012 11:54 amif possible try videos …to easy understanding purpose
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 pmi became a fan of this site……great work joe, i am learining many new things.i love java
pavan_krishna on May 2nd, 2012 6:35 pmcan u please post on hibernate and struts…it will be more help full
Excelent Joe…….
krishnamurthy on May 13th, 2012 12:12 pm