<jsp:include>和<%@include%>的区别 .
2014-08-13 17:35
309 查看
转自:/article/2922562.html
前天上了jsp的课,老师讲了一下<%@include%>和<jsp:include>的区别,由于演示的太快,我没有看太清,所以回来之后,自己又写了个例子,好好看看,还是发现了一些东西的。
<jsp:include> :动态包含
第一种情况(<jsp:include>包含的是html文件):
DynamicInclude.jsp:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>动态包含</title>
</head>
<bodystylebodystyle="background-color:lightblue">
<jsp:include page="header.html"flush="true"/><!--动态包含-->
<tablebordertableborder="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 :
[html]
view plaincopyprint?
<h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">
动态包含的标题(HTML)
</h2>
运行之后,只生成一个servlet,和上面的代码对应如下:
[java]
view plaincopyprint?
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:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>动态包含</title>
</head>
<bodystylebodystyle="background-color:lightblue">
<jsp:include page="header.jsp"flush="true"/><!--动态包含-->
<tablebordertableborder="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 :
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<body>
<h2styleh2style="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:
[html]
view plaincopyprint?
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<bodystylebodystyle=\"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:
[html]
view plaincopyprint?
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>");
[b]<%@include%>:静态包含[/b]
第一种情况:<%@include%>包含的是jsp文件。
StaticInclude.jsp:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>静态包含</title>
</head>
<bodystylebodystyle="background-color:lightblue">
<%@include file="header.jsp"%><!--静态包含-->
<tablebordertableborder="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:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">
静态包含的标题(JSP)
</h2>
运行之后,只生成一个servlet,和上面的代码对应如下:
[html]
view plaincopyprint?
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("<h2styleh2style=\"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:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>静态包含</title>
</head>
<bodystylebodystyle="background-color:lightblue">
<%@include file="header.html"%><!--静态包含-->
<tablebordertableborder="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:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">
静态包含的标题(HTML)
</h2>
运行之后,也是只生成一个servlet,和上面的代码对应如下:
[html]
view plaincopyprint?
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<bodystylebodystyle=\"background-color:lightblue\">\r\n");
out.write("\r\n");
out.write("\t\t");
out.write("\r\n");
<SPAN style="COLOR: #ff0000">out.write("<h2styleh2style=\"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>");
由上可以总结出:
1. 对于静态包含,<%@include%>,中包含的文件,只是简单的嵌入到主文件中,就是在jsp页面转化成Servlet时才嵌入到主文件中,因为运行的结果是只生成了一个Servlet。
而对于动态包含<jsp:incude>,如果被包含文件是动态的,那么就会生成两个Servlet,也就是被包含文件也要经过jsp引擎编译执行生成一个Servlet,两个Servlet通过request和reponse进行通信。如果被包含的文件是静态的,那么这种情况和<%@include>就很相似,只生成了一个Servlet,但是他们之间没有进行简单的嵌入,而依然是通过request和reponse进行的通信。
前天上了jsp的课,老师讲了一下<%@include%>和<jsp:include>的区别,由于演示的太快,我没有看太清,所以回来之后,自己又写了个例子,好好看看,还是发现了一些东西的。
<jsp:include> :动态包含
第一种情况(<jsp:include>包含的是html文件):
DynamicInclude.jsp:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>动态包含</title>
</head>
<bodystylebodystyle="background-color:lightblue">
<jsp:include page="header.html"flush="true"/><!--动态包含-->
<tablebordertableborder="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>
<%@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 :
[html]
view plaincopyprint?
<h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">
动态包含的标题(HTML)
</h2>
<h2style="font-family:arial;color:red;font-size:25px;text-align:center"> 动态包含的标题(HTML) </h2>
运行之后,只生成一个servlet,和上面的代码对应如下:
[java]
view plaincopyprint?
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>");
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:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>动态包含</title>
</head>
<bodystylebodystyle="background-color:lightblue">
<jsp:include page="header.jsp"flush="true"/><!--动态包含-->
<tablebordertableborder="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>
<%@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 :
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<body>
<h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">
动态包含的标题(JSP)
</h2>
</body>
</html>
<%@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:
[html]
view plaincopyprint?
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<bodystylebodystyle=\"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>");
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:
[html]
view plaincopyprint?
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>");
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>");
[b]<%@include%>:静态包含[/b]
第一种情况:<%@include%>包含的是jsp文件。
StaticInclude.jsp:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>静态包含</title>
</head>
<bodystylebodystyle="background-color:lightblue">
<%@include file="header.jsp"%><!--静态包含-->
<tablebordertableborder="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>
<%@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:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">
静态包含的标题(JSP)
</h2>
<%@pagecontentType="text/html;charset=gb2312"%> <h2style="font-family:arial;color:red;font-size:25px;text-align:center"> 静态包含的标题(JSP) </h2>
运行之后,只生成一个servlet,和上面的代码对应如下:
[html]
view plaincopyprint?
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("<h2styleh2style=\"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>");
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:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>静态包含</title>
</head>
<bodystylebodystyle="background-color:lightblue">
<%@include file="header.html"%><!--静态包含-->
<tablebordertableborder="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>
<%@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:
[html]
view plaincopyprint?
<%@pagecontentType="text/html;charset=gb2312"%>
<h2styleh2style="font-family:arial;color:red;font-size:25px;text-align:center">
静态包含的标题(HTML)
</h2>
<%@pagecontentType="text/html;charset=gb2312"%> <h2style="font-family:arial;color:red;font-size:25px;text-align:center"> 静态包含的标题(HTML) </h2>
运行之后,也是只生成一个servlet,和上面的代码对应如下:
[html]
view plaincopyprint?
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<bodystylebodystyle=\"background-color:lightblue\">\r\n");
out.write("\r\n");
out.write("\t\t");
out.write("\r\n");
<SPAN style="COLOR: #ff0000">out.write("<h2styleh2style=\"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>");
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>");
由上可以总结出:
1. 对于静态包含,<%@include%>,中包含的文件,只是简单的嵌入到主文件中,就是在jsp页面转化成Servlet时才嵌入到主文件中,因为运行的结果是只生成了一个Servlet。
而对于动态包含<jsp:incude>,如果被包含文件是动态的,那么就会生成两个Servlet,也就是被包含文件也要经过jsp引擎编译执行生成一个Servlet,两个Servlet通过request和reponse进行通信。如果被包含的文件是静态的,那么这种情况和<%@include>就很相似,只生成了一个Servlet,但是他们之间没有进行简单的嵌入,而依然是通过request和reponse进行的通信。
2. <%@include %> 是静态包含,可以包含任何内容。 例如在A.jsp中 <%@ include file="a.txt" %> ,先把 a.txt 中的内容原封不动的包含到A.jsp 中,相当于一个生成一个新的jsp 再交给Servlet 容器(Tomcat)编译。 <jsp:include /> 是动态包含, 例如在B.jsp 中<jsp:include path="c.jsp"/> ,Servlet容器将会先 编译c.jsp ,把c.jsp 的结果包含到B.jsp 中,然后再一起编译 B.jsp
<%@include%>可以包含任意页面,处理动态页面时:先包含,再处理; <jsp:include/>处理动态页面时:先处理,在包含
相关文章推荐
- <jsp:include> 和 <iframe src=""...>的区别
- jsp <%@include%>和<jsp:inclued />的区别
- <%@ include file="" %>和<jsp:include page="" />的区别
- <%@ include file=""%>与<jsp:include page=""/>区别
- <%@ include file=""%>与<jsp:include page=""/>区别
- Include指令和<jsp:include>两种动作表示的区别
- <%@include%> 和 jsp:include 区别
- 浅析<%@include%>和<jsp:include />的区别
- <%@ include file=”relativeURI”%>与<jsp:include page=””>的区别
- <%@ include file=""%>与<jsp:include page=""/>区别
- <%@ include file=""%>与<jsp:include page=""/>区别
- <%@ include > 和 <jsp:include>的区别
- <%@ include file=""%>与<jsp:include page=""/>区别
- include指令与<jsp:include>动作的区别
- <%@ include file=""%>与<jsp:include page=""/>区别
- jsp 静态引入<%@ include %> 动态引入<jsp:include> 区别
- <jsp:include>和<%@include file=""%> 有什么区别
- jsp 静态引入<%@ include %> 动态引入<jsp:include> 区别
- <%include file="a.jsp"%>与<jsp:include file="a.jsp"/>有什么区别?
- <jsp:include>和<%@include file=""%>的区别