include有兩種形式,分別是Include指令:<%@ include file=""%>和include動作:<jsp教程:include page="" flush="true"/>
include調用檔案
<%@ include file=""%>,是將被引入的JSP與原JSP融合到一起,而這個融合過程是在翻譯階段進行的
index.jsp
<%@ page session="false" %>
<h3>Flavors</h3>
Our most popular flavors are:
<%@ include file="flavor_list.html" %>
Try them all!
flavor_list.html
<ol>
<li>Chocolate</li>
<li>Strawberry</li>
<li>Vanilla</li>
</ol>
常當應用程式中所有的頁面的某些部分(例如標題、頁尾和導覽列)都相同的時候,我們就可以考慮用include。具體在哪些時候用<%@ include file=""%>,哪些時候用<jsp:include page="" flush="true"/>。這種形式
include一個頁面的地址
<%@ page session="false" %>
<h3>Flavors</h3>
Our most popular flavors are:
<jsp:include page="/" flush="true"/>
Try them all!
根據使用者提交的參數請求,我們調用不用的檔案
執行個體
<%
// Diameter of the earth in kilometers
int distance = 12756;
%>
<%@ page session="false" %>
<h4>Diameter of the Earth in SI (Metric) Units</h4>
<jsp:include page="ShowDiameter.jsp" flush="true">
<jsp:param name="dist" value="<%= distance %>" />
<jsp:param name="units" value="SI" />
</jsp:include>
<h4>Diameter of the Earth in U.S. Customary Units</h4>
<jsp:include page="ShowDiameter.jsp" flush="true">
<jsp:param name="dist" value="<%= distance %>" />
<jsp:param name="units" value="US" />
</jsp:include>
ShowDiameter.jsp
<%@ page session="false"%>
<%
String dist = request.getParameter("dist");
if (dist == null)
throw new ServletException
("No distance parameter specified");
int kilometers = Integer.parseInt(dist);
double miles = kilometers / 1.609344;
String units = request.getParameter("units");
if (units == null)
throw new ServletException
("No units parameter specified");
if (units.equals("SI")) {
%> Diameter = <%= kilometers %> km <%
}
else {
%> Diameter = <%= miles %> miles <%
}
%>