Life Cycle of JSP

Last Updated : 17 Apr, 2026

The life cycle of a JavaServer Page (JSP) consists of various phases that start from its creation, followed by its translation into a servlet, and finally managed by the servlet lifecycle. The JSP engine handles this process automatically.

  • JSP is converted into a servlet before execution.
  • The servlet is compiled and loaded by the server.
  • jspInit() method is called once for initialization.

Real-world example: When you open a login page on a website, the JSP file first gets converted into a servlet and initialized. Each time a user enters their credentials, the _jspService() method processes the request and shows the result (success or error), and when the server stops, jspDestroy() is called.

Life Cycle of JSP

The JSP (JavaServer Pages) life cycle defines the stages a JSP page goes through from creation to destruction on the server. It explains how a JSP is translated into a servlet, executed to handle requests, and finally removed when no longer needed.

Life-Cycle-of-JSP

Steps of JSP Life Cycle

Below are the steps to implement the JSP life cycle .

  1. Translation of JSP page to Servlet
  2. Compilation of JSP page(Compilation of JSP into test.java)
  3. Classloading (test.java to test.class)
  4. Instantiation(Object of the generated Servlet is created)
  5. Initialization(jspInit() method is invoked by the container)
  6. Request processing(_jspService()is invoked by the container)
  7. JSP Cleanup (jspDestroy() method is invoked by the container)

We can override jspInit(), jspDestroy() but we can't override _jspService() method.

1. Translation of JSP to Servlet

  • The JSP file is parsed and converted into a Java servlet source file (test.java).
  • This step checks for syntax correctness.

// The JSP is converted to a servlet class.
public class TestServlet extends HttpServlet {
// The generated servlet code
}

2. Compilation of JSP page

  • The generated test.java file is compiled into a class file (test.class).
  • This step converts the servlet code into bytecode.
Java
// JSP is automatically converted into a servlet, such as

public class TestServlet extends HttpServlet {
    // Generated servlet code here
}

3. Classloading

  • The container dynamically loads the compiled class.

4. Instantiation

  • The container creates an instance of the generated servlet class.
  • This instance handles multiple requests unless explicitly configured otherwise.
Java
// The container creates an instance automatically.
TestServlet servlet = new TestServlet();  


5. Initialization (jspInit())

  • This method is called only once when the JSP is first loaded.
  • It is used for initializing resources like database connections or configurations.
Java
public void jspInit() {
  
    // Initialization code, like setting up resources.
    System.out.println("JSP Initialized.");
}

6. Request Processing (_jspService())

  • This method is called for every request.
  • It cannot be overridden because it is auto-generated and declared as final.
  • It receives HttpServletRequest and HttpServletResponse objects to handle the request.
Java
public void _jspService(HttpServletRequest request, HttpServletResponse response) {
  
    // Code that handles the request, like generating HTML output.
    response.getWriter().write("<html><body>Hello, World!</body></html>");
}


7. JSP Cleanup (jspDestroy())

  • This method is called once before removing the JSP from service.
  • It is used for releasing resources, such as closing database connections or cleaning up open files.
Java
public void jspDestroy() {
  
    // Clean up resources like closing database connections.
    System.out.println("JSP Destroyed.");
}

This Lifecycle ensures that JSP pages are efficiently compiled, managed and cleaned up by the server container.

Comment