国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

<jsp:include>和<%@include%>的區(qū)別,JSP動態(tài)include和靜態(tài)include的區(qū)別

2024-04-27 15:06:28
字體:
供稿:網(wǎng)友

http://www.jb51.net/article/43304.htm

動態(tài)INCLUDE 

用法:<jsp:include page="included.jsp" flush="true" /> 說明:它總是會檢查所含文件中的變化,適合用于包含動態(tài)頁面,并且可以帶參數(shù),先編譯之后再進行處理。 原因:1、靜態(tài)include的結(jié)果是把其他jsp引入當前jsp,兩者合為一體。 2、靜態(tài)include純粹是把代碼寫在外面的一種共享方法,所有的變量都是可以和include它的主文件共享,兩者高度緊密結(jié)合,不能有變量同名的沖突.而頁面設(shè)置也可以借用主文件的. 靜態(tài)INCLUDE 用法:<%@ include file="included.htm" %> 說明:用include偽碼實現(xiàn),不會檢查所含文件的變化,適用于包含靜態(tài)頁面,直接將內(nèi)容先包含后處理。 原因:1、動態(tài)include的結(jié)構(gòu)是兩者獨立,直到輸出時才合并( 看看jsp生成的java文件就可以知道了)。 2、動態(tài)include的jsp文件獨立性很強,是一個單獨的jsp文件,需要使用的對象,頁面設(shè)置,都必須有自己創(chuàng)建,當然,還好它和include它的頁面的request范圍是一致的。

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

http://blog.csdn.net/yangbobo1992/article/details/8085667

<%@include%>:頁面請求之前預(yù)編譯,所有代碼包含進來之后,一起進行處理,把所有代碼合在一起,編譯成一個servlet

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

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

<%@ 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>來向所包含頁傳遞參數(shù),方法如下:<%@ 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"/>

<jsp:include> :動態(tài)包含

 

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

DynamicInclude.jsp:

<%@pagecontentType="text/html;charset=gb2312"%><html>         <head>                   <title>動態(tài)包含</title>         </head>         <bodystyle="background-color:lightblue">                    <jsp:include page="header.html"flush="true"/><!--動態(tài)包含-->                    <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 :

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

 

運行之后,只生成一個servlet,和上面的代碼對應(yīng)如下:

  out.write("/r/n");     out.write("<html>/r/n");     out.write("/t<head>/r/n");     out.write("/t/t<title>動態(tài)包含</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("<!--動態(tài)包含-->/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>動態(tài)包含</title>         </head>         <bodystyle="background-color:lightblue">                    <jsp:include page="header.jsp"flush="true"/><!--動態(tài)包含-->                    <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">                            動態(tài)包含的標題(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文件是兩個相互獨立的整體,它們之間的關(guān)系是通過request和reponse來發(fā)生的,而后者只是簡單的嵌套。兩個servlet對應(yīng)的代碼如下:

DynamicInclude_jsp.java:

     out.write("/r/n");     out.write("<html>/r/n");     out.write("/t<head>/r/n");     out.write("/t/t<title>動態(tài)包含</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("<!--動態(tài)包含-->/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動態(tài)包含的標題(JSP)/r/n");     out.write("/t/t</h2>/r/n");     out.write("/t</body>/r/n");     out.write("</html>");

 

<%@include%>:靜態(tài)包含

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

StaticInclude.jsp:

<%@pagecontentType="text/html;charset=gb2312"%><html>         <head>                   <title>靜態(tài)包含</title>         </head>         <bodystyle="background-color:lightblue">                    <%@include file="header.jsp"%><!--靜態(tài)包含-->                   <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">         靜態(tài)包含的標題(JSP)</h2>

 

運行之后,只生成一個servlet,和上面的代碼對應(yīng)如下:

 out.write("/r/n");     out.write("<html>/r/n");     out.write("/t<head>/r/n");     out.write("/t/t<title>靜態(tài)包含</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靜態(tài)包含的標題(JSP)/r/n");     out.write("</h2>");</span>     out.write("<!--靜態(tài)包含-->/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>靜態(tài)包含</title>         </head>         <bodystyle="background-color:lightblue">                    <%@include file="header.html"%><!--靜態(tài)包含-->                   <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">         靜態(tài)包含的標題(HTML)</h2>

運行之后,也是只生成一個servlet,和上面的代碼對應(yīng)如下:

 out.write("/r/n");     out.write("<html>/r/n");     out.write("/t<head>/r/n");     out.write("/t/t<title>靜態(tài)包含</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靜態(tài)包含的標題(HTML)/r/n");     out.write("</h2>");</span>     out.write("<!--靜態(tài)包含-->/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>");

 

由上可以總結(jié)出:

對于靜態(tài)包含,<%@include%>,中包含的文件,只是簡單的嵌入到主文件中,就是在jsp頁面轉(zhuǎn)化成Servlet時才嵌入到主文件中,因為運行的結(jié)果是只生成了一個Servlet。

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 忻州市| 山阳县| 木兰县| 托里县| 微博| 辰溪县| 浦东新区| 图木舒克市| 北川| 黄浦区| 千阳县| 珲春市| 连城县| 广东省| 新郑市| 徐闻县| 上饶县| 辽阳县| 游戏| 新密市| 河池市| 日照市| 五家渠市| 道真| 铁岭市| 高阳县| 双桥区| 康定县| 慈溪市| 五河县| 天门市| 神木县| 白沙| 湖南省| 朝阳市| 云浮市| 滦平县| 阿尔山市| 浏阳市| 清丰县| 贵州省|