Servlet Life Cycle

The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.
  1. If an instance of the servlet does not exist, the web container
    1. Loads the servlet class.
    2. Creates an instance of the servlet class.
    3. Initializes the servlet instance by calling the init method. Initialization is covered in “Initializing a Servlet”.
  2. Invokes the service method, passing request and response objects.
  3. If the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s destroy method.
servlet lifecycle
1. Servlet class is loaded
The classloader is responisble for load a servlet class file into the web container's JVM.
<?xml version="1.0" encoding="UTF-8"?> <web-app > <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> <init-param> <param-name>userName </param-name> <param-value>Sunil</param-value> </init-param> </servlet> </web-app>

2. Servlet instance is created
After the servlet class has been loaded into the JVM, the next step is to create an instance of that class. The servlet specification declares that one, and only one, servlet instance will be created for a single servlet definition in the deploment descriptor.
<?xml version="1.0" encoding="UTF-8"?> <web-app > <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> <init-param> <param-name>userName </param-name> <param-value>Sunil</param-value> </init-param> </servlet> </web-app>
3. Calling the init method
After the servlet instance is created, the web container creates a unique ServletConfig object that contains any initilization parameters that are specified in the deployment descriptor.
<?xml version="1.0" encoding="UTF-8"?> <web-app > <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> <init-param> <param-name>userName </param-name> <param-value>Sunil</param-value> </init-param> </servlet> </web-app>
Getting the init parameter
public void init (ServletConfig config) throws ServletException
4. Calling the service method
The service() method is called by the servlet container after the servlet's init method to allow the servlet to respond to a request. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.
Each HTTP request mapped to a given servlet, the web container will create a unique request and response object to the service() method. And each request is proocessed a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.

Here is the signature of this method:

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{ }
5. Calling the destroy method
When a web application is being shutdown, the web container call the destroy() method. The destroy() method is called only once at the end of the life cycle of a servlet. This method clean up any resources that servlet might have initialized. Such as close database connections, halt background threads, write cookie lists or hit counts to disk and so on.
After the destroy() method is called, the servlet object is marked for garbage collection. The destroy method definition looks like this:

Here is the signature of this method:

public void delete(){ }
 



 

Latest Tutorial


Most Visited Tutorial