JSP life cycle


The key to understanding the underlying functions of JSP is to understand the life cycle they adhere to.

The JSP life cycle is the entire process from creation to destruction, which is similar to the servlet life cycle. The difference is that the JSP life cycle also includes compiling JSP files into servlets.

The following are the stages that the JSP life cycle has gone through:

  • Compilation stage:

    servlet container compilation servlet source file, generate servlet class

  • Initialization phase:

    Load the servlet class corresponding to JSP, create its instance, and call its Initialization method

  • Execution phase:

    Call the service method of the servlet instance corresponding to the JSP

  • Destruction phase:

    Call the destruction method of the servlet instance corresponding to the JSP, and then destroy the servlet instance

Obviously, the JSP life cycle The four main stages are very similar to the servlet life cycle. The diagram is given below:

jsp_life_cycle.jpg


JSP compilation

When the browser requests the JSP page , the JSP engine will first check whether this file needs to be compiled. If this file has not been compiled, or has been changed since the last compilation, compile the JSP file.

The compilation process includes three steps:

  • Parse the JSP file.

  • Convert JSP files to servlets.

  • Compile servlet.


JSP initialization

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

public void jspInit(){
  // 初始化代码
}

Generally speaking, the program is only initialized once, and the same is true for servlets. Normally you can initialize the database connection, open the file and create the query table in the jspInit() method.


JSP execution

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

When the JSP web page completes initialization, the JSP engine will call the _jspService() method. The

_jspService() method requires an HttpServletRequest object and an HttpServletResponse object as its parameters, like the following:

void _jspService(HttpServletRequest request,
                 HttpServletResponse response)
{
   // 服务端处理代码
}

_jspService() method is called once in each request and is responsible for generating The corresponding response, and it is also responsible for generating responses for all 7 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 web page is removed from the container.

The jspDestroy() method in JSP is equivalent to the destruction method in servlet. Override the jspDestroy() method when you need to perform any cleanup work, such as releasing the database connection or closing the folder, etc.

The format of the jspDestroy() method is as follows:

public void jspDestroy()
{
   // 清理代码
}

Instance

The JSP life cycle code example is as follows:

<%@ 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被初始化了"+initVar+"次");
  }
  public void jspDestroy(){
    destroyVar++;
    System.out.println("jspDestroy(): JSP被销毁了"+destroyVar+"次");
  }
%>

<%
  serviceVar++;
  System.out.println("_jspService(): JSP共响应了"+serviceVar+"次请求");

  String content1="初始化次数 : "+initVar;
  String content2="响应客户请求次数 : "+serviceVar;
  String content3="销毁次数 : "+destroyVar;
%>
<h1>php中文网 JSP 测试实例</h1>
<p><%=content1 %></p>
<p><%=content2 %></p>
<p><%=content3 %></p>

</body>
</html>

The browser opens the page, The output result is:

E80496E2-35DF-439F-8A43-6376D92DFA45.jpg