Life Cycle of a Servlet in Java

Mahesh Sharma
2 min readDec 20, 2021

Servlet Technology is very useful in the creation of web-based applications because it can create dynamic web pages that reside at the server’s side. Java servers were the replacement for CGI (also known as Common Gateway Interface, which was a scripting programming language widely employed 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. Servlet Engine manages the life cycle of the servlet, as told earlier.

The web container manages the life-cycle of an server instance. Let’s look at the life cycle of the servlet

  1. The class Servlet is loaded.

2. A Servlet instance is created.

3. the init method is called.

4. the service method is called.

5. The destroy method is used.

There are three states that exist in the servlet: new ready, new and finally, end. The servlet will be in the new state when a servlet instance has been made. After calling an init() process, Servlet comes in the state of readiness.

In the state of readiness the servlet executes all tasks. If the web container executes its destroy() method and then shifts to the final state.

1) A Servlet class is loaded

The class loader has the responsibility to load the class called Servlet. The class loader is loaded after the first request to the servlet is accepted from the Web container.

2.) A Servlet instance is created

The web container generates the instance of a server after loading the class servlet. The instance of the servlet is created once in the servlet’s lifecycle.

3.) the init method is activated

The web container invokes the init method only one time after creating the instance of a servlet. The init method is employed to start the server. It is the life cycle method of the javax.servlet.Servlet interface.

The syntax of the init method can be found below:

public null init(ServletConfig configuration) throws Servlet Exception

4.) the service method is activated

The web container invokes the service method every time a request to the server is received. If the servlet has not been in its initialization phase, it performs the three steps previously described, before calling this method. If it is initially created, it will call the method of service.

Note that the servlet is initially started only once.

This is the syntax for service methods in the Servlet interface is as follows:

public void service(ServletRequest request, ServletResponse response)

Throws ServletException and the IOException

5) The destroy method is invoked

The web container invokes the destroy method prior to taking the instance of a servlet out of the web container. The servlet is given the opportunity to clear any resource, like memory, thread , etc.

This syntax for the method of destruction in the Servlet interface is as follows:

public void destroy()

--

--

Mahesh Sharma

Hey, I'm Mahesh Sharma, a passionate digital marketer with 10+ years of experience in the field. I'll be sharing topics such as SEO, SMO, PPC/ SEM.