We all know that there are two forms of include in jsp, namely
<%@ include file=” ”%>
<jsp:include page=” ” flush=”true”/></P>
The former is an instruction element and the latter is a behavioral element. Where exactly will they be used? How to use them and what's the difference? This should be a question that many people will think of when they see it. Let’s take a look below. </P>
Usually when certain parts of all pages in the application (such as titles, footers, and navigation bars) are the same, we can consider using include. Specifically, when to use <%@ include file=""%>, and when to use <jsp:include page="" flush="true"/>. This form. The first thing to understand is the difference between them. Only by understanding the differences in their usage can you understand when to use them and how to choose.
<%@ include file=" ”%>, the jsp include directive element reads the content of the specified page. And integrate these contents with the original page. (This process is carried out during the translation stage: that is, the stage when jsp is converted into servlet.</P>
Here is an explanation of the translation stage: We know that the jsp page cannot be sent to the browser intact, and all jsp elements must first be processed by the server. This is done by converting the jsp page into a servlet and then executing the servlet. The server needs a jsp container to process jsp pages. The jsp container is usually implemented in the form of a servlet. This servlet is configured to handle all requests for jsp pages. </P>
The Jsp container is responsible for converting the jsp page into a servlet (called a jsp page implementation class? JSP Page implementation class) and compiling this servlet. These two steps constitute the translation stage.</P>
From this we will know: the jsp page adds the actual content (that is, the code snippet) of the page specified by the include directive element to the jsp page that introduces it. After being synthesized into a file, it is converted into a servlet by the jsp container. You can see that a temporary class file and a java file will be generated at this time. Here's an example. </P>
The server uses tomcat, and the jsp file introduced into the page is called test.jsp. The imported page is called date.jsp. This jsp file stores a jsp code related to time, and the current context root is set to test.
//======Source file of date.jsp======//
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%
java.util.Date date=new java.util.Date();
String date_cn = "";
String dateStr = "";
switch(date.getDay())
{
case 0:date_cn = "Day"; break;
case 1:date_cn = "一"; break;
case 2:date_cn = "二"; break;
case 3:date_cn = "三"; break;
case 4:date_cn = "四"; break;
case 5:date_cn = "五"; break;
case 6:date_cn = "六"; break;
}
dateStr = (1900+date.getYear()) + "Year" + (date.getMonth()+1) + "Month" + date.getDate() + "Day (Weekday) + date_cn + ")";
%>
document.write("<%=dateStr%>");
//======The following is the source file of test.jsp=============//
<%@ page language=”java” contentType=”text/html;charset=gb2312”%>
<html>
<head>
<title>Two uses of include</title>
<jsp:include page=”date.jsp” flush=”true”/>
<%--@ include file=”date.jsp” %-->
//We use two different forms of include here to introduce the date.jsp file.
<head>
<body>
<table><tr><td>
Please pay attention to the two usages of include in jsp.
</td></tr></table>
</body>
</html></P>
In the test.jsp file, we only output a line of text "About two uses of include in jsp. Please pay attention." Now let us first use the form <%@ include file="date.jsp" %> Introduce the date.jsp file. Do you think something could go wrong? An error message appears at this time:
HTTP Status 500?
org.apache.jasper.JasperException: /date.jsp(0,0) Page directive: can't have multiple occurrences of contentType
There are a bunch of errors below, but we only need to look at this to see where the problem lies. The status code is http 500 Server Internal Error. Take another look at the tips below. Multiple contentTypes cannot be specified in the date.jsp page.</P>
Here's why. This is because during the translation phase, the code of the date.jsp file was added to the test.jsp page intact to synthesize a file. The synthesized file will be the same:
<%@ page language=”java” contentType=”text/html;charset=gb2312”%>
This code. The solution is to delete this sentence in the date.jsp file. Refresh and then request the test.jsp page</P>
The request test.jsp is displayed on the page as follows
December 10, 2003 13:12:40
Please pay attention to the two usages of include in jsp. </P>
We can't find anything yet. Let’s check the temporary files under tomcat. Go there and see if the contents of the date.jsp file have been added to the test.jsp file.
<Note. The tomcat here is installed in the root directory of drive E>
Table of contents
E:tomcatworkStandalonelocalhosttest.
In this directory you will see
There are two files test_jsp.java and test_jsp.class. </P>
The java file here is the test_jsp.java file obtained by converting jsp into servlet by the jsp container. </P>
The corresponding test_jsp.class file is the class file generated by compiling the test_jsp.java servlet file. Open the generated servlet file (test_jsp.java). At this point we will find that when the test.jsp file is converted into a servlet file, some code that is not in the test.jsp page is added between the output <haed>. The newly added content is the code in date.jsp: Please test and see what new content has been added or whether new content has really been added. It will be clear at a glance. I will not go into details here.</P>
The above is the result we get using the form <%@ include file="date.jsp"%>.
Next, we use <jsp:include page="dae.jsp" flush="true"/>, that is,
Replace <%@ include file="date.jsp"%> with <jsp:include page="dae.jsp" flush="true"/>, and then request test.jsp.
2003?ê 12??10?? 13:30:13
Please pay attention to the two usages of include in jsp. </P>
At this time, you will see on the page that the Chinese characters in the date output by the date.jsp we introduced are garbled. What is the reason? It is because the include behavior element is executed during the request processing stage (here we need to explain the request processing stage. In addition to being responsible for converting jsp pages into servlets as mentioned above, the Jsp container is also responsible for calling the jsp page implementation class to process each request and generate a response. This stage is called the request processing stage. The request processing stage only executes the class file) . </P>
So when we introduce the include action element into the page, we actually only reference the servlet class file generated after the date.jsp file is converted and compiled. In this case, date.jsp is tested as a separate file after execution. It is called when the .jsp file is running. Because the character encoding is not specified in the date.jsp file, garbled characters appear. The solution is to re-replace the characters that were just removed in the date.jsp file.
<%@ page language=”java” contentType=”text/html;charset=gb2312”%>
After adding this line of statement, refresh and re-run. At this time, the page is displayed correctly and is the same as when running normally with the include directive. If you look at the temporary files under tomcat, you will find that there is a date_jsp.java file and a date_jsp.class. files. These two files are obtained in the same way as the test_jsp.java and test_jsp.class files. If you look at the code of the test_jsp.java file at this time, you will find that only a new line of code is added at this time:
JspRuntimeLibrary.include(request, response, "date.jsp", out, true);</P>
It does not add the code of the date.jsp file to test.jsp.</P>
It just introduces the response generated after the date.jsp page is executed at runtime. This means that we can specify any Web resource that can generate a response (such as a servlet or a jsp page), as long as the types generated by these resources are the same as jsp The content type generated by the page is the same. The JSP container will execute the specified resource through an internal function call. Therefore, these imported resources can help process the original request, so these resources can access all objects in the request scope. And all Original request parameters.</P>
Since these pages have not yet been introduced to the main page when the main page is requested, you can use a request-time attribute value for the page attribute to determine which page to introduce based on the runtime situation. You can also add some Request parameters to be read by the incoming page.
<jsp:include page="<%=pageSelectedAtRuntime%>" flush="true" >
<jsp:param name=”fitstParamer” value=”firstvalue”>
<jsp:param name=”lastParamer” value=”lastvalue”>
</jsp:include></P>
If the imported jsp page is modified, the latest version of the page can be used immediately. This is because the imported page is treated in exactly the same way as the jsp page called directly by the browser. That is, the container detects page changes. , and automatically enter the translation stage to get the latest version of the page.</P>
(Note that the include behavior element is the same as other jsp elements. If there is no behavior body, it must end with "/". Just like the following.
<jsp:include page="<%=pageSelectedAtRuntime%>" flush="true" />)</P>
The following is the difference between the two usages of include. There are two main differences;
1: Execution time:
<%@ include file=”relativeURI”%> is executed during the translation phase
<jsp:include page="relativeURI" flush="true" /> is executed during the request processing phase.
2: Differences in introduced content:
<%@ include file=”relativeURI”%>
Introduce static text (html, jsp) and integrate it with the JSP page before it is converted into a servlet.
<jsp:include page="relativeURI" flush="true" />Introduces the response text generated by the execution page or servlet.
Also in both usages the file and page attributes are interpreted as a relative URI. If it starts with a slash, then it is an environment-relative path. Will be interpreted according to the prefix assigned to the URI of the application, if it If it does not start with a slash, then it is a path related to the page. It will be interpreted according to the path of the page where this file is introduced. For more information about how URLs are interpreted, please refer to relevant materials or books.