Life Cycle of Servlet - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Life Cycle of Servlet

Share This

The servlet life cycle can be described as a series of steps through which a servlet goes during its life span, starting from loading till it gets destroyed.


Before explaining the life cycle of Servlet, let's discuss a few frequently used terminologies, which are used in this tutorial later.


Web Server A Web server is a program that uses HTTP to serve the files that form Web pages for the clients, in response to their requests. It can handle HTTP Requests sent by clients and responds to the request with an HTTP Response.

Web Container It's a part of the Web Server that runs Servlets. This is the main component of the Web Server that manages the life cycle of Servlets.

Life Cycle of Servlet


A servlet life cycle can be defined as the entire process from its creation to its destruction. The steps of the Servlet life cycle are discussed below.

Step 1: Loading of Servlet

When the web server starts up, all the servlets are deployed and loaded in the servlet container.

Step 2: Creating an instance of Servlet

Once all the Servlet classes are loaded, the Servlet container creates instances of each Servlet class. Web container creates only one instance per servlet class and all the requests to the servlet are executed on the same servlet instance.

Step 3: Invoke the init() method

Once all the servlet classes are instantiated, the init() method is invoked for each instantiated servlet. This method initializes the servlet. There are certain init parameters that you can specify in the deployment descriptor, web.xml file. For example, if a servlet has a value >=0 then its init() method is immediately invoked during web container startup.


You can specify the element in web.xml file like this:

<servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.t4b.MyServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>

Now the init() method for the corresponding servlet class com.t4b.MyServlet would be invoked during web container startup.


The init() method is called only once during the life cycle of the servlet.

Step 4: Invoke service() method

Each time the web server receives a request for servlet resource, a new thread is created that calls service() method. If the servlet is GenericServlet then the request is served by the service() method itself, if the servlet is HttpServlet then service() method receives the request and dispatches it to the correct handler method based on the type of request.


The service() method can be called any number of times during the servlet life cycle based on the number of requests. As long as the servlet is not destroyed, for each client request the service() method is invoked.

Step 5: Invoke destroy() method

When the servlet container shuts down or the servlet is removed from the container, the destroy() method is called.


Try the following code
package com.t4b.test; import javax.servlet.ServletConfig; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @WebServlet("/TestLifeCycleServ") public class TestLifeCycleServ extends HttpServlet { private static final long serialVersionUID = 1L; static { System.out.println("I am in static block!"); } public TestLifeCycleServ() { super(); System.out.println("I am in Constructor!"); } public void init(ServletConfig config) { System.out.println("I am in init method!"); } public void service(ServletRequest req, ServletResponse res) { System.out.println("I am in service method!"); } public void destroy() { System.out.println("I am in destroy method!"); } }




Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.