JSP page execution principle and way to get context root

1. When a JSP file is requested for the first time, the JSP engine converts the JSP file into a Servlet. And the engine itself is a servlet. The essence of jsp belongs to the background. Its execution process is as follows:

(1) The JSP engine first converts the JSP file into a Java source file. If any syntax errors are found in the JSP file during the conversion, the conversion process will be interrupted and an error message will be output to the server and the client.

(2) If the conversion is successful, the JSP engine uses javac to compile the Java source file into the corresponding class file.

(3) Create an instance of the servlet (the conversion result of the JSP page), the jspInit() method of the servlet is executed, and the jspInit() method is executed only once in the life cycle of the servlet.

(4) The jspService() method is called to process the client's request. For each request, the JSP engine creates a new thread to process the request. If multiple clients request the JSP file at the same time, the JSP engine will create multiple threads. Each client request corresponds to a thread. Executing in multi-threaded mode can greatly reduce the resource requirements of the system and improve the concurrency and response time of the system. However, you should also pay attention to the programming limitations of multi-threading. Since the servlet always resides in memory, the response is very fast.

(5) If the .jsp file is modified, the server will decide whether to recompile the file according to the settings. If recompilation is required, the compilation result will replace the Servlet in the memory, and the above process will continue.

(6) Although the JSP is very efficient, there is some slight delay due to the need for conversion and compilation on the first call. Also, at any time due to insufficient system resources, the JSP engine will remove the servlet from memory in some indeterminate way. When this happens the jspDestroy() method is called first.

(7) The Servlet instance is then marked for "garbage collection" processing. Some initialization work can be performed in jspInit(), such as establishing a connection with the database, or establishing a network connection, taking some parameters from the configuration file, etc., and releasing the corresponding resources in jspDestory().

 

2. When the jsp page is compiled, the included html code will be generated for display, and then the js code will be embedded in the html for control. The jsp page can receive java code as a servlet, but the js file cannot receive java code. If you want js The code receives java code, the js code must be written in the jsp page and cannot be externally quoted, otherwise an error will be reported when compiling and executing,

  The jsp page we use is essentially the background code, so understanding the principle will help you avoid some coding errors and help you avoid detours.

(Note: I wrote this article in my spare time, hoping to help you, welcome to read and comment, thank you)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325204159&siteId=291194637