與的不同

<jsp:include> :動態包含

第一種情況(<jsp:include>包含的是html文件):

DynamicInclude.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <head>  
  4.                    <title>動態包含</title>  
  5.          </head>  
  6.          <bodystylebodystyle="background-color:lightblue">  
  7.    
  8.                    <jsp:include page="header.html"flush="true"/><!--動態包含-->  
  9.    
  10.                    <tablebordertableborder="1" align="center">  
  11.                             <tr>  
  12.                                      <td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>  
  13.                             </tr>  
  14.                             <tr>  
  15.                                      <td>a</td><td>b</td><td>c</td><td>d</td>  
  16.                             </tr>  
  17.                    </table>  
  18.          </body>  
  19. </html>  


Header.html :

[html] view plain copy
  1. <h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">  
  2.          動態包含的標題(HTML)  
  3. </h2>  

運行之後,只生成一個servlet,和上面的代碼對應如下:

[java] view plain copy
  1. out.write("\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("\t<head>\r\n");  
  4. out.write("\t\t<title>動態包含</title>\r\n");  
  5. out.write("\t</head>\r\n");  
  6. out.write("\t<bodystyle=\"background-color:lightblue\">\r\n");  
  7. out.write("\r\n");  
  8. out.write("\t\t");  
  9. <span style="color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.html", out, true);</span>  
  10. out.write("<!--動態包含-->\r\n");  
  11. out.write("\r\n");  
  12. out.write("\t\t<table border=\"1\"align=\"center\">\r\n");  
  13. out.write("\t\t\t<tr>\r\n");  
  14. out.write("\t\t\t\t<td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>\r\n");  
  15. out.write("\t\t\t</tr>\r\n");  
  16. out.write("\t\t\t<tr>\r\n");  
  17. out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");  
  18. out.write("\t\t\t</tr>\r\n");  
  19. out.write("\t\t</table>\r\n");  
  20. out.write("\t</body>\r\n");  
  21. out.write("</html>");  

第二種情況(<jsp:include>包含的是jsp文件):

DynamicInclude.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <head>  
  4.                    <title>動態包含</title>  
  5.          </head>  
  6.          <bodystylebodystyle="background-color:lightblue">  
  7.    
  8.                    <jsp:include page="header.jsp"flush="true"/><!--動態包含-->  
  9.    
  10.                    <tablebordertableborder="1" align="center">  
  11.                             <tr>  
  12.                                      <td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>  
  13.                             </tr>  
  14.                             <tr>  
  15.                                      <td>a</td><td>b</td><td>c</td><td>d</td>  
  16.                             </tr>  
  17.                    </table>  
  18.          </body>  
  19. </html>  

Header.jsp :

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <body>  
  4.                    <h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">  
  5.                             動態包含的標題(JSP)  
  6.                    </h2>  
  7.          </body>  
  8. </html>  

運行之後,生成了兩個servlet:DynamicInclude_jsp.Java和header_jsp.java,這也是爲什麼 Header.jsp中要寫上<%@page contentType="text/html;charset=gb2312"%>和完整的<html></html>和<body></body>,而Header.html不用寫的原因。因爲前者兩個.jsp文件是兩個相互獨立的整體,它們之間的關係是通過request和reponse來發生的,而後者只是簡單的嵌套。兩個servlet對應的代碼如下:


DynamicInclude_jsp.java:

[html] view plain copy
  1. out.write("\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("\t<head>\r\n");  
  4. out.write("\t\t<title>動態包含</title>\r\n");  
  5. out.write("\t</head>\r\n");  
  6. out.write("\t<bodystylebodystyle=\"background-color:lightblue\">\r\n");  
  7. out.write("\r\n");  
  8. out.write("\t\t");  
  9. <span style="color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.jsp", out, true);</span>  
  10. out.write("<!--動態包含-->\r\n");  
  11. out.write("\r\n");  
  12. out.write("\t\t<table border=\"1\"align=\"center\">\r\n");  
  13. out.write("\t\t\t<tr>\r\n");  
  14. out.write("\t\t\t\t<td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>\r\n");  
  15. out.write("\t\t\t</tr>\r\n");  
  16. out.write("\t\t\t<tr>\r\n");  
  17. out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");  
  18. out.write("\t\t\t</tr>\r\n");  
  19. out.write("\t\t</table>\r\n");  
  20. out.write("\t</body>\r\n");  
  21. out.write("</html>");  


header_jsp.java:    

[html] view plain copy
  1. out.write("\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("\t<body>\r\n");  
  4. out.write("\t\t<h2 style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");  
  5. out.write("\t\t\t動態包含的標題(JSP)\r\n");  
  6. out.write("\t\t</h2>\r\n");  
  7. out.write("\t</body>\r\n");  
  8. out.write("</html>");  


<%@include%>:靜態包含

第一種情況:<%@include%>包含的是jsp文件。

StaticInclude.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <head>  
  4.                    <title>靜態包含</title>  
  5.          </head>  
  6.          <bodystylebodystyle="background-color:lightblue">  
  7.    
  8.                    <%@include file="header.jsp"%><!--靜態包含-->  
  9.                    <tablebordertableborder="1" align="center">  
  10.                             <tr>  
  11.                                      <td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>  
  12.                             </tr>  
  13.                             <tr>  
  14.                                      <td>a</td><td>b</td><td>c</td><td>d</td>  
  15.                             </tr>  
  16.                    </table>  
  17.          </body>  
  18. </html>  

header.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">  
  3.          靜態包含的標題(JSP)  
  4. </h2>  

運行之後,只生成一個servlet,和上面的代碼對應如下:

[html] view plain copy
  1. out.write("\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("\t<head>\r\n");  
  4. out.write("\t\t<title>靜態包含</title>\r\n");  
  5. out.write("\t</head>\r\n");  
  6. out.write("\t<body style=\"background-color:lightblue\">\r\n");  
  7. out.write("\r\n");  
  8. out.write("\t\t");  
  9. out.write("\r\n");  
  10. <span style="color:#ff0000;">out.write("<h2styleh2style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");  
  11. out.write("\t靜態包含的標題(JSP)\r\n");  
  12. out.write("</h2>");</span>  
  13. out.write("<!--靜態包含-->\r\n");  
  14. out.write("\t\t<table border=\"1\"align=\"center\">\r\n");  
  15. out.write("\t\t\t<tr>\r\n");  
  16. out.write("\t\t\t\t<td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>\r\n");  
  17. out.write("\t\t\t</tr>\r\n");  
  18.  out.write("\t\t\t<tr>\r\n");  
  19. out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");  
  20. out.write("\t\t\t</tr>\r\n");  
  21. out.write("\t\t</table>\r\n");  
  22. out.write("\t</body>\r\n");  
  23. out.write("</html>");  

第二種情況:<%@include%>包含的是html文件。

StaticInclude.jsp:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <html>  
  3.          <head>  
  4.                    <title>靜態包含</title>  
  5.          </head>  
  6.          <bodystylebodystyle="background-color:lightblue">  
  7.    
  8.                    <%@include file="header.html"%><!--靜態包含-->  
  9.                    <tablebordertableborder="1" align="center">  
  10.                             <tr>  
  11.                                      <td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>  
  12.                             </tr>  
  13.                             <tr>  
  14.                                      <td>a</td><td>b</td><td>c</td><td>d</td>  
  15.                             </tr>  
  16.                    </table>  
  17.          </body>  
  18. </html>  

header.html:

[html] view plain copy
  1. <%@pagecontentType="text/html;charset=gb2312"%>  
  2. <h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">  
  3.          靜態包含的標題(HTML)  
  4. </h2>  


運行之後,也是隻生成一個servlet,和上面的代碼對應如下:

[html] view plain copy
  1. out.write("\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("\t<head>\r\n");  
  4. out.write("\t\t<title>靜態包含</title>\r\n");  
  5. out.write("\t</head>\r\n");  
  6. out.write("\t<bodystylebodystyle=\"background-color:lightblue\">\r\n");  
  7. out.write("\r\n");  
  8. out.write("\t\t");  
  9. out.write("\r\n");  
  10. <span style="color:#ff0000;">out.write("<h2styleh2style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");  
  11. out.write("\t靜態包含的標題(HTML)\r\n");  
  12. out.write("</h2>");</span>  
  13. out.write("<!--靜態包含-->\r\n");  
  14. out.write("\t\t<table border=\"1\"align=\"center\">\r\n");  
  15. out.write("\t\t\t<tr>\r\n");  
  16. out.write("\t\t\t\t<td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>\r\n");  
  17. out.write("\t\t\t</tr>\r\n");  
  18. out.write("\t\t\t<tr>\r\n");  
  19. out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");  
  20. out.write("\t\t\t</tr>\r\n");  
  21. out.write("\t\t</table>\r\n");  
  22. out.write("\t</body>\r\n");  
  23. out.write("</html>");  

由上可以總結出:

對於靜態包含,<%@include%>,中包含的文件,只是簡單的嵌入到主文件中,就是在jsp頁面轉化成Servlet時才嵌入到主文件中,因爲運行的結果是隻生成了一個Servlet。

而對於動態包含<jsp:incude>,如果被包含文件是動態的,那麼就會生成兩個Servlet,也就是被包含文件也要經過jsp引擎編譯執行生成一個Servlet,兩個Servlet通過request和reponse進行通信。如果被包含的文件是靜態的,那麼這種情況和<%@include>就很相似,只生成了一個Servlet,但是他們之間沒有進行簡單的嵌入,而依然是通過request和reponse進行的通信。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章