Servlet life cycle – Servlet Life Cycle With Example | Life Cycle of a Servlet | Stages of the Servlet Life Cycle with Explanation

Servlet life cycle: Here is the proper tutorial for understanding the life cycle of a servlet and its stages. Mainly, there are five stages or phases that the servlet life cycle involves. Check out the detailed explanation about the Java servlet life cycle with examples from this tutorial and learn it efficiently.

Life Cycle of Servlet

Servlet life cycle method: The Servlet Life Cycle is the entire process of its creation until the destruction. The complete life cycle of a Servlet is controlled by the Servlet container which uses the javax.servlet.Servlet interface to understand the Servlet object and manage it. Let’s see the stages or phases of the servlet life cycle from the below image or further sections.

Servlet Life Cycle With Example 1

There are three methods that are central to the life cycle of a servlet. They are init(), service() and destroy() methods. They are implemented by every servlet and are invoked at a specific time by the server.

Do Check:

What are the phases of the Servlet life cycle?

Servlet life cycle in java: The servlet life cycle contains five phases:

1. load a servlet class: The class Classloader is responsible to load the servlet class.ClassLoader object is designed to load a class just once. It is loaded, when the first request for the servlet is received by the web container.

2. Servlet instance is created: At the time code for a servlet is loaded, the server creates a single instance. That single instance handles every request made of the servlet. The servlet object is created once in the entire servlet life cycle.

3. init() method: init() method is called after creating the servlet instance. It is called only once in the entire lifecycle. It is used to initialize the servlet. Init() is guaranteed to be called and completed before the servlet handles its first request. During the init() method, a servlet may want to read its initialization(init) parameter.

4. service() method: Service() method is called every time when a request for a servlet is received and it is used to handle requests as appropriate for the servlet. A request object and a response object are the two parameters that are accepted by the service() method. Also, it overrides doGet() and doPost() method.

  • doGet() method – doGet() method is used to handle the Get request.
  • doPost() method – doPost() method is used to handle the Post request.

5. Destroy() method: This method is called only once in the entire life cycle of a servlet. The servlet calls the destroy() method after the servlet has been taken out of service and all pending requests have been completed or timed out.

Servlet Life Cycle Methods

Java servlets life cycle: There are three life cycle methods in servlet and they are given in detail

  • service()
  • destroy()

init() method: The Servlet.init() method is known as the Servlet Container to indicate this Servlet Instance is instantiated successfully and is about to put into service.

//init() method

public class MyServlet implements Servlet{
public void init(ServletConfig config) throws ServletException {
//initialization code
}
//rest of code
}

service() method: The service() method of Servlet is invoked to inform the Servlet about the client requests.

  • It uses the ServletRequest object to collect the data requested by the client.
  • This method uses the ServletResponse object to generate output content.
// service() method

public class MyServlet implements Servlet{
public void service(ServletRequest res, ServletResponse res)
throws ServletException, IOException {
// request handling code
}
// rest of code
}

destroy() method: The destroy() method runs only once during a lifetime of a Servlet and signals the end of the Servlet instance.

//destroy() method

public void destroy()

Servlet Life Cycle Example Program

Java servlets life cycle: The following java program is demonstrated to define the life cycle of a servlet. Let’s check it out & understand the concept properly:

ServletLifeCycle.java

Servlet Life Cycle With Example 2
Web.xml
Servlet Life Cycle With Example 3

Java Program to Show Servlet Example

Java Program to Show Servlet Example

// Java program to show servlet example
// Importing required Java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class AdvanceJavaConcepts extends HttpServlet
{
private String output;

// Initializing servelet
public void init() throws ServletException
{
output = "Advance Java Concepts";
}

// Requesting and printing the output
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println(output);
}

public void destroy()
{
System.out.println("Over");
}
}