Servlet Life Cycle

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:

Syntax

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:

Syntax

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:

Syntax

public void destroy()  

Servler Life Cycle Example

import javax.servlet.*;
import java.io.*;

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();
resp.setContentType("text/html");
out.println("plz observe output on server console window");
}
public void destroy()
{
 System.out.println("Distroy executed...");
}
}

web.xml

<web-app>
<servlet>
<servlet-name>srv</servlet-name>
<servlet-class>myservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>srv</servlet-name>
<url-pattern>/ms</url-pattern>
</servlet-mapping>
</web-app>

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

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

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements