jspInit(){}: This method is called when the jsp Page is initialized, and this method is only executed once during initialization, so you can perform some initialization parameter configuration and other one-time work here, created by the author
jspDestroy(){}: This method is called when the jsp Page is closed for some reason, created by the author
jspService(){}: A method for processing jsp Page automatically created by the jsp container, created by the jsp container
To be precise, jsp should have three internal methods, namely jspInit(), _jspService(), jspDestroy(). Among these three methods, jspInit() and jspDestroy() can be defined by the author, while _jspService() is defined by jsp The container is defined based on the content of the jsp Pge and cannot be defined by the author.
Let’s first talk about the internal principles of the jsp web page. When the jsp file is processed for the first time, it will be converted into a servlet. The jsp engine first converts the jsp file into a java source file. If an error occurs during the conversion process, it will stop immediately and send an error message report to the server and client; if the conversion is successful, a class will be generated. Then create a Servlet object and first execute the jspInit() method for initialization. Since the jspInit() method is only executed once during the entire execution process, you can perform some necessary operations in this method such as connecting to the database, initializing some parameters, etc. Then execute the _jspService() method to process the client's request. A thread will be created for each request. If there are multiple requests to be processed at the same time, multiple threads will be created. Since the servlet is stored in memory for a long time, execution It is fast, but because initialization requires compilation, the first execution is still relatively slow. If the jsp web page is closed or destroyed for some reason, the jspDestroy() method will be executed.
<%@ page language="java" contentType="text/html; charset=gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
< meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Test</title>
</head>
<body>
<%!
public void jspInit(){
System.out. print("start");
}
%>
<%!
public void jspDestroy(){
System.out.print("end");
}
%>
</body>
</html>
Execute this jsp file and then close it. Check the day's log under tomcat/logs. You will find that the content is "start and end". This is because the jspInit() method is called when starting to execute the jsp file, and the content "start" is recorded in the log. , call the jspDestroy() method when closing the jsp file, and record the content "End" in the log.