×
☰ See All Chapters

Life Cycle of a Servlet - Servlet Life Cycle

From server startup till server shutdown servlet undergoes many stages in its life time. The web container maintains the life cycle of a servlet instance. Different life cycle stages are:

  1. Instantiation: (loading servlet class and creating a servlet instance) – This happens only once. 

  2. Initialization: init() method (doing any initialization operations) – This happens only once. 

  3. Service: service() method – This happens for every request. 

  4. Finalization: destroy() method (doing cleanup operation) – This happens  only once 

  5. Destruction: destroying the servlet instance- This happens only once. 

Instantiation

There are 2 ways a servlet instance gets created.

  • When server starts up. (<load-on-startup> should be used in web.xml)-This will be studied in coming chapters. 

  • When client makes a first request to the servlet. (If <load-on-startup> tag is not used) 

There will be one servlet instance per one servlet irrespective of number of users/clients. Instantiation of servlet takes place only once during the life time of a servlet.

Initialization

After creating servlet instance container creates ServletConfig object and config object have got config parameters as specified in web.xml related to the particular servlet (ServletConfig will be studied in next chapter). These config parameters are used to do initialization operations.

After creating ServletConfig object Container calls init() method by passing object of ServletConfig as parameter.

Syntax of the init() method is given below:

In database and servlet example given in previous chapter, when client makes a request we established connection to database and after giving the response connection was closed. Likewise for each client we were making fresh connection to database and after handling the client connection was closed. This leads to heavy network traffic between server and database as number of clients increases. Solution to this problem is establishing connection to database only once and closing connection to database only once, irrespective of number of clients.

As we know init( ) method executes only once we can write code to establish connection to database inside init().

Service

The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below:

servlet-life-cycle-0
 

Container will never call doXxx(), container will call service().

The order in which these methods are called is

servlet-life-cycle-1
 

If doXxx() is overridden in our servlet then overridden method will be called otherwise method present in HttpServlet class will be called which throws exception. If we do not know type of request then override all doXxx() methods. If you override the default service() method, the servlet must either process all types of HTTP requests or include logic to dispatch requests to the appropriate  doXxx()  method.

Finalization

When server is shutdown, destroy() method will be called do following cleanup operation:

  • All the Database connections will be closed. 

  • All the file connections, socket connections will be released. 

  • Totally all the resources will be released.  

Destruction

In destruction phase following operations will happen

  1. Container destroys all the servlet instances by calling destroy() method on each instance. 

  2. Container destroys all the ServletConfig objects related to all the servlets. 

  3. Container destroys all the filter instances by calling destroy() method on each instance. 

  4. ServletContext object will be destroyed. 

  5. Thread pool will be destroyed. 

  6. web.xml is unloaded. (The objects which contain the data belonging to web.xml will be destroyed). 

Complete steps from server startup to server shutdown

At server start up following things will happen

  1. Container reads the web.xml file i.e. using the SAX parser container reads the data from web.xml document and converts the data into java objects. If any errors are found while parsing the document then SAXParserException will be thrown. If SAXParserException comes at server start up, then it means that deployment is not successful. 

  2. ServletContext object will be created by container using context parameters as specified in the web.xml. 

  3. All the Filter instances in the application will be created. (Filters will be studied later) 

  4. Thread pool will be created by the container. Threads inside this pool will be used by the container whenever client is sending the request. The size of the thread pool will not remain same, container will increases/decreases the pool size based on number of requests from the clients. 

  5. Servlet instances will be created for all the servlets for which <load-on-startup> tag has been used. 

There are 2 ways a servlet instance gets created.

    1. When server starts up. (<load-on-startup> should be used in web.xml)-This will be studied in coming chapters. 

    2. When client makes a first request to the servlet. (if <load-on-startup> tag is not used) 

There will be one servlet instance per one servlet irrespective of number of users/clients. Instantiation of servlet takes   place only once during the life time of a servlet. Server is now ready for accepting the first request from the client.

Once the server is ready, and client is sending a first request to a servlet following things will happen

  1. Client request will be delegated to the container. 

  2. URL will be compared with the URL pattern in web.xml 

  3. If the request URI is not found with any servlet then error message “request URI not available status code: 404” will be sent to the client. 

  4. If URL patter is available in web.xml then servlet class will be identified. 

  5. If identified class does not exist then ClassNotFoundException will be thrown. 

  6. If the servlet class is found then container creates the servlet instance by calling the default constructor. Servlets should not have any parameterized constructor. There are no chances of calling any parameterized constructor. 

  7. ServletConfig object will be created by container using config parameters as specified in the web.xml. 

  8. Container calls init() method by passing ServletConfig object as parameter. 

  9. Container assigns a thread to the request. Thread will be taken from thread pool which was created earlier. 

  10. The thread calls service() method by passing request and response as arguments. 

  11. service() method processes the request and provides the response to the client. 

  12. Once the response is handed over to the client, request and response objects will be destroyed and the thread will be returned back to the pool. 

  13. The servlet instance now waits for further requests from the client. Servlet instance will not be destroyed. 

If next requests come to the same servlet following things will happen

  1. A thread will be assigned to the request. 

  2. After service () method completes execution and provides response, Request and Response objects are destroyed and the thread is returned back to the pool. 

Once server is shutdown following things will happen

  1. Container destroys all the servlet instances,  ServletConfig objects, filter instances, ServletContext object, Thread pool by calling destroy() method on each instance. 

  2. web.xml is unloaded. All the xml objects created for web.xml will be destroyed. 

 

 

 


All Chapters
Author