Life cycle of Servlet
This article is discussing about the Life cycle of Servlet and teaches you the Servlet Life cycle methods. As a beginner you should understand the different lifecycle methods of the Servlet. You use these methods to for the Servlet initialization and the cleaning the resource at the end of Servlet lifecycle.
You can also view the example of Servlet Life Cycle which is explained with the help of code.
There are four phases in the life cycle of Servlet. The lifecycle phases are Loading and Instantiation, Initialization, Servicing the Request and Destroying the Servlet.
Following steps are performed by the container when it receives the request from client:
- First of all Web container try to finds the Servlet instance
and if it does not exists Server loads the Servlet. Then creates
the instance of the Servlet class. Then it initializes the
Servlet instance by calling the init method. Finally it calls
the service methods.
- If Servlet instance exists then it calls the service methods.
The life cycle of a Servlet can be categorized into four parts:
- Loading and Instantiation: 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.
Above picture shows the Life Cycle of a Servlet