English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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, the difference being that the JSP lifecycle also includes compiling the JSP file into a servlet.
The following are several stages that JSP goes through in its lifecycle:
Compilation phase:
The servlet container compiles the servlet source file to generate the servlet class
Initialization phase:
Load the servlet class corresponding to JSP, create its instance, and call its initialization method
Execution phase:
Invoke the service method of the servlet corresponding to JSP
Destruction phase:
Invoke the destroy method of the servlet corresponding to JSP and then destroy the servlet instance
It is obvious that the four main stages of the JSP lifecycle are very similar to the servlet lifecycle, and the following is a diagram:
When the browser requests a JSP page, the JSP engine will first check whether it needs to compile this file. If this file has not been compiled before, or if it has been changed since the last compilation, then compile this JSP file.
The compilation process includes three steps:
Parse JSP file.
Convert JSP file to servlet.
compile servlet.
After the container loads the JSP file, it will call the jspInit() method before providing any service for the request. If you need to perform custom JSP initialization tasks, just override the jspInit() method as follows:
public void jspInit(){ // Initialization code }
Generally speaking, the program is initialized only once, and servlet is the same. Usually, you can initialize database connections, open files, and create query tables in the jspInit() method.
This phase describes all the interaction behaviors related to requests in the JSP lifecycle until it is destroyed.
After the JSP page is initialized, the JSP engine will call the _jspService() method.
_jspService() method requires a HttpServletRequest object and an HttpServletResponse object as its parameters, as shown below:
void _jspService(HttpServletRequest request, HttpServletResponse response) { // server-side processing code }
_jspService() method is called once for each request and is responsible for generating the corresponding response, and it is also responsible for generating all7responses to HTTP methods, such as GET, POST, DELETE, etc.
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 servlet. When you need to perform any cleanup work, override the jspDestroy() method, such as releasing database connections or closing folders, etc.
The format of the jspDestroy() method is as follows:
public void jspDestroy() { // Cleanup code }
The following is an example of JSP lifecycle code:
<%@ 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 was initialized"+initVar+" times"); } public void jspDestroy(){ destroyVar++; System.out.println("jspDestroy(): JSP was destroyed"+destroyVar+" times"); } %> <% serviceVar++; System.out.println("_jspService(): JSP responded a total of "+serviceVar+" requests"); String content1="Initialization Count: "+initVar; String content2="Response to Customer Request Count: "+serviceVar; String content3="Destroy Count: "+destroyVar; %> <h1>Basic Tutorial(oldtoolbag.com) JSP Test Example</h1> <p><%=content1 %></p> <p><%=content2 %></p> <p><%=content3 %></p> </body> </html>
When the browser opens this page, the output is: