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:
Compilation Phase:
The servlet container compiles the servlet source file, generating the servlet class.
Initialization Phase:
The servlet class corresponding to the JSP is loaded, an instance is created, and its initialization method is called.
Execution Phase:
The service method of the servlet instance corresponding to the JSP is called.
Destruction Phase:
The destruction method of the servlet instance corresponding to the JSP is called, followed by the destruction of the servlet instance.
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:
Parsing the JSP file.
Converting the JSP file into a servlet.
Compiling the servlet.
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: