Life Cycle of Servlet

Servlet Life Cycle

 
The life cycle of a Servlet involves creating an instance of the Servlet, associating the Servlet with initialization information, dispatching request to the servlet instance and finally destroying the servlet instance. The life cycle of a servlet begins when it is loaded into Application Server memory and ends when the servlet is terminated or reloaded.
 

Four stages in Servlet life Cycle 

  1. Loading and Instantiation
  2. Initialization
  3. Servicing the Request
  4. Destroying the Servlet
Process Diagram of the Servlet Life-cycle
 
Servlet Life Cycle
 
Loading a Servlet: The first stage of the Servlet life-cycle involves loading and initializing the servlet by container. Whenever the web container is started, it loads and initializes the Servlet. the web container may delay this untill the web container determines which servlet is needed to service a request. The Servlet Container loads the servlet during startup or when the first request made. The Loading of servlet depends on <load-on-startup> attribute in web.xml file. If the attribute <load-on-startup> has the positive values then the servlet is load with loading of the container otherwise it will load when the first request comes for service. After loading the servlet, the container creates the instance of the servlet.
 
Servlet Initializing: After creating the instances, the servlet container calls the init() method and the servlet initialization parameters to the init() method. The init() method must be called by the servlet container before the servlet can service any request. The initialization parameters persist until the servlet is destroyed. The init() method is called only once throughout the life cycle of the servlet. The Servlet will be available for service if its loaded successfully otherwise the servlet container unloads the servletServlet's can be dynamically loaded and instantiated when their services are first requested, the Web server can be configured so that specific servlets are loaded and instantiated when the Web server initializes. In either case, the init method of the servlet performs any necessary servlet initialization and is guaranteed to be called once for each servlet instance, before any requests to the servlet are handled. An example of a task which may be performed in the init method is the loading of default data parameters or database connections.
 
The most common form of the init method of the servlet accepts a ServletConfig object parameter. This interface object allows the servlet to access name/value pairs of initialization parameters that are specific to that servlet. The ServletConfig object also gives us access to the SevletContext object that describes information about our servlet environment. Each of these objects will be discussed in more detail in the servlet examples sections.
 
Request Handling: After completing the initialization process, the servlet will be available for service. Servlet creates separate thread for each request. The servlet container calls the service() method for servicing any request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost() for handling the request and sends response to the client using the method of the response object. Application Server - Express receives a client request. The servlet engine creates a request object and a response object. The servlet engine invokes the servlet service() method, in the request and response objects. The service() method gets information about the request from the request object, processes the request, and uses methods of the response object to create the client response. The service method can invoke other methods to process the request, such as doGet(), doPost(), or methods you write.
 
request.gif 
 
doPost: Invoked whenever an HTTP POST request is issued through an HTML form. The parameters associated with the POST request are communicated from the browser to the server as a separate HTTP request. The doPost method should be used whenever modifications on the server will take place.
 
doGet: Invoked whenever an HTTP GET method from a URL request is issued, or an HTML form. An HTTP GET method is the default when a URL is specified in a Web browser. In contrast to the doPost method, doGet should be used when no modifications will be made on the server, or when the parameters are not sensitive data. The parameters associated with a GET request are appended to the end of the URL, and are ed into the QueryString property of the HttpServletRequest.
 
Destroying the Servlet: If the Servlet is no longer needed for servicing any request, the servlet container calls the destroy() method. like init() method it is also called only once throughout the life cycle of the servlet. The servlet engine stops a servlet by invoking the servlet's destroy() method. Typically, a servlet's destroy() method is invoked when the servlet engine is stopping a Web application which contains the servlet. The destroy() method runs only one time during the lifetime of the servlet and signals the end of the servlet. After a servlet's destroy() method is invoked, the servlet engine unloads the servlet, and the Java virtual machine eventually performs garbage collection on the memory resources associated with the servlet.
 

Developing the Servlet application

 
Here we are going to develop a simple Servlet application Of servlet lifecycle. This application includes the following files:
 
Home.html: First we create a html file
  1. <html>  
  2. <head>  
  3. <title>Servlet Example</title>  
  4. </head>  
  5. <body bgcolor="cyan">  
  6. <form action="LifeCycleServlet.java"><br/><br/>  
  7. <center>  
  8. <input type="submit" value="Invoke Life Cycle Servlet"/>  
  9. </center>  
  10. </form>  
  11. </body>  
  12. </html>  
LifeCycleServlet.java: Then we create a servlet file.
  1. package my;  
  2.   
  3. import javax.servlet.*;  
  4. import javax.servlet.Servlet;  
  5.   
  6. public class LifeCycleServlet implements Servlet   
  7. {  
  8.   public void init(ServletConfig sc)   
  9.   { config=sc;  
  10.    System.out.println("In Init");  
  11.   }  
  12.   public void service(ServletRequest req,ServletResponse res)throws ServletException, java.io.IOException  
  13.   {  
  14.    java.io.PrintWriter out=res.getWriter();  
  15.    out.println("Hello from lifecycle servlet");  
  16.    System.out.println("in Service");  
  17.   }  
  18.    public void destroy(){  
  19.    System.out.println("In Destroy");  
  20.   }  
  21.    public String getServletInfo(){return "LyfeCycleServlet";  
  22.   }  
  23.    public ServletConfig getServletconfig(){  
  24.    return config;  
  25.   }  
  26.    private ServletConfig config;  
  27. }  
web.xml: Then we create a web.xml file for mappimg the servlet file with html
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  3. <servlet>  
  4. <servlet-name>Ls</servlet-name>  
  5. <servlet-class>my.LifeCycleServlet</servlet-class>  
  6. </servlet>  
  7. <servlet-mapping>  
  8. <servlet-name>Ls</servlet-name>  
  9. <url-pattern>/lifeCycleServlet</url-pattern>  
  10. </servlet-mapping>  
  11. </web-app>  
OUTPUT
 
Home.html
 
home.gif
 
Invoking Servlet
 
lifecycleservlet.gif


Similar Articles