Home > php教程 > PHP开发 > body text

Detailed introduction to jsp automatic compilation mechanism

高洛峰
Release: 2016-12-03 09:40:11
Original
1041 people have browsed it

Detailed introduction to the jsp automatic compilation mechanism

In general, Jasper's automatic detection mechanism is relatively simple. It relies on a background thread to continuously detect whether the last modification time of the JSP file and the compiled class file are the same. If they are the same, it is considered not Change, but if different, you need to recompile. In fact, since the JSP of the project deployed in Tomcat may introduce other pages or other jar packages, and these resources may be remote resources, the actual processing will be more complicated. It is also necessary to traverse and detect whether these different imported resources are Modifications were made.

Detailed introduction to jsp automatic compilation mechanism

The above picture is a vivid schematic diagram. We know that there are four levels of containers in the Tomcat architecture, Engine, Host, Context and Wrapper, and jsp compilation corresponds to the wrapper level, so the StandardWrapper continuously performs tasks to call jasper , and jasper continuously detects and verifies various local and remote resources, and recompiles once it is found that it needs to be recompiled. Read on to see how this is achieved.

First of all, a background execution thread is needed. There is a dedicated thread in Tomcat to handle background tasks of different containers. If you want to perform certain background tasks in different containers, you only need to rewrite the backgroundProcess method. Since JspServlet corresponds to Wrapper level, so backgroundProcess needs to be rewritten in StandardWrapper. It will call the Servlet that implements the PeriodicEventListener interface. JspServlet implements the PeriodicEventListener interface. This interface has only one periodicEvent method. The specific detection logic can be implemented in this method.

Secondly, what is the basis for detection and judgment of recompilation? Recompiling is to turn jsp into Java and then into class again, and the condition that triggers this action is that when we modify a certain jsp file, or after the resources introduced by a certain jsp file are modified, the recompiling action will be triggered, so The best basis for judgment is the last modified attribute of a certain jsp or resource. The normal sequence is that the jsp is compiled to generate a class file, and the last modified attribute of the class file is set to the last modified attribute of the jsp file. At this time, the last modified attributes of the two files are are the same. When we change the jsp file and save it, the lastmodified attribute of the jsp is set to the current time. At this time, we determine whether to recompile by judging the lastmodified attribute of the two files. After recompilation, the lastmodified attributes of the jsp and class files are set to the same again. For imported resources, the lastmodified attribute of the imported resource during the last compilation is maintained in the memory. The lastmodified attribute of the imported resource is continuously obtained and compared with the corresponding lastmodified attribute in the memory. It can also be easily judged whether recompilation is needed.

Finally, how to detect local and remote resources respectively? For local resources, the java.io.File class can be used to easily read the lastmodified attribute of a JSP file or other files. For remote resources, such as jar packages, in order to facilitate the processing of the attributes contained in the jar, it is very convenient to use java.NET.URL. It contains many protocols, such as common jar, file, ftp and other protocols, which is very convenient to use,

URL includeUrl = new URL("jar:http://hostname/third.jar!/");
URLConnection iuc = includeUrl.openConnection();
long includeLastModified = ((JarURLConnection) iuc).getJarEntry().getTime();
Copy after login

It only takes three steps to complete reading the remote jar package and get the last modification time. Of course, URL also supports reading of local file resources, so it is a good abstract object for resource reading. The management of imported resources in Tomcat uses URL as the operation object.

This section discusses the implementation of Jasper's automatic detection mechanism. The automatic detection mechanism brings a good experience to our development. We don't have to modify the jsp ourselves and then perform the compilation operation ourselves. Instead, tomcat helps us time it through jasper. Detect compilation operations.

Thanks for reading, I hope it helps everyone


Related labels:
jsp
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!