zoukankan      html  css  js  c++  java
  • [转]<jsp:include>和<%@include%>的区别

    首先,转发自yangbobo1992

    <jsp:include>和<%@include%>的区别

    这个是我见过写的最好的之一

    <%@include%>和<jsp:include>的区别,发现了一些东西的。

    首先说说对这两句话的理解

    <%@ include file=""%>
    <jsp:include page=""/>
    是这两个吧?
    共同点:两者都是在请求时包含另一个页面进来!
    不同点:
    前者:不合适包含动态页面,因为用它包含动态页面时,它并不会去检查所包含文件所发生的变化!所以适应于html页面,不能传任何参数。
    后者:合适包含动态页面,它会去定时检查所包含文件的变化,适应用jsp等动态页面,并且可以传参数

    我对上面的理解是include指令包含文件就是单纯的文件合并,合并城一个文件jsp文件然后编译成一个文件,这种包含之后被包含的文件如果发生变化,比如在生产环境中替换了一个jsp此时和没有替换是一样的,因为没有再次编译

    include动作就不一样了,它会生成各自编译成一个servlet文件(猜测是一个servlet文件),然后通过request和response再把jsp中的内容传给要合如的jsp(即就是编译后的sevlet文件),同样在生产环境中替换了一个被包含的jsp那么当这种变化被检测到时应该就会再合入一次(纯属个人推理)


    <%@include%>:页面请求之前预编译,所有代码包含进来之后,一起进行处理,把所有代码合在一起,编译成一个servlet

    <jsp:include>:所有代码分别处理,在页面被请求的时候才编译,被编译成多个servlet,页面语法相对独立,处理完成之后再将代码的显示结果(处理结果)组合进来。


    JSP中的两种包含页面的方法
    第一种:include指令:当JSP转换成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>来向所包含页传递参数,方法如下:
    <%@ 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> :动态包含

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

    DynamicInclude.jsp:

    @pagecontentType""%>
    <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 :

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

     

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

      out.write("
    ");
         out.write("<html>
    ");
         out.write("	<head>
    ");
         out.write("		<title>动态包含</title>
    ");
         out.write("	</head>
    ");
         out.write("	<bodystyle="background-color:lightblue">
    ");
         out.write("
    ");
         out.write("		");
         <span style="color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.html", out, true);</span>
         out.write("<!--动态包含-->
    ");
         out.write("
    ");
         out.write("		<table border="1"align="center">
    ");
         out.write("			<tr>
    ");
         out.write("				<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>
    ");
         out.write("			</tr>
    ");
         out.write("			<tr>
    ");
         out.write("				<td>a</td><td>b</td><td>c</td><td>d</td>
    ");
         out.write("			</tr>
    ");
         out.write("		</table>
    ");
         out.write("	</body>
    ");
         out.write("</html>");

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

    DynamicInclude.jsp:

    @pagecontentType""%>
    <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""%>
    <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("
    ");
         out.write("<html>
    ");
         out.write("	<head>
    ");
         out.write("		<title>动态包含</title>
    ");
         out.write("	</head>
    ");
         out.write("	<bodystyle="background-color:lightblue">
    ");
         out.write("
    ");
         out.write("		");
         <span style="color:#ff0000;">org.apache.jasper.runtime.JspRuntimeLibrary.include(request,response, "header.jsp", out, true);</span>
         out.write("<!--动态包含-->
    ");
         out.write("
    ");
         out.write("		<table border="1"align="center">
    ");
         out.write("			<tr>
    ");
         out.write("				<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>
    ");
         out.write("			</tr>
    ");
         out.write("			<tr>
    ");
         out.write("				<td>a</td><td>b</td><td>c</td><td>d</td>
    ");
         out.write("			</tr>
    ");
         out.write("		</table>
    ");
         out.write("	</body>
    ");
         out.write("</html>");

    header_jsp.java:    

         out.write("
    ");
         out.write("<html>
    ");
         out.write("	<body>
    ");
         out.write("		<h2 style="font-family:arial;color:red;font-size:25px;text-align:center">
    ");
         out.write("			动态包含的标题(JSP)
    ");
         out.write("		</h2>
    ");
         out.write("	</body>
    ");
         out.write("</html>");

    <%@include%>:静态包含

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

    StaticInclude.jsp:

    @pagecontentType""%>
    <html>
             <head>
                       <title>静态包含</title>
             </head>
             <bodystyle="background-color:lightblue">
     
                       @include file""<!--静态包含-->
                       <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""%>
    <h2style="font-family:arial;color:red;font-size:25px;text-align:center">
             静态包含的标题(JSP)
    </h2>

     

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

     out.write("
    ");
         out.write("<html>
    ");
         out.write("	<head>
    ");
         out.write("		<title>静态包含</title>
    ");
         out.write("	</head>
    ");
         out.write("	<body style="background-color:lightblue">
    ");
         out.write("
    ");
         out.write("		");
         out.write("
    ");
         <span style="color:#ff0000;">out.write("<h2style="font-family:arial;color:red;font-size:25px;text-align:center">
    ");
         out.write("	静态包含的标题(JSP)
    ");
         out.write("</h2>");</span>
         out.write("<!--静态包含-->
    ");
         out.write("		<table border="1"align="center">
    ");
         out.write("			<tr>
    ");
         out.write("				<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>
    ");
         out.write("			</tr>
    ");
          out.write("			<tr>
    ");
         out.write("				<td>a</td><td>b</td><td>c</td><td>d</td>
    ");
         out.write("			</tr>
    ");
         out.write("		</table>
    ");
         out.write("	</body>
    ");
         out.write("</html>");

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

    StaticInclude.jsp:

    @pagecontentType""%>
    <html>
             <head>
                       <title>静态包含</title>
             </head>
             <bodystyle="background-color:lightblue">
     
                       @include file""<!--静态包含-->
                       <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""%>
    <h2style="font-family:arial;color:red;font-size:25px;text-align:center">
             静态包含的标题(HTML)
    </h2>

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

     out.write("
    ");
         out.write("<html>
    ");
         out.write("	<head>
    ");
         out.write("		<title>静态包含</title>
    ");
         out.write("	</head>
    ");
         out.write("	<bodystyle="background-color:lightblue">
    ");
         out.write("
    ");
         out.write("		");
         out.write("
    ");
         <span style="color:#ff0000;">out.write("<h2style="font-family:arial;color:red;font-size:25px;text-align:center">
    ");
         out.write("	静态包含的标题(HTML)
    ");
         out.write("</h2>");</span>
         out.write("<!--静态包含-->
    ");
         out.write("		<table border="1"align="center">
    ");
         out.write("			<tr>
    ");
         out.write("				<td>姓名</td><td>性别</td><td>年龄</td><td>爱好</td>
    ");
         out.write("			</tr>
    ");
         out.write("			<tr>
    ");
         out.write("				<td>a</td><td>b</td><td>c</td><td>d</td>
    ");
         out.write("			</tr>
    ");
         out.write("		</table>
    ");
         out.write("	</body>
    ");
         out.write("</html>");

    由上可以总结出:

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

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

  • 相关阅读:
    HttpClient-----待补充
    JDK8的新特性
    关于日期转换的知识点(SimpleDateFormat)
    mybatis中的增删改查操作
    mybatis的快速入门
    018 HDFS中,namenode与datanode的交互
    Unit的各种断言
    分组数据
    Javassist学习总结
    hibernate Validator 6.X 的学习,bean的约束(字段,get方法上的验证)
  • 原文地址:https://www.cnblogs.com/rocky-AGE-24/p/5933568.html
Copyright © 2011-2022 走看看