1. What is Servlet Life Cycle?
Definition: Different states in which a Servlet exists between its object creation and object garbage collection is known as life cycle of Servlet.
2. Who is responsible to maintain the life cycle? Is it the Developer? Everything goes smooth with Servlets without much Developer's intervention. Servlet Container or Servlet Engine is responsible for:
3. How to get a Servlet container?
Just load a Web server like Tomcat or Weblogic or WebSphere. These Web servers get you the Servlet container.
4. Any methods exist in Servlet Life Cycle like init(), start() and paint() as in the case of Applet life cycle? Yes. The life cycle comprises of 3 methods and all are defied in javax.servlet.Servlet interface and overridden by HttpServlet. The methods are
In it's life cycle, the Servlet should exist in one of these three methods. These methods are callback methods called automatically by Servlet container. Following gives the explanation on these methods elaborately. 1. The init() method
When the client request a Servlet (by giving alias name declared in web.xml), the container loads the Servlet into the RAM. To start execution, it creates the Servlet object and calls init() method. At this stage, the container creates another object of ServletConfig interface representing the Servlet and passes it to the parameter of init(). For this reason, the parameter of init() method is an object of ServletConfig. For every Servlet under execution in the container, there exists a corresponding ServletConfig object.
Let us what Java API says about this method
This method is called and executed only once in the life cycle of Servlet (like init() of Applet).
2. The service() method
After completing the execution of init(), the container calls service() method to process the client request. It is the heart of the Servlet like run() method for a thread. All the code the Servlet should execute to honour the client request, should be written in this method. Receiving request with client data, generating response and sending to client - all this happens here only.
Following is the method signature as defined in javax.servlet.Servlet interface
At this point, let us go a little deeper. When the Servlet is in the RAM honouring the client request, if multiple clients request the same Servlet, then how many objects of Servlet are created? Servlets support multithreading (here, where Servlet differs from CGI/PERL). For all the clients, only one Servlet object is created (for this reason, init() is called only once) and for each client a separate service() method is created and called. That is, each client is treated as a separate thread (spawns a new thread) and each thread launches a separate service() method.
Note: Incase of HttpServlet (not GenericServlet), the service() method can be replaced by more appropriate doGet() or doPost() methods.
3. The destroy() method
When the service() method execution is over (the execution is over means, when the client request is fully honoured by sending all the required response), the Servlet process is terminated with the calling of destroy() method. In the destroy() method, the Servlet object is garbage collected by JVM and is the end of the life cycle.
Following is the method signature
The container does any clean up action and frees any resources held by the Servlet like memory, file handles and threads etc.