HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
The key to understanding the underlying JSP function is to understand that they comply with the life cycle.
JSP is the life cycle from creation to destruction, similar to the servlet life cycle, except that the JSP life cycle also includes a JSP file is compiled into a servlet.
The following is the JSP lifecycle traversed several stages:
Compile Phase:
servlet servlet container compile source files generated servlet class
Loading and corresponding JSP servlet class, create an instance and call its initialization method
Examples of service calls the servlet and JSP corresponding method
Destruction method call and the corresponding JSP servlet instance, then destroyed servlet instance
Obviously, the four main stages of the life cycle of JSP and servlet life cycle is very similar to the illustration below:
When a browser requests a JSP page, JSP engine will first need to check whether to compile the file. If this file is not compiled, or compiled was changed after the last time, then compile the JSP files.
The compilation process involves three steps:
After the container was loaded JSP file, it calls jspInit () method for the request before providing any service. If you need to perform initialization tasks JSP custom replication jspInit () method on the line, like this:
public void jspInit(){ // 初始化代码 }
In general, the program initialized only once, servlet well. Under normal circumstances, you can initialize the database connection in jspInit () method, open the file and create a query table.
This stage describes the JSP life cycle of all interactions associated with the request until it is destroyed.
When the JSP page initialization is complete, JSP engine will call _jspService () method.
_jspService () method requires a HttpServletRequest object and an HttpServletResponse object as its parameter, like this:
void _jspService(HttpServletRequest request, HttpServletResponse response) { // 服务端处理代码 }
_jspService () method is called once for each request and is responsible for generating Correspondingly response, and it is responsible for generating responses to all seven HTTP methods such as GET, POST, DELETE, and so on.
Destruction stage JSP lifecycle describes everything when a JSP page is removed from the container occurred.
jspDestroy () method in a JSP servlet equivalent to the destruction methods. When you need to perform any cleanup duplicate jspDestroy () method, such as the release of the database connection or close the folder, and so on.
Format jspDestroy () method is as follows:
public void jspDestroy() { // 清理代码 }
JSP life cycle of the code examples are as follows:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <title>life</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>本教程 JSP 测试实例</h1> <p><%=content1 %></p> <p><%=content2 %></p> <p><%=content3 %></p> </body> </html>
Browser opens the page, the output is: