在jsp網頁中,如何於該網頁內部分區域、插入靜態或動態網頁

1.靜態網頁 <%@ include file="…htm" %>

<%@ include file ="test.htm" %>
例:
<html>
     <head>
      <title>JSP include element test</title>
     </head>
     <body>
      This content is statically in the main JSP file.<br />
      <%@ include file="included.html" %>
     </body>
</html>

 
2.可載入靜態,也可以動態網頁 <jsp:include page ="….jsp"/>
   
    不傳遞參數範例:

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


    需傳遞參數範例:
      <jsp:include page ="test.jsp">
          <jsp:param name="file_year" value="2008"/>
          <jsp:param name="file_size" value="1234"/>
      </jsp:include>


   例:

<html>
     <head>
      <title>JSP include element test</title>
     </head>
     <body>
      This content is statically in the main JSP file.<br />
      <jsp:include page="included.html"  />
     </body>
</html>


 
3.將瀏覽器顯示的網頁,導向另一個html或jsp網頁

   不傳遞參數
    <jsp:forward page ="test.jsp"/>

   需傳遞參數
   <jsp:forward page ="test.jsp">
      <jsp:param name="file_year" value="2008"/>
      <jsp:param name="file_size" value="1234"/>
   </jsp:forward>

   例:

<html>
<head><title>test</title></head>
<body>
<%
    String test = (String) session.getAttribute("test");
    if(test == null) {
%>
<jsp:forward page="ERROR.html" />
<%
    }
%>
</body>
</html>


何時該使用載入【靜態網頁】,何時使用載入【動態網頁】?
資料來源(
https://www.ibm.com/developerworks/cn/java/j-jsp04293/)

     <%@ include 虛擬指令在某些網站上有其用武之地。
    例如,如果網站包含一些(如果有變化,也很少)
    幾乎沒有變化的頁眉、頁腳和導航檔,
    那麼基本的 <%@ include 虛擬指令是這些元件的最佳選項。
    由於<%@ include 虛擬指令採用了快取記憶體,
    因此只需放入包含檔一次,其內容就會被快取記憶體,
    其結果會是極大地提高了網站的性能。


    然而,對於現在許多 Web 應用程式或網站而言,
    地毯式的快取記憶體並不能解決問題。雖然頁首和頁尾可能是靜態的,
    但是不可能整個網站都是靜態的。
    例如,從資料庫提取導航連結是很常見的,
    並且許多基於 JSP 技術的網站還從其它網站或應用程式上的動態 JSP 頁面提取內容。
    如果正在處理動態內容,那麼需要採用 jsp:include 來處理該內容。


    2008-11-10 05:59

    arrow
    arrow
      全站熱搜

      味味A 發表在 痞客邦 留言(0) 人氣()