Servlet Life Cycle

Servlet Life Cycle

Introduction

In this article we will discuss the life cycle of servlets. The servlet container provides us the facility to manage the life cycle of the servlet and uses javax.servlet.Servlet for the same. Before creating the servlet object we need first understand the life cycle of the servlet object which is based on understanding how the servlet container manages the servlet object.

The stages of the servlet life cycle are the following:

  • Load a servlet
  • Initialize a servlet
  • Handle the request.
  • Terminate the servlet.

Now we will discuss all these stages in detail:

Load a servlet

This is the very first stage of the servlet. It is all about loading and initializing the servlet with the help of the servlet container. The servlet can be loaded by the servlet container or web container in one of the following two stages:

  • Initialize the context, by configuring the servlet using zero or positive integral value.
  • There might be delay in loading the process if the server is not in the preceding stage.

The following two operations are performed in this stage:

  • Loading: It loads the servlet class.
  • Instantiation: The instance of the servlet is created. In order to create a new instance of the servlet, the container makes use of the no-argument constructor.

Initialize a servlet

After a servlet is initialized successfully, the server container then initializes the servlet object. The servlet object is initialized by calling the Servlet.init(ServletConfig) function. This function accepts ServletConfig object reference as a parameter. The Servlet.init(ServletConfig) method is called only once by the Servlet container just after the Servlet.init(ServletConfig) object is created.

Handle the request

After the initialization, the servlet object is ready to fulfill the requests made by clients. The following operations are performed by the servlet container when a servlet instance appends a service to a request:

  • The objects of ServletRequest and ServletResponse are created. If this comes out to be a HTTP request then the web container builds HttpServletRequest and HttpServletResponse objects that are the subtypes of the ServletRequest and ServletObject objects.
  • Once the request and response objects are made successfully, the Servlet.service is invoked by passing the request and response objects.

Terminate a servlet

The following operations are performed when it is finally decided to terminate or destroy a servlet:

  • All the threads are allowed to complete their job and get released that are currently running in the service method.
  • After the jobs are done, the destroy() method is called on the object of the servlet.

Once the servlet is destroyed, the servlet container releases all the references of the servlet objects.

Conclusion

In this article, we discussed the life cycle of a servlet. We saw how different stages work in the servlet life cycle. We believe that this tutorial has been very knowledgeable for you.