Thursday 29 August 2013

Servlet Life Cycle

Servlet Life Cycle

The servlet lifecycle under goes four steps: Last 4th step will not happen immediately after the 3rd step.
the servlet is destroyed after the completion of the request .

1. loading a servlet
2. initializing the servlet
3.. request handling
4. Destroying the servlet


fig: 1 shows

Loading a Servlet

loading and initializing the servlet by the Servlet container. the Web container can load the Servlet at either of the following two stages.
initializing the context, on configuring the Servlet with a zero or positive integer value to load-on startup property.
if the servlet is not loaded in the preceding stage, it may delay the loading process until the Web container determines that this servlet is needed to service a request.

The Servlet container performs the following operations while loading the servlet

Loading: Loads the Servlet class by using the normal java class loading option. servlet class may be loaded a local system, other network system or a remote file system. If the Servlet container fails to load the class, it terminates this process.

Instantiation: creates an instance of the servlet. To create a new instance (obj) of the servlet, the container uses no-argument constructor. The no. of objects for a Servlet instance is as below:

1. if a servlet is not hosted in a distributed environment, and it does not implement the SingleThreadModel interface, the container creates only one instance of the servlet declared in the Deployment Descriptor. A web application configuration file is also known as Deployment Descriptor.

2. If the Servlet is hosted in a distributed environment, the servlet container creates one servlet instance per java virtual machine (JVM)
3. If the Servlet implements the SingleThreadModel, the servlet container may create multiple objects for the servlet. If, in this servlet is hosted as distributable, the Servlet container may create multipel instances per JVM as well.

Intializing a Servlet:

After the Servlet is instantiated, the Servlet container initializes the instantiated servlet object. the container initialize the object by invoking the Servlet.init (ServletConfit) method, which accepts the ServletConfig object reference as a parameter. The servletconfig object allows teh Servlet instance to access name-value configured initialization parameters and other container specific details. The servlet container creates one ServletConfig object per servlet declared in the deployment descriptor.The Servlet Container calls the Servlet.init(ServletConfig) method is used to initialize resources, such as business Factory and JDBC DataSource and implement a code that has to be executed once per instance.
If this process completed successfully, Servlet comes into active state and makes it available and ready to handle requests. Servlet container considers a Servlet object that is initialized successfully only if the init() returns an object within the time period defined by the Web server, successfully without throwing ServletException.

If the Servlet does not initialize the servlet object?

then it will inform the servlet container about it by throwing the ServletException or UnavailableException, where the UnavailableException is a subtype of ServletException. We can create UnavailableException by using any of the following two constructors:

UnavailableException (String msg): instantiates a permanently unavailabe exception with the given message.
UnavailableException(String msg, int seconds)- instantiates a temporarily unavailable exception with the given message and estimated time period; ie: how long it will be unavailable
If the servlet.init(ServletConfig) method throws an exception, the Servlet container does not put Servlet instance into the active state, which means it removes (or destroys) the instance. If an exception iis thrown by the Servlet.init(0 method, the Servlet container performs the following operations.
1. if it throws temporarily UnavialableException, it implies that the Servlet initialization has failed, and will not be available for the specified time period. The Servlet container does not accept any request till the time the Servlet is available. If any request is made, UnavailableException returns the SERVICE UNAVAILABLE (503) response. However, when there is another after the given time period, container can try to instantiate and initialize the Servlet..
2. If it throws permanantly UnavaialbeException, it implies that the Servlet initialization has failed, and will not be available until the context is redeployed.
3. If the servlet doesnt initialize the object, servlet container can not call the destroy().

Example program for Life Cycle Servlet

<HTML>
<HEAD>
<title> Servlet Examples (Example to show Servlet Lifecycle)</title>
</HEAD>
<BODY>
<FORM ACTION=" myFirstServlet "><br/><br/><br/>
<center><INPUT TYPE="submit" value="Invoke Life Cycle Servlet"/></center>
</FORM>
</BODY>
</HTML>



package com.rajendra.servlets;

import javax.servlet.*;


public class LifeCycleServlet implements Servlet {

public void init(ServletConfig sc) {

config=sc;
System.out.println("in Init");
}//init

public void service(ServletRequest req,ServletResponse res) 
throws ServletException, java.io.IOException {

java.io.PrintWriter out=res.getWriter();
out.println("Hello from LifeCycleServlet");
System.out.println("in service");
}//service

public void destroy() {

System.out.println("in destroy");
}//destroy

public String getServletInfo() {

return "LifeCycleServlet";
}//getSI

public ServletConfig getServletConfig() {

return config;
}//getServletConfig

private ServletConfig config;
}//class
//
//D:\\lifecycle>javac  -d . LifeCycleServlet.java
//D:\webtechnologies\servlets\prog\lifecycle>jar -cvf lifecycle.war *


web.xml

<web-app>
<servlet>
<servlet-name>ls</servlet-name>
  <servlet-class>com.rajendra.servlets.LifeCycleServlet</servlet-class>
  </servlet>

<servlet-mapping>
  <servlet-name>ls</servlet-name>
  <url-pattern>/myFirstServlet</url-pattern>
  </servlet-mapping>
</web-app>

No comments:

Post a Comment