javascript
%@ INCLUDE FILE=%与JSP:INCLUDE PAGE=/区别
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
?兩者最重要的區(qū)別:JSP指令<%@ include file=""%>,是將被引入的JSP與原JSP融合到一起,而這個融合過程是在翻譯階段進行的。
??? 為什么需要翻譯階段?我們知道,jsp頁面并不是原封不動的發(fā)送到客戶端的,因為瀏覽JSP頁面的客戶端并不需要安裝Java虛擬機,客戶端機器并不能讀懂JSP,它能讀懂的只有HTML、JavaScript (當然還有其他,例如:Applet、Flex、AxtiveX等等,但那些都需要下載相應的客戶端解析器),這樣就需要Servlet Engine (例如:Tomcat) 將所有的JSP元素進行處理。這是通過將jsp頁面轉化成Servlet,然后執(zhí)行這個Servlet來完成的。服務器需要一個jsp容器來處理jsp頁面。jsp容器通常以Servlet的形式實現(xiàn),這個servlet經過配置,可以處理對jsp頁面的所有請求。
??? JSP容器負責將jsp頁面轉化成servlet,并編譯這個servlet。這兩個步驟就構成了翻譯階段。
??? 而jsp翻譯之后的servlet輸出的內容才是客戶端瀏覽器能夠識別的東西,HTML、JavaScript之類的,servlet是使用JspWriter對象輸出輸出這些HTML、JavaScript的。如果你去翻看翻看jsp編譯后的servlet代碼,你會發(fā)現(xiàn)很有意思的東西,比如Struts的<logic:iterator>標簽,被翻譯成do{}while()語句實現(xiàn)循環(huán)。如果我們把<bean:write>寫在<logic:iterator>內部,則在do的內部會出現(xiàn)類似_jspx_meth_bean_write_2(_jspx_th_logic_iterate_0, _jspx_page_context)的方法調用。這些說明了一切。
??? 由此我們知道:jsp頁面是把include指令元素(<%@ include file=""%>)所指定的頁面的實際內容(也就是代碼段)加入到引入它的jsp頁面中,合成一個文件后被jsp容器將它轉化成servlet。可以看到這時會產生一個臨時class文件和一個servlet源文件。而動作元素(<jsp:include page=""/>)是在請求處理階段引入的,會被JSP容器生成兩個臨時class文件和兩個servlet原文件。而引入的只是servlet的輸出結果,即JspWriter對象的輸出結果,而不是jsp的源代碼。
??? 舉個例子:
??? main.jsp
<%@?page?language="java"?pageEncoding="GBK"%>??<%@?taglib?uri="http://struts.apache.org/tags-bean"?prefix="bean"?%>?? <%@?taglib?uri="http://struts.apache.org/tags-html"?prefix="html"?%>?? <%@?taglib?uri="http://struts.apache.org/tags-logic"?prefix="logic"?%>?? <%@?taglib?uri="http://struts.apache.org/tags-tiles"?prefix="tiles"?%>??<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">?? <html:html?lang="true">??<head>??<html:base?/>??<title>index.jsp</title>??<meta?http-equiv="Content-Type"?content="text/html;?charset=GBK"?/>??<meta?http-equiv="pragma"?content="no-cache">??<meta?http-equiv="cache-control"?content="no-cache">??<meta?http-equiv="expires"?content="0">??????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">??<meta?http-equiv="description"?content="This?is?my?page">??<!--?<link?rel="stylesheet"?type="text/css"?href="styles.css">?-->??</head>??<body>??<!--?Header?Page?Information?-->?? <%--?<%@?include?file="include/head.jsp"%>--%>??<jsp:include?page="include/head.jsp"></jsp:include>??<!--?Nav?Bar?-->?? <%--?<%@?include?file="include/menubar.jsp"%>--%>??<jsp:include?page="include/menubar.jsp"></jsp:include>??我是主體<br><br><br>??我是主體<br><br><br>??我是主體<br><br><br>??我是主體<br><br><br>??我是主體<br><br><br>??我是主體<br><br><br>??我是主體<br><br><br>??我是主體<br><br><br>??我是主體<br><br><br>??我是主體<br><br><br>?? <%--????????<%@?include?file="include/copyright.jsp"%>--%>?? <%--????????<jsp:include?page="/includeSample_copyright.do"></jsp:include>--%>?? <%--?<jsp:include?flush="true"?page="include/copyright.jsp"></jsp:include>--%>??</body>?? </html:html> <%@?page?language="java"?pageEncoding="GBK"%><%@?taglib?uri="http://struts.apache.org/tags-bean"?prefix="bean"?%><%@?taglib?uri="http://struts.apache.org/tags-html"?prefix="html"?%><%@?taglib?uri="http://struts.apache.org/tags-logic"?prefix="logic"?%><%@?taglib?uri="http://struts.apache.org/tags-tiles"?prefix="tiles"?%><!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"><html:html>??<head>????<html:base?/>????????<title>index.jsp</title><meta?http-equiv="Content-Type"?content="text/html;?charset=GBK"?/><meta?http-equiv="pragma"?content="no-cache"><meta?http-equiv="cache-control"?content="no-cache"><meta?http-equiv="expires"?content="0">????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3"><meta?http-equiv="description"?content="This?is?my?page"><!--<link?rel="stylesheet"?type="text/css"?href="styles.css">-->??</head>????<body>????<!--?Header?Page?Information?--><%--<%@?include?file="include/head.jsp"%>--%><jsp:include?page="include/head.jsp"></jsp:include><!--?Nav?Bar?--><%--<%@?include?file="include/menubar.jsp"%>--%><jsp:include?page="include/menubar.jsp"></jsp:include>????我是主體<br><br><br>????我是主體<br><br><br>????我是主體<br><br><br>????我是主體<br><br><br>????我是主體<br><br><br>????我是主體<br><br><br>????我是主體<br><br><br>????我是主體<br><br><br>????我是主體<br><br><br>????我是主體<br><br><br><%--????????<%@?include?file="include/copyright.jsp"%>--%><%--????????<jsp:include?page="/includeSample_copyright.do"></jsp:include>--%><%--<jsp:include?flush="true"?page="include/copyright.jsp"></jsp:include>--%>??</body></html:html>?head.jsp
<%--<%@?page?language="java"?pageEncoding="GBK"%>--%>??<%--<%@?taglib?uri="/WEB-INF/struts-html.tld"?prefix="html"?%>--%>?? <%--<%@?taglib?uri="/WEB-INF/struts-logic.tld"?prefix="logic"?%>--%>?? <%--<%@?taglib?uri="/WEB-INF/struts-bean.tld"?prefix="bean"?%>--%>??<table>??<tr>??<td><bean:message?key="copyright.inc.copyright"/>??我是Head?我是Head我是Head我是Head我是Head我是Head我是Head</td>??</tr>?? </table> <%--<%@?page?language="java"?pageEncoding="GBK"%>--%><%--<%@?taglib?uri="/WEB-INF/struts-html.tld"?prefix="html"?%>--%><%--<%@?taglib?uri="/WEB-INF/struts-logic.tld"?prefix="logic"?%>--%><%--<%@?taglib?uri="/WEB-INF/struts-bean.tld"?prefix="bean"?%>--%><table><tr>????<td><bean:message?key="copyright.inc.copyright"/>???????????????????????我是Head?我是Head我是Head我是Head我是Head我是Head我是Head</td>????</tr></table>?
?以head.jsp為例:
?1、如果我是用<%@ include file="include/head.jsp"%>引入,注意,head.jsp被我注釋掉的字符集和Struts標簽的引入,如果打開注釋,會怎么樣呢??會拋出500異常,/main.jsp(44,4) /include/head.jsp(3,56) Attempt to redefine the prefix html to /WEB-INF/struts-html.tld, when it was already defined as http://struts.apache.org/tags-html in the current scope.這時因為在翻譯階段main.jsp和head.jsp被原封不動的合稱為一個jsp,察看Tomcat工作目錄只有一個servlet類文件。
?????????? \work\Catalina\localhost\IncludeAction\org\apache\jsp\main_jsp.java
???????????\work\Catalina\localhost\IncludeAction\org\apache\jsp\main_jsp.class
試想,在一個類文件中兩次引入相同的Struts標簽,編譯時當然回拋出異常了。
2、那么如果我是以<jsp:include page="include/head.jsp"/>方式引入呢?首先先將head.jap中注釋的Struts標簽的部分打開,而字符集部分仍然注釋。結果出現(xiàn)亂碼:
?
<strong>結果:?? 2004-2006?版權所有??? ????Head?????Head????Head????Head????Head????Head????Head??? ????MenuBar??????? 我是主體...?? </strong> 結果:2004-2006?版權所有?????Head?????Head????Head????Head????Head????Head????Head?????MenuBar?????我是主體...???什么原因?include動作元素是在請求階段執(zhí)行引入的,所以它引入的只是head.jsp被翻譯成servlet文件中_jspService這個方法中JspWriter這個對象的輸出(out.write()方法的輸出流)。該輸出的執(zhí)行是在head.jsp被引入main.jsp之前就進行了,所以main.jsp頁面中的字符集設置當然對head.jsp不起作用了。
????如果Struts標簽部分也注釋掉呢?"2004-2006 版權所有"這一行不會輸出,因為這一行是由Struts標簽輸出的,沒有輸出的原因和字符集相同,我想大家應該明白了。
?? 最后觀察Tomcat工作目錄下,會有兩個Servlet:
?????? 第一個:\work\Catalina\localhost\IncludeAction\org\apache\jsp\main_jsp.java
???????????????????\work\Catalina\localhost\IncludeAction\org\apache\jsp\main_jsp.class
?????? 第二個:\work\Catalina\localhost\IncludeAction\org\apache\jsp\include\head_jsp.java
?????????????????? \work\Catalina\localhost\IncludeAction\org\apache\jsp\include\head_jsp.class
???另外,如果希望通過修改后綴的方法表示哪些是被引入的文件,例如:將head.jsp改名為head.inc的話,JSP容器不能識別*.inc。所以不能翻譯head.inc,所以此時只能使用<%@ include file="head.inc"%>方法引入一個文件了。
?
我們來總結一下兩種include 兩種用法的區(qū)別,主要有兩個方面的不同:?
一、執(zhí)行時間上:?
<%@ include file=”relativeURI”%> 是在翻譯階段執(zhí)行?
<jsp:include page=”relativeURI” flush=”true” /> 在請求處理階段執(zhí)行。?
二、二:引入內容的不同:?
<%@ include file=”relativeURI”%> 引入靜態(tài)文本(html,jsp),在JSP頁面被轉化成servlet之前和它融和到一起。?
<jsp:include page=”relativeURI” flush=”true” /> 引入執(zhí)行頁面或servlet所生成的應答文本。
轉載于:https://my.oschina.net/yongqingfan/blog/620112
總結
以上是生活随笔為你收集整理的%@ INCLUDE FILE=%与JSP:INCLUDE PAGE=/区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]Jenkins实战演练之Windo
- 下一篇: 网络虚拟化是否需要额外的网络架构?