Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSP lifecycle


May 12, 2021 JSP


Table of contents


JSP lifecycle

The key to understanding the underlying functionality of JSP is to understand the lifecycle they adhere to.

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

Here are a few stages in the JSP lifecycle:

  • Compilation phase:

    The servlet container compiles the servlet source file to generate the servlet class

  • Initialization phase:

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

  • Execution phase:

    Calls the service method of the servlet instance that corresponds to the JSP

  • Destruction phase:

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

Obviously, the four main phases of the JSP life cycle are very similar to the servlet lifecycle, as shown below:

JSP lifecycle


JSP compilation

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

The compilation process consists of three steps:

  • Resolve the JSP file.
  • Turn the JSP file into a servlet.
  • Compile the servlet.

JSP initialization

When the container loads the JSP file, it calls the jspInit() method before providing any services for the request. If you need to perform a custom JSP initialization task, just rewrite the jspInit() method, as follows:

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

In general, the program is initialized only once, as is the servlet. 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 throughout the JSP lifecycle until they are destroyed.

When the JSP page is initialized, the JSP engine calls the _jspService () method.

_jspService () method requires an HttpServletRequest object and an HttpServletResponse object as its parameters, as in 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 to all seven HTTP methods, such as GET, POST, DELETE, and so on.


JSP cleanup

The destruction phase of the JSP lifecycle describes what happens when a JSP page is removed from a container.

The jspDestroy() method is the destruction method in the JSP medium price in the servlet. When you need to do any cleanup work, write jpDestroy() methods, such as releasing database connections or closing folders, and so on.

The jspDestroy() method is formatted as follows:

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

Instance

An example of JSP lifecycle code looks like this:

<%@ 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>w3cschool教程 JSP 测试实例</h1>
<p><%=content1 %></p>
<p><%=content2 %></p>
<p><%=content3 %></p>

</body>
</html>