Servlet Life Cycle | Life Cycle of Servlet


The life cycle of a servlet can be categorized into four parts:
  1. Loading and Insatantiation: 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.
  2. 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.
  3. 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.
  4. 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.

Understanding the Servlet life cycle with an example

We will create a Servlet that will help you in better understanding the life cycle of a servlet.
import java.io.IOException;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ServletLifeCycleExample extends HttpServlet {
 
 private int count;
 
 @Override
 public void init(ServletConfig config) throws ServletException {
  super.init(config);
  getServletContext().log("init() called");
  count=0;
 }
 
 @Override
 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  getServletContext().log("service() called");
  count++;
  response.getWriter().write("Incrementig the count: Count = "+count);
 
 }
 
 @Override
 public void destroy() {
  getServletContext().log("destroy() called");
 } 
 
}
Above ServletLifeCycleExample Servlet extends HttpServlet and overrides init() Service() and destroy() methods. The servlet logs a message into server log file when servlet is initialized and sets counter to 0. Every time the service method is called, it increments the counter by 1 and displays the current value of counter to user. finally when destroy method is called, it logs a message to server log file.
Compile above class and put it into WEB-INF/classes directory. Define the servlet and servlet mapping into web.xml file. If you do not know how to define the servlet in web.xml see this servlet example.
Deploy the application and start the server. Call the servlet by opening the URL that you specified as url-pattern in web.xml. See the server log file. Hit the URL multiple times and you will see that a message is logged every time the service() method is called. The current value of counter will be displayed in browser.

No comments:

Post a Comment