The Life Cycle of a Servlet
The life cycle of a Servlet is managed by a component of the server (where the Servlet has been deployed) called the Servlet Container. The life cycle of any Servlet is managed by three methods - init(), service(), and destroy() in this order only. When the Web Server maps a request URI to a Servlet, then the Servlet Container does the following activities:-
First the Servlet Container checks if the Servlet being requested is already instantiated or not. Most of the Application Servers (or Sevlet Containers like Tomcat... though newer releases of Tomcat are at times termed as Application Server as well. We'll discuss the differences between A Servlet Container and an Application Server in some other article. For the time being we can safely assume a J2EE compliant Application Server as something which has many components including one component being a typical Servlet Container) use the Deployment Descriptor of the deployed Web Application to instantiate all the Servlets mentioned there. In Model 2 architecture or in the MVC architecture, Servlets are treated as Controllers and hence they are normally supposed to be a part of the environment for the underlying Web Application and hence they are loaded and initialized at the time of application start up itself.
init() method - If the Servlet is being initialized at the first time (either on request or during the server start up), the Servlet Container calls the init() method of the Servlet to initialize it. We can override this method to put the code responsible for one-time initialization activities of that Servlet as this method is called only once during the entire life cycle of the Servlet. It's an abstract method of the Servlet interface and a default implementation is given by the GenericServlet class. The init() method should throw an UnavailableException if a fatal error occurs during its initialization.
service() method - Once, the Servlet Container is done with the initialization of the Servlet then it calls the service() method of it passing the incoming HttpRequest and a an empty HttpResponse object as the two patameters of the service method. This method is responsible for serving the request by understanding the request, processing it, building an appropriate response and finally sending that request to the corresponding client. The service() method is normally executed in a new spawned thread so that the Servlet Container can keep utilizing the same Servlet for multiple incoming requests concurrently. You can implement SIngle Thread Model for a Servlet or a JSP, but that's rarely used. We'll discuss that in some other article :-)
destroy() method - this method is called before the Servlet Container removes the Servlet instance from the memory while unloading the servlet. You can override this method to have the code responsible for all the clean-up activities required for this Servlet, such as resource-release, updating logs, saving info on persistent medium, etc. This method is automatically called for all the Servlets loaded into the memory of the Servlet Container once the Application Server is shut down as the server unloads all the loaded servlets first before it shuts down. This method is called only if all the threads executing service() method of this Servlet for serving client requests have already been terminated. Otherwise, all those threads are notified about the Shutdown of the Servlet and the server either forces them to terminate or wait for them to complete. The bottom line is that all service threads should be terminated before the destroy() method can be called.
Monday, June 9, 2008
The Life Cycle of a Servlet