This is the fourth day of my participation in the First Challenge 2022

The basic concept

  • For JSP-based Web applications, you can write Java code directly in JSP pages, add third-party libraries, and use EL expressions. But the final output to the client browser is standard HTML pages, including JS, CSS and so on. Without java-related syntax.JSP can be thought of as a script that runs on the server side and responds to the client as an HTML page
  • Convert JSP files to HTML page files using the Jasper engine in Tomcat:
    • A JSP is essentially a Servlet
    • Tomcat uses Jasper to parse JSP syntax, generate servlets, and generate Class bytecode files
    • When the user accesses the JSP file, he/she will access the converted Servlet, and the final access result responds directly to the browser in the form of AN HTML page
    • At run time, the Jasper engine detects if the JSP file has changed and recompiles the JSP file if it has

Compile the way

Runtime compilation

  • Tomcat does not automatically compile JSP files when the Web application is started, but only compiles the JSP files that need to be accessed when the client first requests them

The build process

  • Tomcat web in the default XML configuration in the org. Apache. Jasper. Servlet. JspServlet, used to handle all the JSP and the JSPX at the end of the request
  • The implementation of JspServlet is the entry point to run compile time
<servlet>
	<servlet-name>jsp</servlet-name>
	<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
	<init-param>
		<param-name>fork</param-name>
		<param-value>false</param-value>
	</init-param>
	<init-param>
		<param-name>xpoweredBy</param-name>
		<param-value>false</param-value>
	</init-param>
	<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>jsp</servlet-name>
	<url-pattern>.jsp</url-pattern>
	<url-pattern>.jspx</url-pattern>
</servlet-mapping>
Copy the code
  • JspServlet request processing flow:

Compile the results

  • If scratchdir is configured in tomcat/conf/web.xml, the compiled JSP is printed to the configured directory:
<init-param>
	<param-name>scratchdir</param-name>
	<param-value>e:/jsp/</param-value>
</init-param>
Copy the code
  • If this option is not configured, the compiled JSP will be stored in the work/Catalina/localhost/ directory of the Tomcat installation directory

precompiled

  • Precompile: Compile all the JSP pages used by the Web application at once, directly when the Web project starts. In this way, during the running of web projects, real-time compilation is no longer necessary, but the servlet corresponding to JSP page is directly called to complete the request processing, so as to improve the system performance
  • To precompile, you must first ensure that Apache Ant is downloaded and installed
  • Tomcat provides a shell program, JspC, to support JSP compilation, and a catalina-tasks. XML file in the Tomcat installation directory declares the Ant tasks that Tomcat supports, making it easy to perform JSP precompilation using Ant

Compilation principle

The code analysis

  • The generated Java file class called index_jsp. Java, inherited from org. Apache. Jasper. Runtime. HttpJspBase, this class is a subclass of HttpServlet. So JSP is essentially a servlet
  • The _jspx_denpendants attribute holds the resources that the current JSP page depends on, including the imported external JSP page, the imported tag, and the JAR package in which the tag is located. Easy to use in subsequent operations. For example, the last modification time of each resource is saved as a Map to facilitate recompilation detection
  • The _jspx_imports_packages attribute holds imported Java packages. By default, javax.servlet, javax.servlet. HTTP, and javax.servlet.jsp are imported
  • The _jspX_importS_classes attribute holds the imported classes. Classes imported by the import tag in JSP pages are contained in this collection. _jspX_IMPORT_packages and The _jSPX_IMPORT_classes attribute is primarily used to configure the EL engine context
  • Request processing is done by the _jspService method. The Service method in the parent HttpJspBase class calls the _jspService method in the subclass using the template method pattern
  • The _jspService method defines several important local variables: pageContext, Session, Application, config, out, and page. Because the output of the entire page is done by the _jspService method, these variables and parameters apply to the entire JSP page. This is why you can use variables in JSP pages
  • The value of the page-change tag in a JSP page that specifies the document type is ultimately used as Response.setContentType ()
  • For the static content of the generated HTML file, call out.write() to output
  • The < %.. The code in the %> tag is translated directly into the code in the Servlet class. If static files are embedded in the code, out.write() is also called

The compilation process

  • Jasper’s compilation process mainly consists of code generation and compilation

  • Compiler uses a PageInfo object to hold various configurations during JSP page compilation. These can be initialization parameters from web applications or tag directives from JSP pages, such as page, include, and so on
  • ParseController is called to parse the label instruction node, verify whether the label instruction is valid, and save the configuration information to PageInfo to control code generation
  • Call ParseController to parse the entire JSP page. Since JSP is parsed row by row, a specific Node object is created for each row, such as static text TemplateText, Java code Scriptlet, CustomTag CustomTag, Include tag directive IncludeDirective
  • Validates the validity of the remaining nodes outside the label instruction. Scripts, custom tags,EL expressions, etc
  • Gets page configuration information for nodes other than the label directive
  • Compile and load the tags that the current JSP page depends on
  • For EL expressions of JSP pages, the corresponding mapping functions are generated
  • Generate the corresponding servlet source code for the JSP page
  • After code generation is complete,Compiler generates SMAP information. If the configuration generates SMAP information, the Compiler writes the SMAP information to the class file at compile time
  • At compile time,CompilerTwo implementations ofAntCompilerandJDTCompilerOf the relevant frameworkAPISource code parsing
    • The AntCompiler completes the compilation by constructing an Ant javac task
    • JDTCompiler by calling org.eclipse.jdt.internal.compiler.Com piler complete compilation