Two usages of include in JSP
1. Two ways to use
<%@ include file= ""%>
<jsp:include page= "" flush= "true"/>
2. Usage differences
(1) Difference in 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) the different content of the introduction
<%@ include file= ""%>
The include directive element of the JSP reads the contents of the specified page. and combine the content with the original page. (This process is done during the translation phase: that is, the JSP is transformed into a servlet.)
<jsp:include page= "Relativeuri" flush= "true"/> Introduces the answer text that is innate by the execution page or servlet.
3. How to bring the Include page into the reference
<jsp:include page= "<%=pageSelectedAtRuntime%>" flush= "true" >
<jsp:param name= "Fitstparamer" value= "Firstvalue" >
<jsp:param name= "Lastparamer" value= "Lastvalue" >
</jsp:include>
4. Description: Translation phase
Translation phase:
JSP pages cannot be passed to the browser intact, and all JSP elements must first be processed by the server. This is done by passing the JSP page into a servlet and then executing the servlet. The server needs a JSP container to process the JSP pages. The JSP container is usually implemented in the context of the servlet, which is configured to handle all requests to the JSP page.
The JSP container attaches importance to translating the JSP page into a servlet (called the JSP page implementation Class-JSP page implementation Class) and compiles the servlet. These two steps constitute the translation phase.
Request Processing phase:
JspContainer In addition to the above mentioned emphasis will beJspThe page turns intoServletOutside,Also attaches importance to callingJspThe page implementation class to process each request and generate an answer.This phase is called the request processing phase.. The request processing phase executes only the class document .
5. Example
Server with Tomcat, the JSP document that introduced the page is called test.jsp. The page being introduced is called date.jsp. This JSP document contains a JSP code about the time, and the current context root is set to test
Source document for ======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 = "one"; Break
Case 2:DATE_CN = "two"; Break
Case 3:DATE_CN = "three"; Break
Case 4:DATE_CN = "four"; Break
Case 5:DATE_CN = "five"; Break
Case 6:DATE_CN = "six"; Break
}
Datestr = (1900+date.getyear ()) + "year" + (Date.getmonth () +1) + "month" + date.getdate () + "Day (week" + DATE_CN + ")";
%>
document.write ("<%=dateStr%>");
====== The following is the source document for Test.jsp =============//
<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
Two ways to use <title>include </title>
<jsp:include page= "date.jsp" flush= "true"/>
<%[email protected] include file= "date.jsp"%-->
<body>
<table><tr><td></td></tr></table>
</body>
Use <%@ include file= "date.jsp"%> this situation to introduce date.jsp this document. At this point, an error message appears:
HTTP Status 500–
Org.apache.jasper.JasperException:/date.jsp (0,0) Page Directive:can ' t has multiple occurrences of contentType
The status code is an HTTP 500 server internal error. Then look at the following tips. You cannot specify more than one contenttype in the Date.jsp page.
The reason is here. Because in the translation phase, the code for the date.jsp document was taken to the test.jsp page intact to synthesize a document. The synthesized document will be identical:
<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
This line of code. The solution is to delete this sentence from the date.jsp document. Refresh and then request test.jsp page, normal.
ViewTomcatUnder the temp document,: \tomcat\work\standalone\localhost\testYou'll see in the directoryTest_jsp.javaand Test_jsp.class two documents,
Here the Java document is the JSP container to convert the JSP into a servlet Test_jsp.java this document.
The corresponding Test_jsp.class this document is compiled Test_jsp.java the servlet document generated by the class document. Open the resulting servlet document (Test_jsp.java). At this point we will find that the test.jsp document is converted into a servlet document, in the output of the
Replace the <%@ include file= "date.jsp"%> with <jsp:include page= "dae.jsp" flush= "true"/> Then request test.jsp. This will be seen on the page. The date we introduced the date.jsp output was garbled in Chinese.
So when we introduce a page to the include action element, we actually simply refer to the Servlet class document that date.jsp the document was converted and compiled. Date.jsp is called when the test.jsp document is run as a separate document. Because no character encoding is specified in the date.jsp document. So there was garbled. The solution is to re-remove the
<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
Check out the temporary documents under Tomcat and you'll find out. A Date_jsp.java document and a Date_jsp.class document are present. These two documents come in the same way as Test_jsp.java and Test_ The Jsp.class documentation is the same way. Check the code for the Test_jsp.java document at this point. Add only one line of code at this point:
Jspruntimelibrary.include (Request, Response, "date.jsp", out, true);
It does not introduce the code for the date.jsp document into the test.jsp.
Two usages of include in JSP