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()
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.
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() 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 are closed for "Servlet Life Cycle".
comprehensive info on servlet life cycle, thanks.
good yar but pls drw the figure of srvlet life cycle…….
thnx….
nice explaination..
This can be more elaborated..
nice explianation
nice yaar
Very nice explanation !
It would be helpful if you publish for struts and hibernate
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.
please provide the easy explanation of jdbc driver it always mix up in my mind!
super da ….
good da…machi
details about servlet life cycle got through scwcd book
Your j2ee blogs are not as good as Java blogs. I am keen to see some topics related spring/hibernate
its very help full.
if 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.
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
Excelent Joe…….
Simple and short but really helpful.
is good but explain load_on_startup clearly and also explain getconfig()and getinfo().
this is very nice explanation but are you add the servlet life cycle diagram
thanks….
simply SUPERB …please post on hibernates and struts
very good explanation
nice…..!
it can be elaborated even more with nice figures..
superb explain man..
always,nice explanation on any topic..n liked the way u designed this blog..well done joe
thanx..
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
hey this is ash
its very nice yaar
sir , i like ur explanation. thank u so much
sooooo nice.becoz of this servlet notes i learned about servlet lifecycle.very awesome explanation.once again thanking you.
awesome explanation
your explanation is very good ,but put diagram whenever is required.
its good bt diagram is required
Give some examples
thank you sir.it is good.please give some pragrams related
thank you sir.it is good.please give some programs related
Very good explanations
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);
nice explanation . :)
could you please elobarate destroy() method some more….
thank you
elaborate more
Hi Joe,
Great post, was really helpful Thanks for Sharing !!
diagrams are required. explaination is nice.
Plg draw the structure of life cycle
Brief and nice explanation for a simple but tricky Topic.
Thanks a lot for this blog :)
nice bt draw diagram for life cycle
nice explanation
Thanks Karthi.