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
- Loading and Instantiation
- Initialization
- Servicing the Request
- Destroying the Servlet
Process Diagram of the 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.
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
- <html>
- <head>
- <title>Servlet Example</title>
- </head>
- <body bgcolor="cyan">
- <form action="LifeCycleServlet.java"><br/><br/>
- <center>
- <input type="submit" value="Invoke Life Cycle Servlet"/>
- </center>
- </form>
- </body>
- </html>
LifeCycleServlet.java: Then we create a servlet file.
- package my;
-
- import javax.servlet.*;
- import javax.servlet.Servlet;
-
- public class LifeCycleServlet implements Servlet
- {
- public void init(ServletConfig sc)
- { config=sc;
- System.out.println("In Init");
- }
- public void service(ServletRequest req,ServletResponse res)throws ServletException, java.io.IOException
- {
- java.io.PrintWriter out=res.getWriter();
- out.println("Hello from lifecycle servlet");
- System.out.println("in Service");
- }
- public void destroy(){
- System.out.println("In Destroy");
- }
- public String getServletInfo(){return "LyfeCycleServlet";
- }
- public ServletConfig getServletconfig(){
- return config;
- }
- private ServletConfig config;
- }
web.xml: Then we create a web.xml file for mappimg the servlet file with
html
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <servlet>
- <servlet-name>Ls</servlet-name>
- <servlet-class>my.LifeCycleServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Ls</servlet-name>
- <url-pattern>/lifeCycleServlet</url-pattern>
- </servlet-mapping>
- </web-app>
OUTPUT
Home.html
Invoking Servlet