Servlet Life Cycle
The life cycle of a Java Servlet is determined by three of its methods: init(), service(), and destroy(). The life cycle starts when Servlet container instantiates the object of Servlet class and calls the init() method, and process the request using service() method and ends with the web container calling the destroy() method. The different phases of the Servlet life cycle are described below with figure:
Figure 1: Servlet Life Cycle
(1) Loading, instantiation and initialization:
When we start up a Servlet container, it looks for a set of configuration files, also called the deployment descriptors that describe all the web applications. Each web application has its own deployment descriptor file, called web.xml, which includes an entry for each of the Servlets it uses. The Servlet container first loads the corresponding Servlet class and creates an instance of this class using the method Class.forName(className).newInstance(). Then the Servlet container creates the ServletConfig object, which contains the configuration values stated in the web.xml file for this application and uses it to pass the Servlet initialization parameters to the init() method. The initialization parameters persist until the Servlet is destroyed and are applied to all invocations of that servlet. The Servlet container calls this init() method exactly once.
If the initialization is successful, the Servlet is available for service. If the initialization fails, the Servlet container unloads the Servlet. In such cases, the web application and the Servlet remain unavailable until the administrator changes them to be available.
The signature of init() method is shown below:
public void init (ServletConfig config) throws ServletException
This method also can throw a ServletException. The servlet container cannot place the Servlet into service if the init method throws a ServletException or the method does not return within a time period defined by the web server.
(2) Processing requests:
After the Servlet instance is properly initialized, it is ready to service client requests. When the servlet container receives requests for this Servlet, it will dispatch them to the Servlet instance by calling the Servlet.service (ServletRequest, ServletResponse) method. The Servlet container passes a ServletRequest object and the ServletResponse object. The ServletRequest object contains the client’s request and the ServletResponse contains the Servlet’s response. For HttpServlet, the service() method is basically replaced by doGet() or doPost().
The signature of service() method is shown below:
public void service (ServletRequest request, ServletResponse response) throws ServletException,IOException
The service() method throws a ServletException if an exception occurs that interferes with the Servlet’s normal operation. The service() method also can throw an IOException if an input or output exception occurs during the execution of this method.
(3) Unloading:
The Servlet container unloads a Servlet class by invoking the Servlet’s destroy() method. Typically, this method is invoked when the Servlet container is stopping a web application which contains the Servlet. The method runs only once during the lifetime of the Servlet and signals the end of the Servlet. After a Servlet’s destroy() method is invoked, the Servlet container unloads the Servlet, and the JVM eventually performs garbage collection on the memory resources associated with the Servlet.
The signature of destroy() method is shown below:
public void destroy()
There are also two auxiliary methods whose short descriptions are given below:-
- public String getServletInfo(): Returns a ServletConfig object, which contains any initialization parameters and startup configuration for this Servlet.
- public ServletConfig getServletConfig(): Returns a string containing information about the Servlet, such as its author, version, and copyright.
Preloading and Lazy loading:
Usually, a Servlet container does not initialize the Servlets as soon as it starts up. It initializes a Servlet when it receives a request for that Servlet for the first time. This is called lazy loading. Although this process greatly improves the startup time of the Servlet container, it has a drawback. If the Servlet performs many tasks at the time of initialization, such as caching static data from a database on initialization, the client that sends the first request will have a poor response time. In many cases, this is unacceptable.
The servlet specification defines the element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. This process of loading a servlet before any request comes in is called preloading, or pre-initializing, a Servlet.