Servlet Life Cycle
Life Cycle of Servlet
The web container maintains the life cycle of a Servlet instance or object.
Life cycle of Servlet
- Loading (Servlet class is loaded)
- Installation (Servlet instance is created)
- Initialization (init method is invoked)
- Service providing (service method is invoked)
- Destroying (destroy method is invoked)
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>