和<%@include%>的區別

<%@include%>和<jsp:include>的區別,發現了一些東西的。 html


<%@include%>:頁面請求以前預編譯,全部代碼包含進來以後,一塊兒進行處理,把全部代碼合在一塊兒,編譯成一個servlet java

<jsp:include>:全部代碼分別處理,在頁面被請求的時候才編譯,被編譯成多個servlet,頁面語法相對獨立,處理完成以後再將代碼的顯示結果(處理結果)組合進來。 apache


JSP中的兩種包含頁面的方法
第一種:include指令:當JSP轉換成Servlet時引入指定文件
jsp

<%@ pagecontentType="text/html;charset=GB2312" language="java"errorPage=""%>
<%@ include file="head.jsp"%>
<%@ include file="body.jsp"%>
<%@ include file="tail.jsp"%>
第二種:<jsp:include>動做元素:當JSP頁面被請求時引入指定文件
<%@ page contentType="text/html; charset=GB2312"language="java" errorPage=""%>
<jsp:include page="head.jsp"/>
<jsp:include page="body.jsp"/>
<jsp:include page="tail.jsp"/>
第二種方法能夠很方便的用<jsp:param>來向所包含頁傳遞參數,方法以下:
<%@ page contentType="text/html; charset=GB2312"language="java" errorPage=""%>
<jsp:include page="head.jsp"/>
<jsp:includepage="body.jsp">
<jsp:param name="uid"value="username"/>
<jsp:param name="pwd"value="password"/>
</jsp:include>
<jsp:includepage="tail.jsp"/>
ui


<jsp:include> :動態包含 spa

 

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

DynamicInclude.jsp: code

<%@pagecontentType="text/html;charset=gb2312"%> <html> <head> <title>動態包含</title> </head> <bodystyle="background-color:lightblue"> <jsp:include page="header.html"flush="true"/><!--動態包含--> <tableborder="1" align="center"> <tr> <td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td> </tr> <tr> <td>a</td><td>b</td><td>c</td><td>d</td> </tr> </table> </body> </html>

 

Header.html : htm

<h2style="font-family:arial;color:red;font-size:25px;text-align:center"> 動態包含的標題(HTML) </h2>

 

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

out.write("\r\n");
     out.write("<html>\r\n");
     out.write("\t<head>\r\n");
     out.write("\t\t<title>動態包含</title>\r\n");
     out.write("\t</head>\r\n");
     out.write("\t<bodystyle=\"background-color:lightblue\">\r\n");
     out.write("\r\n");
     out.write("\t\t");
     <span style="color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.html", out, true);</span>
     out.write("<!--動態包含-->\r\n");
     out.write("\r\n");
     out.write("\t\t<table border=\"1\"align=\"center\">\r\n");
     out.write("\t\t\t<tr>\r\n");
     out.write("\t\t\t\t<td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>\r\n");
     out.write("\t\t\t</tr>\r\n");
     out.write("\t\t\t<tr>\r\n");
     out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");
     out.write("\t\t\t</tr>\r\n");
     out.write("\t\t</table>\r\n");
     out.write("\t</body>\r\n");
     out.write("</html>");

 

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

DynamicInclude.jsp:

<%@pagecontentType="text/html;charset=gb2312"%> <html> <head> <title>動態包含</title> </head> <bodystyle="background-color:lightblue"> <jsp:include page="header.jsp"flush="true"/><!--動態包含--> <tableborder="1" align="center"> <tr> <td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td> </tr> <tr> <td>a</td><td>b</td><td>c</td><td>d</td> </tr> </table> </body> </html>

Header.jsp :

<%@pagecontentType="text/html;charset=gb2312"%> <html> <body> <h2style="font-family:arial;color:red;font-size:25px;text-align:center"> 動態包含的標題(JSP) </h2> </body> </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:

out.write("\r\n");
     out.write("<html>\r\n");
     out.write("\t<head>\r\n");
     out.write("\t\t<title>動態包含</title>\r\n");
     out.write("\t</head>\r\n");
     out.write("\t<bodystyle=\"background-color:lightblue\">\r\n");
     out.write("\r\n");
     out.write("\t\t");
     <span style="color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.jsp", out, true);</span>
     out.write("<!--動態包含-->\r\n");
     out.write("\r\n");
     out.write("\t\t<table border=\"1\"align=\"center\">\r\n");
     out.write("\t\t\t<tr>\r\n");
     out.write("\t\t\t\t<td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>\r\n");
     out.write("\t\t\t</tr>\r\n");
     out.write("\t\t\t<tr>\r\n");
     out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");
     out.write("\t\t\t</tr>\r\n");
     out.write("\t\t</table>\r\n");
     out.write("\t</body>\r\n");
     out.write("</html>");

header_jsp.java:    

out.write("\r\n");
     out.write("<html>\r\n");
     out.write("\t<body>\r\n");
     out.write("\t\t<h2 style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");
     out.write("\t\t\t動態包含的標題(JSP)\r\n");
     out.write("\t\t</h2>\r\n");
     out.write("\t</body>\r\n");
     out.write("</html>");

 

<%@include%>:靜態包含

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

StaticInclude.jsp:

<%@pagecontentType="text/html;charset=gb2312"%> <html> <head> <title>靜態包含</title> </head> <bodystyle="background-color:lightblue"> <%@include file="header.jsp"%><!--靜態包含--> <tableborder="1" align="center"> <tr> <td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td> </tr> <tr> <td>a</td><td>b</td><td>c</td><td>d</td> </tr> </table> </body> </html>

header.jsp:

<%@pagecontentType="text/html;charset=gb2312"%> <h2style="font-family:arial;color:red;font-size:25px;text-align:center"> 靜態包含的標題(JSP) </h2>

 

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

out.write("\r\n");
     out.write("<html>\r\n");
     out.write("\t<head>\r\n");
     out.write("\t\t<title>靜態包含</title>\r\n");
     out.write("\t</head>\r\n");
     out.write("\t<body style=\"background-color:lightblue\">\r\n");
     out.write("\r\n");
     out.write("\t\t");
     out.write("\r\n");
     <span style="color:#ff0000;">out.write("<h2style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");
     out.write("\t靜態包含的標題(JSP)\r\n");
     out.write("</h2>");</span>
     out.write("<!--靜態包含-->\r\n");
     out.write("\t\t<table border=\"1\"align=\"center\">\r\n");
     out.write("\t\t\t<tr>\r\n");
     out.write("\t\t\t\t<td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>\r\n");
     out.write("\t\t\t</tr>\r\n");
      out.write("\t\t\t<tr>\r\n");
     out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");
     out.write("\t\t\t</tr>\r\n");
     out.write("\t\t</table>\r\n");
     out.write("\t</body>\r\n");
     out.write("</html>");

 

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

StaticInclude.jsp:

<%@pagecontentType="text/html;charset=gb2312"%> <html> <head> <title>靜態包含</title> </head> <bodystyle="background-color:lightblue"> <%@include file="header.html"%><!--靜態包含--> <tableborder="1" align="center"> <tr> <td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td> </tr> <tr> <td>a</td><td>b</td><td>c</td><td>d</td> </tr> </table> </body> </html>

header.html:

<%@pagecontentType="text/html;charset=gb2312"%> <h2style="font-family:arial;color:red;font-size:25px;text-align:center"> 靜態包含的標題(HTML) </h2>

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

out.write("\r\n");
     out.write("<html>\r\n");
     out.write("\t<head>\r\n");
     out.write("\t\t<title>靜態包含</title>\r\n");
     out.write("\t</head>\r\n");
     out.write("\t<bodystyle=\"background-color:lightblue\">\r\n");
     out.write("\r\n");
     out.write("\t\t");
     out.write("\r\n");
     <span style="color:#ff0000;">out.write("<h2style=\"font-family:arial;color:red;font-size:25px;text-align:center\">\r\n");
     out.write("\t靜態包含的標題(HTML)\r\n");
     out.write("</h2>");</span>
     out.write("<!--靜態包含-->\r\n");
     out.write("\t\t<table border=\"1\"align=\"center\">\r\n");
     out.write("\t\t\t<tr>\r\n");
     out.write("\t\t\t\t<td>姓名</td><td>性別</td><td>年齡</td><td>愛好</td>\r\n");
     out.write("\t\t\t</tr>\r\n");
     out.write("\t\t\t<tr>\r\n");
     out.write("\t\t\t\t<td>a</td><td>b</td><td>c</td><td>d</td>\r\n");
     out.write("\t\t\t</tr>\r\n");
     out.write("\t\t</table>\r\n");
     out.write("\t</body>\r\n");
     out.write("</html>");

 

由上能夠總結出:

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

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

相關文章
相關標籤/搜索