`
sangmin214
  • 浏览: 176790 次
  • 性别: Icon_minigender_1
  • 来自: 黄山
文章分类
社区版块
存档分类
最新评论

【转】@include和jsp:include的区别

阅读更多

昨天稍微复习了下servlet,也就稍微复习了下jsp,刚才看到这两个东西的用法,然后也记不清他们之间的具体区别了,所以就又复习了一下。

原文地址在:http://geekexplains.blogspot.com/2008/06/diff-between-include-directive-include.html

 

Difference between include directive & include action in JSP

include directive - <%@ include file="fileName" %> - as is the case with all other directives in a JSP page, this include directive is also processed at the time when the JSP page is translated into its equivalent servlet. This directive simply causes the contents of the specified file to be pasted into the JSP page at the place where the page contains this directive. Irrespective of whether the included resource is a static resource or a JSP page - only the contents of the file are pasted (and not the processed results). This directive is normally used for including static resources only - like, banner, header, footer, etc. for the obvious reason.

One common issue with include directive (if it's used to include another JSP) is that the main JSP (which calls this directive to include another JSP) doesn't get re-compiled in the case when the included JSP changes but not the main JSP. Reason being, a JSP is re-compiled only if the contents of the JSP changes. Now in this case, the main JSP (after inclusion) remains the same. Once the include directive has been processed during the translation phase, the main JSP is unaware that part of its contents have come from another file. The entire translated file is treated as a single unit. Any changes to the main JSP will initiate the translation phase and then compilation phase again and hence the specified is included again. So, any modification in the included JSP will not get reflected unless the main JSP is also changed.

include action - <jsp:include page="relativeURL" /> - this include action is executed at run time and the specified 'page' is first executed and then the result of that 'page' is appended to the response object of the calling JSP at the point where the <jsp:include> tag occurs. Obviously if the specified page is a static resource then the contents of it are included as there won't be an executed result in that case. This action allows additional parameters to be passed via <jsp:param> child element of this include action element.

Remember that we can specify only a relative URL i.e., a resource which is in the same context as the calling JSP page. Since the resource is called by passing both the request and response objects, hence the called JSP can access any beans defined in the request using the <jsp:useBean> tag. But, this included resource can't set any HTTP headers. This is the reason why an included resource can't set cookies (as they are carried in the HTTP Headers). Such an attempt will throw an exception.

Difference between include directive & include action

  • The most evident difference is that include directive is processed at the translation time while the include action is processed at the run time.
  • include directive don't require a relative file path (it can be either relative or absolute) whereas the include action requires the page name to be relative to the context of the calling JSP.
  • include action always includes the contents of the specified file irrespective of whether the specified file is a static resource or a dynamic resource whereas include action executed a specified dynamic resource and only the result gets included in the response of the calling JSP. In case of static resource even the include action just includes the contents only as in that case the resource can't be executed.
  • include directive does not ask for any parameters to be passed, which is quite obvious because include directive doesn't execute the specified resource whereas we may specify additional parameters which may be used while execution of the specified resource in case of include action.
  • include directive can't access the objects created in the request scope (in fact, it doesn't need to access any again because it's not going to execute the resource) whereas the include action can access the objects created with the request scope.
分享到:
评论

相关推荐

    JSP:include和include指令区别

    JSP:include和include指令区别,对应我的博客 blog.csdn.net/svitter

    jsp:include与include实探

    NULL 博文链接:https://dreamzhong.iteye.com/blog/1127436

    JSP中include指令和include行为的区别

    通常当应用程序中所有的页面的某些部分(例如标题、页脚和导航栏)都相同的时候,我们就可以考虑用include。具体在哪些时候用&lt; %@ include file=” ”%&gt;,哪些时候用&lt; jsp:include page=” ” flush=...

    jsp基础测试 期末考试

    考虑下面JSP文件代码片断: &lt;jsp:include page=”test2.jsp”&gt; &lt;jsp:param name=”username” value=”accp”/&gt; &lt;/jsp:include&gt; 以下( )代码片断放置在test2.jsp中不会导致错误。

    JSP下动态INCLUDE与静态INCLUDE的区别分析

     用jsp:include动作实现 &lt;jsp include page=”included.jsp” flush&gt;它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数。 静态INCLUDE   用include伪码实现,定不会检查所含文件的变化,...

    2.4 include指令 JSP+AJAX

    2.4 include指令 JSP+AJAX2.4 include指令 JSP+AJAX2.4 include指令 JSP+AJAX2.4 include指令 JSP+AJAX

    JavaWeb开发技术-JSP动作元素.pptx

    在JSP页面和文档中使用写入文本的模板 在JSP页面中,为了把其他资源的输出内容插入到当前JSP页面的输出内容中,JSP技术提供了&lt;jsp:include&gt;动作元素。其语法格式为: &lt;jsp:include page="relativeURL" flush="true|...

    两中包含的区别.txt

    1.&lt;%@include file="head.jsp" %&gt; 2.&lt;jsp:include page="head.jsp" /&gt; 或者: &lt;jsp:include page="jieshou.jsp"&gt; &lt;jsp:param name="canshu" value="magci" /&gt; &lt;/jsp:include&gt; 两者的区别: 1.&lt;%@include ...

    使用jsp:include控制动态内容的方法

    本文实例讲述了使用jsp:include控制动态内容的方法。分享给大家供大家参考,具体如下: 清单 1. JSP include 伪指令 &lt;![CDATA[ &lt;&#37;@ page language=java contentType=text/html %&gt; &lt;html&gt; &lt;head&...

    用jsp:include控制动态内容的方法.docx

    用jsp:include控制动态内容的方法.docx

    可以include的jsp独立分页

    有很详细的注释解释,是中国人都应该看的懂,在你需要的分页的页面嵌入这个jsp,通过修改它的默认属性,和使用它的内置方法,就可以达到分页效果,而且可以有自己的样式效果,以下是例子: 你的页面头顶部加入&lt;%@include ...

    JSP中动态include与静态include的区别介绍

    2、静态include纯粹是把代码写在外面的一种共享方法,所有的变量都是可以和include它的主文件共享,两者高度紧密结合,不能有变量同名的冲突.而页面设置也可以借用主文件的. 静态INCLUDE 用法:&lt;&#37;@ include ...

    Java Web 实验四 掌握<jsp:include>动作的使用

    问题描述: 网上购物系统,商品包括mp3和mp4两项...中使用jsp:include动作包括了选购商品信息item.jsp文件。 实现步骤: 1.创建 index.jsp文件; 2.创建order.jsp文件; 3.创建item.jsp文件; 4.运行结果并完成实验报告。

    jsp的Include方式

    jsp的Include方式

    前端分页jsp实现(二)

    采用jsp开发的前端分页,抽取成一个公共的jsp,在需要的界面采用 &lt;jsp:include&gt;引入就行,白色按钮,黑色选中样式 &lt;div style="display: block;height: 200px;"&gt; &lt;jsp:include page="pagination.jsp" flush="true"&gt;...

    abstract class和interface有什么区别?

    25、JSP中动态INCLUDE与静态INCLUDE的区别?  动态INCLUDE用jsp:include动作实现 &lt;jsp:in clude page="included.jsp" flush="true" / &gt;它总是会检查所含文件中的变化,适合用于包含动 态页面,并且可以带参数。 ...

    jsp页面错误信息

    &lt;tr&gt;&lt;td colspan="2"&gt;&lt;jsp:include page="view/AdminTop.jsp" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;td width="180" valign="top"&gt;&lt;jsp:include page="view/AdminLeft.jsp"/&gt; ...

    jsp的include的两种用法

    jsp的include的两种用法 jsp的include的两种用法

    jsp中include指令静态导入和动态导入的区别详解.docx

    jsp中include指令静态导入和动态导入的区别详解.docx

    springMVC使用jsp:include嵌入页面的两种方法(推荐)

    下面小编就为大家带来一篇springMVC使用jsp:include嵌入页面的两种方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Global site tag (gtag.js) - Google Analytics