Life cycle of Servlet Posted on: July 13, 2007 By Deepak Kumar
In this section, you will learn about the life cycle of servlets.
Life cycle of Servlet
The life cycle of a servlet can be categorized into
four parts:
Loading and Inatantiation: The servlet
container loads the servlet during startup or when the first request is
made. The loading of the servlet depends on the attribute
<load-on-startup> of web.xml file. If the attribute
<load-on-startup> has a positive value then the servlet is load with
loading of the container otherwise it load when the first request comes for
service. After loading of the servlet, the container creates the instances
of the servlet.
Initialization: After creating the instances,
the servlet container calls the init() method and passes the servlet
initialization parameters to the init() method. The init() must be called by
the servlet container before the servlet can service any request. The
initialization parameters persist untill the servlet is destroyed. The
init() method is called only once throughout the life cycle of the servlet.
The servlet will be available for service if it is loaded successfully
otherwise the servlet container unloads the servlet.
Servicing the Request: After successfully
completing the initialization process, the servlet will be available for
service. Servlet creates seperate threads for each request. The sevlet
container calls the service() method for servicing any request. The
service() method determines the kind of request and calls the appropriate
method (doGet() or doPost()) for handling the request and sends response to
the client using the methods of the response object.
Destroying the Servlet: If the servlet is no
longer needed for servicing any request, the servlet container calls the
destroy() method . Like the init() method this method is also called only
once throughout the life cycle of the servlet. Calling the destroy() method
indicates to the servlet container not to sent the any request for service
and the servlet releases all the resources associated with it. Java
Virtual Machine claims for the memory associated with the resources for
garbage collection.
Paul
August 12, 2011
Thanks
helpful topic. Before working with servlet, each of us should know it precisely. Otherwise various unwanted circumstance may occur.
Rinku
February 20, 2012
about sevlets
no comment
kalai
October 14, 2011
good
useful to us
thi
January 19, 2012
reply
short and qute
satish
February 18, 2012
satish
hi this is satish .thanks for the article .
satish
February 18, 2012
nice tutorial
can you please explain the difference between context and container
sravan
March 10, 2012
servlet
when we send a request then it creates an object that is requests & response objects
plz tell me i am in confusion
sharath babu
April 27, 2012
life cycle of servlet
please explain the concepts of init and destroy in detail
ramya
June 5, 2012
feedback
this metirial is very good and easy to understand.thank u for providing this type of metirial.
kulvant singh
August 27, 2012
Ask question
why are servlet's service method abstract
sagar thoriya
September 23, 2012
servlet
good explain..
anynomous
October 18, 2012
anynomous
hi
shruthi
December 31, 2012
Nice Explanation
Hi there,
Really ur explanation was very nice:) and easily understable.
Thanks and regards,
Shruthi
jay raghav
April 13, 2013
interface creation
sir,
give me a small code of java interface how i will i crete a intrface in java and give me short note on java how it will work in compile time
also tell me about java i/o
thnks
Ask Questions? Discuss: Life cycle of Servlet View All Comments
Post your Comment