Easy Tutorial
❮ Jstl Function Replace Jstl Core Choose Tag ❯

JSP Life Cycle

Understanding the underlying functionality of JSPs is key to understanding their life cycle.

The JSP life cycle is the entire process from creation to destruction, similar to the servlet life cycle, with the difference being that the JSP life cycle also includes compiling the JSP file into a servlet.

Here are the several stages in the JSP life cycle:

Clearly, the four main stages of the JSP life cycle are very similar to the servlet life cycle. Below is a diagram:


JSP Compilation

When a browser requests a JSP page, the JSP engine first checks if the file needs to be compiled. If the file has not been compiled before, or has been modified since the last compilation, the JSP file is compiled.

The compilation process includes three steps:


JSP Initialization

After the container loads the JSP file, it calls the jspInit() method before serving any requests. If you need to perform custom JSP initialization tasks, override the jspInit() method as follows:

public void jspInit(){
  // Initialization code
}

Generally, initialization is done only once, just like with servlets. Typically, you can initialize database connections, open files, and create query tables in the jspInit() method.


JSP Execution

This phase describes all request-related interactions in the JSP life cycle until it is destroyed.

After the JSP page is initialized, the JSP engine will call the _jspService() method.

The _jspService() method requires a HttpServletRequest object and a HttpServletResponse object as its parameters, as shown below:

void _jspService(HttpServletRequest request,
                 HttpServletResponse response)
{
   // Server-side processing code
}

The _jspService() method is called once for each request and is responsible for generating the corresponding response. It also handles responses for all seven HTTP methods, such as GET, POST, DELETE, etc.


JSP Cleanup

The destruction phase of the JSP life cycle describes everything that happens when a JSP page is removed from the container.

The jspDestroy() method in JSP is equivalent to the destroy method in servlets. When you need to perform any cleanup tasks, such as releasing database connections or closing folders, override the jspDestroy() method as follows:

public void jspDestroy()
{
   // Cleanup code
}

Example

Here is an example of JSP life cycle code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>life.jsp</title>
</head>
<body>

<%! 
  private int initVar=0;
  private int serviceVar=0;
  private int destroyVar=0;
%>

<%!
  public void jspInit(){
    initVar++;
    System.out.println("jspInit(): JSP has been initialized "+initVar+" times");
  }
  public void jspDestroy(){
    destroyVar++;
    System.out.println("jspDestroy(): JSP has been destroyed "+destroyVar+" times");
  }
%>

<%
  serviceVar++;
  System.out.println("_jspService(): JSP has responded to "+serviceVar+" requests");

  String content1="Initialization count: "+initVar;
  String content2="Request response count: "+serviceVar;
  String content3="Destruction count: "+destroyVar;
%>
<h1>tutorialpro.org JSP Test Example</h1>
<p><%=content1 %></p>
<p><%=content2 %></p>
<p><%=content3 %></p>

</body>

The browser displays the page as:

❮ Jstl Function Replace Jstl Core Choose Tag ❯