jsp:include和@include的区别

2022-11-30 18:58:56 浏览数 (1)

代码语言:javascript复制
<jsp:include> :动态包含

1、<jsp:include>包含的是html文件

举例:

DynamicInclude.jsp:

代码语言:javascript复制
<%@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 :

代码语言:javascript复制
<h2style="font-family:arial;color:red;font-size:25px;text-align:center">
         动态包含的标题(HTML)
</h2>

运行之后,只生成一个servlet,和上面的代码对应如下:

代码语言:javascript复制
 out.write("rn");
 out.write("<html>rn");
 out.write("t<head>rn");
 out.write("tt<title>动态包含</title>rn");
 out.write("t</head>rn");
 out.write("t<bodystyle="background-color:lightblue">rn");
 out.write("rn");
 out.write("tt");
 org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.html", out, true);
 out.write("<!--动态包含-->rn");
 out.write("rn");
 out.write("tt<table border="1"align="center">rn");
 out.write("ttt<tr>rn");
 out.write("tttt<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>rn");
 out.write("ttt</tr>rn");
 out.write("ttt<tr>rn");
 out.write("tttt<td>a</td><td>b</td><td>c</td><td>d</td>rn");
 out.write("ttt</tr>rn");
 out.write("tt</table>rn");
 out.write("t</body>rn");
 out.write("</html>");

2、<jsp:include>包含的是jsp文件 举例: DynamicInclude.jsp:

代码语言:javascript复制
<%@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 :

代码语言:javascript复制
<%@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”%>和完整的和,而Header.html不用写的原因。因为前者两个.jsp文件是两个相互独立的整体,它们之间的关系是通过request和reponse来发生的,而后者只是简单的嵌套。两个servlet对应的代码如下:

DynamicInclude_jsp.java:

代码语言:javascript复制
 out.write("rn");
 out.write("<html>rn");
 out.write("t<head>rn");
 out.write("tt<title>动态包含</title>rn");
 out.write("t</head>rn");
 out.write("t<bodystyle="background-color:lightblue">rn");
 out.write("rn");
 out.write("tt");
 org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.jsp", out, true);
 out.write("<!--动态包含-->rn");
 out.write("rn");
 out.write("tt<table border="1"align="center">rn");
 out.write("ttt<tr>rn");
 out.write("tttt<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>rn");
 out.write("ttt</tr>rn");
 out.write("ttt<tr>rn");
 out.write("tttt<td>a</td><td>b</td><td>c</td><td>d</td>rn");
 out.write("ttt</tr>rn");
 out.write("tt</table>rn");
 out.write("t</body>rn");
 out.write("</html>");

header_jsp.java:

代码语言:javascript复制
 out.write("rn");
 out.write("<html>rn");
 out.write("t<body>rn");
 out.write("tt<h2 style="font-family:arial;color:red;font-size:25px;text-align:center">rn");
 out.write("ttt动态包含的标题(JSP)rn");
 out.write("tt</h2>rn");
 out.write("t</body>rn");
 out.write("</html>");

<%@include%>:静态包含 1、<%@include%>包含的是jsp文件

StaticInclude.jsp:

代码语言:javascript复制
<%@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:

代码语言:javascript复制
<%@pagecontentType="text/html;charset=gb2312"%>
<h2style="font-family:arial;color:red;font-size:25px;text-align:center">
         静态包含的标题(JSP)
</h2>

运行之后,只生成一个servlet,和上面的代码对应如下:

代码语言:javascript复制
 out.write("rn");
 out.write("<html>rn");
 out.write("t<head>rn");
 out.write("tt<title>静态包含</title>rn");
 out.write("t</head>rn");
 out.write("t<body style="background-color:lightblue">rn");
 out.write("rn");
 out.write("tt");
 out.write("rn");
 out.write("<h2style="font-family:arial;color:red;font-size:25px;text-align:center">rn");
 out.write("t静态包含的标题(JSP)rn");
 out.write("</h2>");
 out.write("<!--静态包含-->rn");
 out.write("tt<table border="1"align="center">rn");
 out.write("ttt<tr>rn");
 out.write("tttt<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>rn");
 out.write("ttt</tr>rn");
  out.write("ttt<tr>rn");
 out.write("tttt<td>a</td><td>b</td><td>c</td><td>d</td>rn");
 out.write("ttt</tr>rn");
 out.write("tt</table>rn");
 out.write("t</body>rn");
 out.write("</html>");

2、<%@include%>包含的是html文件。

StaticInclude.jsp:

代码语言:javascript复制
<%@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:

代码语言:javascript复制
<%@pagecontentType="text/html;charset=gb2312"%>
<h2style="font-family:arial;color:red;font-size:25px;text-align:center">
         静态包含的标题(HTML)
</h2>

运行之后,也是只生成一个servlet,和上面的代码对应如下:

代码语言:javascript复制
 out.write("rn");
 out.write("<html>rn");
 out.write("t<head>rn");
 out.write("tt<title>静态包含</title>rn");
 out.write("t</head>rn");
 out.write("t<bodystyle="background-color:lightblue">rn");
 out.write("rn");
 out.write("tt");
 out.write("rn");
 out.write("<h2style="font-family:arial;color:red;font-size:25px;text-align:center">rn");
 out.write("t静态包含的标题(HTML)rn");
 out.write("</h2>");
 out.write("<!--静态包含-->rn");
 out.write("tt<table border="1"align="center">rn");
 out.write("ttt<tr>rn");
 out.write("tttt<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>rn");
 out.write("ttt</tr>rn");
 out.write("ttt<tr>rn");
 out.write("tttt<td>a</td><td>b</td><td>c</td><td>d</td>rn");
 out.write("ttt</tr>rn");
 out.write("tt</table>rn");
 out.write("t</body>rn");
 out.write("</html>");

由上可以总结出:

对于静态包含,<%@include%>,中包含的文件,只是简单的嵌入到主文件中,就是在jsp页面转化成Servlet时才嵌入到主文件中,因为运行的结果是只生成了一个Servlet。

而对于动态包含<jsp:incude>,如果被包含文件是动态的,那么就会生成两个Servlet,也就是被包含文件也要经过jsp引擎编译执行生成一个Servlet,两个Servlet通过request和reponse进行通信。如果被包含的文件是静态的,那么这种情况和<%@include>就很相似,只生成了一个Servlet,但是他们之间没有进行简单的嵌入,而依然是通过request和reponse进行的通信。

在jsp中有两种包含,静态包含<%@include file=“xxx.jsp”%>和动态包含<jsp:include page=“xxx.jsp”>,下面说一下它们之间的区别

1、<%@include file=“xxx.jsp”%>为jsp中的编译指令,其文件的包含是发生在jsp向servlet转换的时期,而<jsp:include page=“xxx.jsp”>是jsp中的动作指令,其文件的包含是发生在编译时期,也就是将java文件编译为class文件的时期

2、使用静态包含只会产生一个class文件,而使用动态包含会产生多个class文件

3、使用静态包含,包含页面和被包含页面的request对象为同一对象,因为静态包含只是将被包含的页面的内容复制到包含的页面中去;而动态包含包含页面和被包含页面不是同一个页面,被包含的页面的request对象可以取到的参数范围要相对大些,不仅可以取到传递到包含页面的参数,同样也能取得在包含页面向下传递的参数

0 人点赞