The key to understanding the underlying functionality of JSPs is to understand the life cycles 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 goes through:
Compilation phase:
The servlet container compiles the servlet source file and generates the servlet class
Initialization phase:
Load the servlet class corresponding to the 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 four main stages of the JSP life cycle are very similar to the servlet life cycle, as shown below:
When the browser requests a 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 consists of three steps:
Parse JSP files.
Convert JSP files into servlets.
Compile the servlet.
After the container loads the JSP file, it calls the jspInit() method before providing any service to the request. If you need to perform custom JSP initialization tasks, just override the jspInit() method, like this:
public void jspInit(){ // initialization code}
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.
This stage describes all request-related interactions 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){ // Server-side processing code}
The _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.
The destruction phase of the JSP life cycle describes what 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(){ // Clean up code}
An example of the JSP life cycle code 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 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 responded to a total of "+serviceVar+" requests"); String content1="Number of initializations: "+initVar; String content2="Number of responses to customer requests: "+serviceVar; String content3="Number of destructions: "+destroyVar;%> <h1>w3cschool tutorial JSP test example</h1><p><%=content1 %></p><p><%=content2 %></p><p><%=content3 %></p></body></html>