Servlet Life Cycle

Google Advertisements

Prev Tutorial Next Tutorial

Life Cycle of Servlet

The web container maintains the life cycle of a Servlet instance or object.

Life cycle of Servlet

  1. Loading (Servlet class is loaded)
  2. Installation (Servlet instance is created)
  3. Initialization (init method is invoked)
  4. Service providing (service method is invoked)
  5. Destroying (destroy method is invoked)
servlet life cycle

As displayed in the above diagram, there are three states of a Servlet: new, ready and end. The Servlet is in new state if Servlet instance is created. After invoking the init() method, Servlet comes in the ready state. In the ready state, Servlet performs all the tasks. When the web container invokes the destroy() method, it shifts to the end state.

1. Servlet class is loaded

The classloader is responsible to load the Servlet class. The Servlet class is loaded when the first request for the Servlet is received by the web container.

2. Servlet instance is created

The web container creates the instance of a Servlet after loading the Servlet class. The Servlet instance is created only once in the Servlet life cycle.

3. init method is invoked

The web container calls the init method only once after creating the Servlet instance. The init method is used to initialize the Servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given below:


public void init(ServletConfig config) throws ServletException 

4. service method is invoked

The web container calls the service method each time when request for the Servlet is received. If Servlet is not initialized, it follows the first three steps as described above then calls the service method. If Servlet is initialized, it calls the service method. Notice that Servlet is initialized only once. The syntax of the service method of the Servlet interface is given below:


public void service(ServletRequest request, ServletResponse response)   
  throws ServletException, IOException  

5. destroy method is invoked

The web container calls the destroy method before removing the Servlet instance from the service. It gives the Servlet an opportunity to clean up any resource for example memory, thread etc. The syntax of the destroy method of the Servlet interface is given below:


public void destroy()  

Servler Life Cycle Example

import javax.servlet.*;

public class myservlet extends GenericServlet
 public void init(ServletConfig sc)
  System.out.println("init executed...");
public void service(ServletRequest req, ServletResponse resp)throws IOException, ServletException 
System.out.println("service executed...");
PrintWriter out=resp.getWriter();
out.println("plz observe output on server console window");
public void destroy()
 System.out.println("Distroy executed...");



Prev Tutorial Next Tutorial

Google Advertisements

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements

Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: or 8076671483 Try this Keyword C++ Programs