Open In App

Life Cycle of JSP

Last Updated : 05 Feb, 2025
Comments
Improve
Suggest changes
79 Likes
Like
Report

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.

Life-Cycle-of-JSP


Steps of 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.


Explore