(轉)JSP的董濤包含和靜態包含

在網上看到很多說法:

jsp靜態包含和動態包含的區別:
  1. 動態INCLUDE 用jsp:include 動作實現。   
  2. <jsp:include page="included.jsp"  
  3. flush="true" />它總是會檢查所含文件中的變化,適合用於包含動態頁面,並   
  4. 且可以帶參數   
  5. 靜態INCLUDE 用include 僞碼實現,定不會檢查所含文件的變化,適用於包   
  6. 含靜態頁面:<%@ include file="included.htm" %>  

可是經本人驗證這種說話是不完全正確的,至少動態<jsp:inlude>指令是可以包含動態頁面的。

個人認爲區別是:

1<jsp:inlude>在同一個頁面中可以包含儘可能多的條數,不管這個頁面中有什麼內容,只要頁面本身合法就行,而<%@ include>僞指令如果被包含頁面有定義變量和方法的話只能包含一條。

(這個是和第二條的編譯方式相一致的)

2動態包含在請求到來時編譯包含頁面和被包含頁面,如果都是jsp頁面,那麼將生成倆個頁面對應的class文件和java文件。而靜態包含只會生成包含頁面的java文件和類文件。

 

3所謂動態包含是指在請求包含頁面的時候遇到動態包含指令將請求轉到被包含頁面,這時去編譯被包含頁面。靜態包含是在請求包含頁面時去編譯包含頁面,編譯時遇到靜態頁面包含僞碼將被包含頁面的內容複製到被包含頁面中進行編譯。

4<jsp:inlude >指令時相對包含頁面的被包含文件路徑,但靜態包含是相對被包含文件路徑的。(這一點孫鑫老師在《java web 深入詳解》中講的很清楚)

5引用被包含頁面的範圍屬性時動態包含的指令是與位置相關的,即在<jsp:include>指令之前引用被包含頁面中設置的屬性值是無效的。但是靜態包含是不區分包含指令的位置的,可以在包含指令之前引用被包含頁面設置的屬性,是有效的。

倆者是有相同點的:

1 都可以進行交互,request範圍對象中的屬性包含頁和被包含頁之間可以交互使用。

2被包含頁面中引用包含頁面設置的屬性時倆者都和設置包含頁面中範圍屬性的值有關,即在包含被包含頁面之前設置的範圍屬性纔有效。

代碼如下: 包含頁面: index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" buffer="23kb"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>this is 'index.jsp' </title>

</head>

<body> 
這是包含頁面<br/>

<%--靜態包含前引用被包含頁面設置的屬性 --%>
${name }靜態包含前引用被包含頁面設置的屬性<br/>
<%--包含頁面設置屬性 --%>
   
   <%request.setAttribute("pws","456") ;%>

   <%--<%@include file="MyJsp.jsp"%>此處因爲MyJsp.jsp中定義了變量,固不可以重複包含 --%>
   <%-- --%><%@include file="MyJsp.jsp" %>
   
    ${name }包含靜態頁面之後引用屬性<br/>
   <%--此處沒有you.jsp中沒有定義變量,所以可以重複包含 --%>
   ${wangzhanming }<br/>
   <%@include file="you.jsp" %>
   <%@include file="you.jsp" %>
   ${wangzhanming }<br/>
  

<jsp:include page="MyJsp.jsp" ></jsp:include>
<jsp:include page="MyJsp.jsp" ></jsp:include>

 

<%request.setAttribute("pws","lyx") ;%>
<%--此處可以重複包含--%>
   <jsp:include page="MyJsp.jsp" ></jsp:include>
   <%@include file="you.jsp" %>
   ${name }<br/>
</body>
</html>
設置變量的包含頁面: MyJsp.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%--定義變量 --%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    
    <title>this is 'MyJsp.jsp' </title>
    
<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>
這是定義變量的包含頁面<br/>
<%--被包含頁面中設置範圍屬性 --%>
<%request.setAttribute("name","wzm"); %>
    ${name } <br>
    <%--被包含頁面中引用包含頁面的屬性 --%>
    ${pws }<br/>
</body>
</html>

不包含變量設置的被包含頁面: you.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>

    
    <title>this is 'you.jsp' </title>
    
<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>
    這是不定義變量的被包含頁面 <br>
    <%request.setAttribute("wangzhanming","haoren"); %>
    ${wangzhanming }<br/>
    ${pws }<br/>
</body>
</html>

發佈了56 篇原創文章 · 獲贊 0 · 訪問量 1483
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章