Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP Lifecycle

JSP Lifecycle

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

The JSP lifecycle is the entire process from creation to destruction, similar to the servlet lifecycle, except that the JSP lifecycle also includes compiling the JSP file into a servlet.

The following are the stages that the JSP lifecycle goes through:

Clearly, the four main stages of the JSP lifecycle are very similar to the servlet lifecycle; a diagram is given below:


JSP Compilation

When a browser requests a JSP page, the JSP engine first checks whether 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, just override the jspInit() method, as follows:



public void jspInit(){

  // Initialize code

}

Generally, a program is initialized only once, and the same is true for servlets. Typically you can initialize database connections, open files, and create lookup tables in the jspInit() method.


JSP Execution

This stage describes all request-related interactions in the JSP lifecycle until it is destroyed.

After the JSP page finishes initialization, the JSP engine calls the _jspService() method.

The _jspService() method requires an HttpServletRequest object and an HttpServletResponse object as its parameters, as follows:



void _jspService(HttpServletRequest request,

                 HttpServletResponse response)

{

   // Server-side processing code

}

The _jspService() method is called once per request and is responsible for generating the corresponding response; it is also responsible for generating responses for all 7 HTTP methods, such as GET, POST, DELETE, and so on.


JSP Cleanup

The destruction phase of the JSP lifecycle 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 a servlet. Override the jspDestroy() method when you need to perform any cleanup work, such as releasing database connections or closing files and folders.

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



public void jspDestroy()

{

   // Clean up code

}

Example

A JSP lifecycle code example is shown below:



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>





life.jsp







<%! 

  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;

%>

Runoob Tutorial JSP 测试实例

<%=content1 %>

<%=content2 %>

<%=content3 %>

Open the page in a browser; the output is as follows: