DEV Community

Cover image for Servlet Life Cycle
ADITYA SASANE
ADITYA SASANE

Posted on

Servlet Life Cycle

Introduction to Servlet Life Cycle

This tutorial gives you an understanding of Java Servlets and their life cycle. Before getting started with the Servlet Life Cycle, let us first gain some insight on what exactly is a Servlet and its process. Java Servlet is a class in Java programming language which is defined in Java Enterprise Edition, also known as Java EE. It was developed by Sun Microsystems in the year 1997. After the first version, 1.0 of Servlet, was released in the year 1997, many new versions were released, the latest being Servlet 4.0.

What is Servlet?

Servlet Technology is very useful in creating web applications as it generates dynamic web pages while residing at the server-side. Java servlets replaced CGI or Common Gateway Interface, which was a scripting language commonly being used as a server-side programming language.

Servlet is platform-independent, robust and it can access all the Java APIs like the JDBC (Java Database Connectivity) API for accessing the databases of any enterprise.

Java Servlet is used to create web applications that are dynamic in nature. In order to do so, it extends the server capability. It is capable of running on any web server which has a Servlet container integrated with it.

The Java Servlet process can be easily understood from the steps mentioned below :

  1. The client sends a request to a servlet container. The client here refers to any browser such as Chrome, IE, Mozilla, etc., in use.
  2. The container or the Web Server looks for the servlet. As soon as the server finds the servlet, it initiates the servlet.
  3. Now, the servlet processes the client request, and then a response is sent back to the server.
  4. This response is sent to the client by the server.

Before we move to the life cycle of a servlet, you should be clear with terminologies used until now in this article. This will be helpful while understanding the Servlet Life Cycle.

  1. Web Server: The Web Server or HTTP Server handles HTTP Requests and HTTP Responses. The requests sent by clients are handled, and a response is sent based on the request made by this server.
  2. Web Container : Web Container or Servlet Container or Servlet Engine interacts with the Servlets. It is an important component of a web server as it manages a servlet’s life cycle.

Life Cycle of Servlet

You can understand the life cycle of a Servlet as a sequence of steps that a servlet undergoes in its span of life, beginning from its initiation to getting destroyed. Servlet Engine manages the life cycle of the servlet, as told earlier.

The life cycle of a servlet can be summed up in below mentioned five points:

  1. The Servlet class is loaded.
  2. The Servlet instance is created.
  3. The init() method is invoked in order to initialize the servlet.
  4. The service() method is invoked repeatedly for each client request placed.
  5. The servlet is destroyed using destroy() method.

1. Loading of the Servlet Class

The servlet class is loaded with the help of the classloader. As soon as the request for a servlet is received by the web container, the servlet class gets loaded.

2. Creating a Servlet Class Instance

An instance of the servlet is created by the web container as soon as the servlet class gets loaded. Do keep in mind that the creation of a servlet instance is once in a lifetime process for every servlet, which means that it will be instantiated only once in the life cycle of the servlet.

3. Invoking the Init() Method

After the servlet instance is created, the task of the web container is to invoke the init() method. The init() method is invoked only once and initializes the servlet. If a servlet is invoked by any user, only one instance of this servlet will be created. Every single request results in a generation of a new thread. The data created or loaded by invoking the init() method remains throughout the servlet’s life.

Please make a note that the init() method will be called only once during the entire life of the servlet.

The syntax given below will invoke the init method –

public void init()throws ServletException
{
/​/code
}
Enter fullscreen mode Exit fullscreen mode

4. Invoking the Service() Method

The service () method is an important method that will be called every time when a request is received for the servlet. The web container is the component that will call the service() method to perform some real work, i.e., to receive requests sent by the client browser and to handle it by sending an appropriate response to the client. As the servlet is initialized, the service method is invoked, and all the HTTP request types are analyzed, such as GET, POST, PUT, DELETE, etc. After the request types are known, the service() method will dispatch the request to its handler method based on that.

Consider the case of a POST request made by the client. The job of the service() method is to call the doPost() method and dispatch the request to it. All the requests have their own handler method to which a call will be made by the service() method based on the type of request made. For example, for getting an exception, there is the doGet() handler method; for Put, there is a doPut() method, and so on.

Please note that the service() method is invoked every time a client request happens. This means that unlike the init() and destroy() method, the service() method can be invoked innumerable times during the servlet life cycle.

The syntax that you find below will invoke the service() method for Servlet :

public void service(ServletRequest req, ServletResponse response) throws IOException,ServletException
{
//code
}
Enter fullscreen mode Exit fullscreen mode

5. Invoking a Destroy() Method

On the occasion of shutting down the web server, the servlet is given a chance to unload all the servlets created. The destroy() method will remove all the initialized servlets, thus cleaning up the memory.

The syntax for it is:

public void destroy()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)