Jsp (preferred for SUN Enterprise Applications) Init () {}: jsp (preferred for SUN Enterprise Applications) This method is called when Page is initialized, in addition, this method is only executed once upon initialization. Therefore, the author creates the method by performing some initialization parameter configuration and other one-time work here.
Jsp (preferred for SUN Enterprise Applications) Destroy () {}: jsp (preferred for SUN Enterprise Applications) Page called when it is disabled for some reason, created by the author
Jsp (preferred for SUN Enterprise Applications) Service () {}: the jsp (preferred for SUN Enterprise Applications) method automatically created by the jsp (preferred for SUN Enterprise Applications) container to process the Page, created by jsp (preferred for SUN Enterprise Applications) Containers
To be precise, jsp (the preferred choice for SUN Enterprise Applications) should be implemented by three internal methods, namely jsp (the preferred choice for SUN Enterprise Applications) Init (), _ jsp (preferred for SUN Enterprise Applications) Service (), jsp (preferred for SUN Enterprise Applications) Destroy (), among which jsp (preferred for SUN Enterprise applications) init () and jsp (preferred for SUN Enterprise Applications) Destroy () can be defined by the author, while _ jsp (preferred for SUN Enterprise Applications) Service () the jsp (preferred for SUN Enterprise Applications) container is defined according to the content of jsp (preferred for SUN Enterprise Applications) Pge and cannot be defined by the author.
First, let's talk about the internal principle of jsp (preferred for SUN Enterprise Applications) web pages. When jsp (preferred for SUN Enterprise Applications) files are processed for the first time, they are converted into a servlet. The jsp (preferred for SUN Enterprise Applications) engine first converts a jsp (preferred for SUN Enterprise Applications) file into a java source file. If an error occurs during the conversion process, it will be immediately aborted, sends an error message report to both the server and client. If the conversion is successful, a class is generated. Then create a Servlet object. First, execute the jsp (preferred for SUN Enterprise Applications) Init () method for initialization, because the entire execution process jsp (preferred for SUN Enterprise Applications) Init () the method is executed only once, so you can perform some necessary operations in this method, such as connecting to the database and initializing some parameters, and then execute _ jsp (preferred for SUN Enterprise Applications) Service () method to process client requests. A thread is created for each request. If multiple requests need to be processed at the same time, multiple threads are created, because the servlet is stored in memory for a long time, the execution speed is fast, but the first execution is still slow due to initialization needs to be compiled. For some reason, jsp (preferred for SUN enterprise-level applications) the Destroy () method is executed when the web page is closed or destroyed.
<% @ 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 jsp (preferred for SUN Enterprise Applications) Init (){
System. out. print ("START ");
}
%>
<%!
Public void jsp (preferred for SUN Enterprise Applications) Destroy (){
System. out. print ("ended ");
}
%>
</Body>
</Html>
Run the jsp file (preferred for SUN enterprise-level applications) and close it to view the logs of the day under tomcat (a useful JSP running platform)/logs, you will find that the content is "start to end", because when you start to execute the jsp (preferred for SUN Enterprise Applications) file, call the jsp (preferred for SUN Enterprise Applications) Init () method, record the content "START" in the log. Call the Destroy () method of jsp (preferred for SUN Enterprise Applications) When closing the jsp (preferred for SUN Enterprise Applications) file, record "end" in the log ".