The detailed introduction of JSP automatic compiling mechanism
In general, the Jasper Automatic detection mechanism is relatively simple, relying on a background thread to constantly detect the JSP file and the compiled class file after the final modification time is the same, if the same is not changed, but if different will need to recompile. In fact, because the JSP for the project deployed in Tomcat may have introduced other pages, or other jar packages have been introduced, and these resources may be remote resources, the actual processing will be more complex, as is the need to traverse the different resources that are being introduced to detect changes.
Pictured above, we know that there are four levels of containers in the Tomcat architecture, Engine, Host, context, and wrapper, while JSP compilation corresponds to the wrapper level. So through Standardwrapper constantly carry out tasks to invoke Jasper, while Jasper constantly detect the verification of local and remote resources, once found that need to recompile to recompile. Look down to see how it is implemented.
First, a background execution thread is needed, and Tomcat has a dedicated thread to handle the background tasks of different containers, and it is only possible to rewrite the Backgroundprocess method to perform some background tasks in different containers. Because Jspservlet corresponds to the wrapper level, to override backgroundprocess in Standardwrapper, it invokes the servlet that implements the Periodiceventlistener interface. The Periodiceventlistener interface is implemented in Jspservlet, and this interface has only one periodicevent method, and the specific detection logic can be realized in this method.
Second, what is the basis for detecting and judging recompilation? Recompile is to turn the JSP into Java again into class, and trigger the action of the condition is that when we modify a JSP file, or a JSP file introduced by the resource is modified, will trigger the recompile action, So the best judgment is based on a JSP or resources of the last modification time lastmodified attribute, the normal order is the JSP compiled after the generation of class files, the class file LastModified attribute set to JSP file LastModified, At this time, the LastModified property of two files is the same, when we changed the JSP file Save, JSP LastModified attribute is set to the current time, at this point by judging the LastModified property of two files to decide whether to recompile. The LastModified property of the JSP and the class file is again set to the same after the recompile. For the introduced resources, the LastModified attribute of the resource introduced at the last compile time is maintained in memory, and the LastModified attribute of introducing the resource is constantly obtained and compared with the corresponding LastModified property in memory, it is also easy to determine whether recompilation is necessary.
Finally, how do you detect local and remote resources separately? For local resources, using the Java.io.File class makes it easy to read LastModified properties for a JSP file or other file. For remote resources, such as jar packages, to facilitate handling of the properties of the jar, it is convenient to use Java.NET.URL, it contains many protocols, such as common jar, file, FTP and other protocols, is very convenient to use,
URL includeurl = new URL ("jar:http://hostname/third.jar!/");
URLConnection Iuc = Includeurl.openconnection ();
Long includelastmodified = ((jarurlconnection) IUC). Getjarentry (). GetTime ();
It takes only three steps to complete the read of the remote Jar packet and the last modified time. Of course the URL also supports reading of local file resources, so it is a good resource to read abstract objects, and the management of introducing resources in Tomcat uses URLs as an object of action.
This section discusses the implementation of Jasper Automatic detection mechanism, automatic detection mechanism for our development has brought a good experience, we do not have to modify the JSP themselves to perform the compilation operation, but Tomcat through the Jasper to help us regularly detect the compilation operation.
Thank you for reading, I hope to help you, thank you for your support for this site!