I. Overview of JSP Mechanism
The execution of JSP pages can be divided into two phases: Translation and request.
Translation phase: Convert JSP pages into Servlet classes.
Request Phase: servlet class execution, send the response results to the client.
1. the user (client) accesses the response JSP page, as shown in figureHttp: // localhost: 8080/prj_test/ch02/hellojsp. jsp.
2. The server finds the corresponding JSP page.
3. The server translates JSP into servletSource code.
4. The server loads the Servlet SourceCodeCompile as a class file.
5. The server loads the class file to the memory and executes it.
6. The server generates HTML code after executing the class file and sends it to the client. The client browser displays the HTML code according to the response.
If the JSP page is executed for the first time, it will go through these two phases. If it is not the first execution, it will only execute the request phase. This is why the second execution of the JSP page is faster than the first execution.
If the JSP page is modified, the server will find the modification and re-execute the translation and request phases. This is why the access speed becomes slower after the page is modified.
JSP execution process flowchart:
1. JSP Engine
the JSP Engine actually converts JSP tags, Java code on JSP pages, and even static HTML content into large Java code. These code blocks are organized by the JSP Engine into Java Servlets that are invisible to users. Then, the servlet automatically compiles the JVM (Java Virtual Machine) into Java bytecode. In this way, when a visitor requests a JSP page, if he does not know it, a pre-compiled servlet will actually complete all the work. Very concealed-and efficient. Because the servlet is compiled, the JSP code in the webpage does not need to be interpreted every time the page is requested. The JSP engine only needs to compile the servlet code once after it is modified. Then the compiled servlet can be executed. Because the JSP Engine automatically generates and compiles Servlets, you do not need to compile the code by using the Program , therefore, jsp can provide you with efficient performance and the flexibility required for rapid development.
2. Web Container and Servlet Container
the main task of the servlet container is to manage the servlet lifecycle. Web containers should be called Web servers more accurately. They are used to manage and deploy Web applications. Another type of server is called the application server, which has more powerful functions than the web server, because it can deploy EJB applications and implement transactions managed by containers, generally, application servers include Weblogic and websphere, which are both commercial servers and are powerful but cost-effective. Tomcat is the most typical Web Container. Tomcat is a Web Container and servlet container.
2. Working Principles of JSP when a Web Container (tomcat, JBoss, etc) when receiving the user's first JSP page request, the JSP Engine converts the JSP page to Java source code (servlet class). During the conversion process, if any syntax error is found in the JSP file, the conversion process is terminated and the error message is output to the server and client. If the conversion is successful, the JSP engine uses javac to compile the Java source code to generate the class file, then, the Web container loads the class file and creates a new servlet object for instantiation. After the servlet class is instantiated, the container loads jsinit to notify the servlet that it has entered the service column. The init method must be loaded, and the servelt can receive and request. If you want to load the database driver and initialize some values, the programmer can rewrite this method. In other cases, this method is generally empty. The jspinit () method is executed only once in the servlet lifecycle. Then, the jspservice () method is called to process client requests. The container creates a response document and sends it to the user. For example, when the user accesses the JSPs again, the container creates a response document again, after the class file is detached from the container, the JSP Engine reinitializes the JSP file instead of re-converting and compiling the file, create a response document and return it to the client. For each request, the Web Container creates a new thread to process the request. If multiple clients request the JSP file at the same time, the JSP Engine creates multiple threads. Each client request corresponds to a thread. Multi-threaded execution can greatly reduce system resource requirements and improve system concurrency and response time. but pay attention to the multi-thread programming restrictions. Because the servlet is always in the memory, the response is very fast. If the. jsp file is modified, the server will decide whether to recompile the file based on the settings. If you need to re-compile the file, replace the compilation result with the servlet in the memory and continue the above process. If the system resources are insufficient at any time, the Web Container will remove the servlet from the memory in an uncertain way. In this case, the jspdestroy () method is called first, and the servlet instance is marked as "Garbage Collection.
3. Differences between JSP scripts and declarations JSP code
- <%! Int COUNT =100; %> -------- JSP Declaration
- <% Int COUNT =100; %> --------- JSP script
<%! Int COUNT = 100; %> -------- JSP declaration <% int COUNT = 100; %> --------- JSP script
the difference between the two lies in the scope and lifetime.
(1). the names created in the JSP Declaration have the scope and lifetime of the class
(2) the names created in JSP scripts are limited to the scope and lifetime of methods.
the scope of the two is like defining an attribute a in the class in Java and defining an attribute B in the class method,
attribute B cannot be referenced in a class, but attribute a can be referenced in a method,
survival time:
JSP declaration, for example: <%! Int COUNT = 100; %> <% = count ++ %>
the variable lifetime in the script exists in the first user until the second user ...., if the first user accesses 100 for the first time, the second user accesses 101, and the third user accesses 102, and so on... If the server is stopped and restarted, the Count value is 100.
JSP script, example: <% int COUNT = 100; %> <% = count ++ %>
the variable lifetime in the script exists during each user's access, so no user access is 100
the JSP Container first initializes the declaration regardless of the declaration and script placement location, then execute the script.
conclusion:
(1) methods cannot be defined in scripts, however, you can define your own methods in the JSP declaration, because the script program is limited to the jspservice method. It is not allowed to re-define the method in the jspservice method.
(2) hidden objects such as out cannot be used in JSO declarations, because out and other hidden objects are defined in the scope jspservice method.
(3) variables defined in scripts cannot be referenced in JSP declarations.
(4) If a variable is defined in a method, it cannot be used before the method.