jsp include兩種調用形式詳細

來源:互聯網
上載者:User

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 <%
   }
%>
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.