首先,转发自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("\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""%>
<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("\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""%>
<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("\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""%>
<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("\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进行的通信。

[转]<jsp:include>和<%@include%>的区别的更多相关文章

  1. jsp中的@include与jsp:include区别详解

    1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...

  2. <%@page include%>、<%@include%>、<jsp:include>三者之间的本质区别

    <%@page include%>.<%@include%>.<jsp:include>三者之间的本质区别 先从它的几个内置对象说起. application和se ...

  3. JSP 中动态 INCLUDE 与静态 INCLUDE 的区别?

    一.静态包含指令<%@include file="fileurl"%> 两个jsp页面的<%@page contentType="text/html:c ...

  4. JSP中的include有哪些?有什么区别?

    JSP中的include有哪些?有什么区别? 1.JSP中的include有哪些 (1)<%@include file="" %> (2)<jsp:include ...

  5. jsp中静态include和动态include的区别

    jsp中静态include和动态include的区别 动态 INCLUDE 用 jsp:include 动作实现 <jsp:include page="included.jsp&quo ...

  6. JSP指令include和JSP动作元素include的区别

    include指令用于在JSP页面静态的包含一个文件,该文件可以是JSP页面.HTML页面.文本文件或者一段java代码.使用include指令的JSP页面在转换时,JSP容器会在其中插入所包含文件的 ...

  7. <jsp:include>和<%@include file=""%>的区别(简单了解)

    简单了解 include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改,否则视为已被修改.由于被包含的文件是在编译时才插入的 ...

  8. 牛客网Java刷题知识点之什么是JSP的3大常用指令、JSP的6大哪些动作、JSP中include指令和include动作有什么区别

    不多说,直接上干货! JSP的3大常用指令 包含指令(Include directive):用来包含文件和合并文件内容到当前的页面. 页面指令(Page directive):用来定义JSP页面中特定 ...

  9. JSP中动态include和静态include区别

    静态include(<%@ include file=""%>): 静态include(静态导入)是指将一个其他文件(一个jsp/html)嵌入到本页面 jsp的inc ...

随机推荐

  1. 将UIview描画成虚线等.

    - (UIView *)lineView{ if (!_lineView) { _lineView = [UIView new]; // _lineView.backgroundColor = UIC ...

  2. 对象映射工具AutoMapper介绍

    AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间. 一. AutoMapper ...

  3. Android:开发环境搭建相关问题

    1.Android.ADT.SDK概念与关系 SDK:Software Development Kit,中文意思是“软件开发工具包”.这是一个覆盖面相当广泛的名词,可以这么说:辅助开发某一类软件的相关 ...

  4. qt5.4.0编译错误

    error1: 进程"C:\Qt\Qt5.4.0\Tools\QtCreator\bin\jom.exe"退出,退出代码 2 solution:去工具->选项->构建和 ...

  5. WPF 自定义绕圈进度条

    在设计界面时,有时会遇到进度条,本次讲解如何设计自定义的绕圈进度条,直接上代码: 1.控件界面 <UserControl x:Class="ProgressBarControl&quo ...

  6. Log4J基础详解及示例大全

    去年这个时候,为做软件工程的大作业就详细学过Log4J的用法了,时隔一年想要在新的项目中好好使用一下的时候,发现几乎全忘了,悲催啊-- 再上网查资料,总是不能找到一篇符合我的口味,拿来就能轻松上手,方 ...

  7. 【转】What is an SDET? Part 2 – Skill Matrix of SDET

    What is an SDET? Part 2 ---- Skill Matrix of SDET (Instead of naming it as part 2 of What is an SDET ...

  8. 《The Elder Scrolls V: Skyrim》百般冷门却强力职业

    <The Elder Scrolls V: Skyrim>百般冷门却强力职业 1.有如成龙平常的杂耍型战斗窃贼 每次看帖都察觉大伙一贯在强调窃贼不需要防御,窃贼不需要血,窃贼就是一击致命, ...

  9. 1.认识EJB

    什么是EJB?1. Enterprice JavaBeans(EJB)是一个用于分布式业务应用的标准服务端组件模型. . 采用EJB架构编写的应用是可伸的.事务性的.多用户安全的. . 采用EJB编写 ...

  10. AR For Unity3D之HiAR分析

     前言 关于AR和Unity的基础知识,请自行前往各自的文档中心进行科普. 本文以国产的HiAR SDK为例,日后将尝试高通的vuforia SDK  我的环境 基于Hi AR1.2.0 ( hiar ...