1. What is static import?
Static import refers to embedding an external file into the current JSP file and parsing the JSP statement on this page. It also contains other compilation commands on the target page.
Include static import command syntax:
Copy codeThe Code is as follows:
<% @ Include file = "relativeURLSpec" %>
Example of static import:
Copy codeThe Code is as follows:
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" errorPage = "" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> static include example </title>
</Head>
<Body>
<! -- Use include to compile the specified import page -->
<% @ Include file = "error. jsp" %>
</Body>
</Html>
Here, error. jsp is a simple error handling page with the following code:
Copy codeThe Code is as follows:
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" isErrorPage = "true" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> error message page </title>
</Head>
<Body>
This is an error handling page </br>
</Body>
</Html>
After running the static include example in tomcat, view the generated servlet class and see the following code segment:
(View method: in windows, find the work folder under the tomcat installation directory, go to the localhost folder under the Catalina folder, find the folder where the current project is located, find the org directory, go to the jsp folder under the apache directory, and you will see a deployment demo-jsp.java file. This is the servlet class generated for the deployment de1.jsp. Ps: I put this test page under webDemo, so my directory is \ apache-tomcat-7.0.47 \ work \ Catalina \ localhost \ webDemo \ org \ apache \ jsp)
From the box, we can see that the static import is: The containing page will completely contain the code of the included page during compilation. It should be noted that static import will also include the compilation commands on the included pages. If the compilation commands on the two pages conflict, the page will fail.
2. What is dynamic import?
Dynamic import does not import the compilation instructions of the include page, but simply inserts the body content of the imported page into this page.
Syntax format of dynamic import:
Copy codeThe Code is as follows:
<Jsp: include page = "{relativeURL | <% = expression %>}" flush = "true"/>
Or
Copy codeThe Code is as follows:
<Jsp: include page = "{relativeURL | <% = expression %>}" flush = "true">
<Jsp: param name = "parameterName" value = "parameterValue"/>
</Jsp: include>
Flush attribute specifies whether the output cache is transferred to the imported file. If it is set to true, it is included in the imported file. If it is set to false, it is included in the original file. For earlier versions of JSP1.1, it can only be set to false.
For the second syntax format, you can add additional request parameters to the imported page.
Dynamic import example:
We changed the static import of the above include1.jsp code to dynamic import.
Copy codeThe Code is as follows:
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" errorPage = "" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> static include example </title>
</Head>
<Body>
<! -- Use include to compile the specified import page -->
<Jsp: include page = "error. jsp"/>
</Body>
</Html>
After compilation, view the generated servlet code. You can see the following section:
The code in the red box shows that the dynamic import only uses the include method to insert the content of the target page, rather than fully integrating the target page into this page.
To sum up, there are three differences between static and dynamic imports:
1. Static import fully integrates the code on the imported page and the two pages into a Servlet. Dynamic import uses the include method in the Servlet to introduce the content on the imported page.
2. During static import, the compilation commands on the imported page will take effect, while during dynamic import, the compilation commands on the imported page will become useless, but only the body content of the imported page will be inserted.
3. Additional parameters can be added for dynamic import. The following code is used:
Copy codeThe Code is as follows:
<Jsp: include page = "{relativeURL | <% = expression %>}" flush = "true">
<Jsp: param name = "parameterName" value = "parameterValue"/>
</Jsp: include>