Servlet Life Cycle

The Life Cycle of Servlet having some good characteristics that allows servlet to address both performance and resource problems of CGI along-with security pertains of low-level Server API programming. The life cycle of a servlet is operated by the container in which the servlet has been deployed. Servlet life cycle is categorized into 4 parts i.e. Instantiation, Initialization, Servicing the Request/Implementation & Destruction.

Servlet Life Cycle


The Life Cycle of Servlet having some good characterstics that allows servlet to address both performance and resource problems of CGI alongwith security pertains of low-level Server API progamming. The life cycle of a servlet is operated by the container in which the servlet has been deployed.

Servlet life cycle is categorized into 4 parts:

  1. Instantiation
  2. Initialization
  3. Servicing the Request/Implementation
  4. Destruction

Servlets are normal Java classes, which are created when they are needed and are destroyed when they are not needed. Servlets run within a Servlet Container that creates and destroys Servlets.

Instantiation:

Servlet Container loads the servlet from web.xml. Loading of the Servlet is done either during startup or when the first request is made. If the attribute <load-on-startup> of web.xml file has a positive value then the servlet is loaded otherwise it load when the first request comes for service. After loading of the servlet, the container creates the instances of the servlet by using Class.forName(ServletName).newInstance().

Initialization:

After instantiation, init() method is called by Servlet Container. It must be called before the servlet can service any request. The init() method is called only once.

The servlet will be available for service if it is loaded successfully otherwise the servlet container unloads the servlet.

Implementation/Servicing the Request:

After Initialization, Servlet Container sends any request to the service() method. Servlet creates separate threads for each request. doGet(), doPost(), doDelete(), doOptions(), doPut() and doTrace() are some of the service() method that are called by Servlets for handling the request and sending response

Destruction:

Servlet Container calls the destroy() method when the Servlet is no longer needed for servicing any request. destroy() method is also called only once. Once the destroy() method is called Servlet Container does not sends any request for service.

Java Virtual Machine (JVM) then performs garbage collection on the memory resources associated with the servlet.

Example of the Servlet having all parts of its lifecycle:

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

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 static final long serialVersionUID = 1L;
	public void init(ServletConfig config) throws ServletException
    {
    	System.out.println("init() method is called ");
    }
   	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   		response.setContentType("text/plain");
   	    PrintWriter out = response.getWriter();   	    
   	    out.println("service() method called");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	public void destroy()
	{
		System.out.println("Servlet is destroying....");
	}

}

Servlet mapping for the above example

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servletLifeCycleExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file> 
</welcome-file-list>
<servlet>
<servlet-name>ServletLifeCycleExample</servlet-name>
<servlet-class>roseindia.net.ServletLifeCycleExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletLifeCycleExample</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</web-app>