`
sangmin214
  • 浏览: 179226 次
  • 性别: 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指令区别

    在JavaServer Pages (JSP) 技术中,`&lt;jsp:include&gt;` 和 `&lt;%@ include %&gt;` 是两个用于页面组合的指令,它们虽然都用于将一个或多个文件的内容插入到主页面中,但它们的工作机制和使用场景有所不同。理解这两者的区别...

    jsp:include与include实探

    `jsp:include`和`&lt;jsp:include&gt;`的一个关键区别在于处理动态内容的能力。`jsp:include`适合于包含动态内容,因为它会在每次请求时重新计算;而`&lt;jsp:include&gt;`则适用于包含静态内容,因为它在页面编译时就完成了包含...

    jsp的include的两种用法

    JSP 中的 Include 有两种用法,分别是 `&lt;%@ include file=” ”%&gt;` 和 `&lt;jsp:include page=” ” flush=”true”/&gt;`。这两种用法都可以用于引入其他 JSP 文件,但是它们之间存在着一些关键的区别。 首先,让我们...

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

    在实际开发中,合理地利用`&lt;jsp:include&gt;`和`&lt;%@include file="..."%&gt;`可以帮助优化代码结构和提高代码的可维护性。静态内容(如页眉、页脚、样式表和脚本)通常适合使用`&lt;%@include file="..."%&gt;`,因为它们不需要...

    jsp中两种包含关系(include动作和include指令)的区别

    在JavaServer Pages (JSP) 技术中,存在两种包含关系:`&lt;jsp:include&gt;` 动作和 `jsp:include` 指令。它们都是用来将一个页面的内容插入到另一个页面中的,但它们的工作方式和适用场景有所不同。 **1. `&lt;jsp:include...

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

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

    jsp的Include方式

    在JSP中,`Include` 方式是实现页面组合的重要技术,它允许我们把多个页面的内容合并到一个主页面中,从而实现代码复用和页面布局的灵活管理。本文将深入探讨JSP的`Include` 方式的概念、类型以及使用方法。 1. **...

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

    在Java Web开发中,`&lt;jsp:include&gt;`动作是一个非常重要的元素,用于动态地将一个页面包含到另一个页面中。这个动作使得开发者可以实现页面的重用和模块化设计,提高代码的可维护性。本实验的目标是让学生掌握`&lt;jsp:...

    十三、JSP动作

    JSP动作包括: jsp:include:在页面被请求的时候引入一个文件。 jsp:useBean:寻找或者实例化一个JavaBean。 jsp:setProperty:设置JavaBean的属性。 jsp:getProperty:输出某个JavaBean的属性。 jsp:forward:把...

    jsp include文件时的一个乱码解决方法.docx

    在 JSP 中,include 文件可以使用两种方式来包含,即 `@include` 方式和 `jsp:include` 方式。其中,`@include` 方式可以使得包含页面和被包含页面在编译时被编译成一个文件,从而使得变量可以相互通用。但是,这种...

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

    JSP 中 Include 指令静态导入和动态导入的区别详解 JSP 中的 Include 指令可以实现静态导入和动态导入两种方式。静态导入和动态导入是 JSP 中 Include 指令的两种不同的实现方式,它们之间有着明显的区别。 静态...

    include 两种用法的区别

    ### JSP Include 两种用法的区别 在JSP(JavaServer Pages)开发中,`include`指令被广泛用于将一个JSP页面的内容嵌入到另一个JSP页面中。这种技术可以提高代码的重用性、简化维护过程并有助于实现更灵活的设计。在...

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

    从技术角度上,jsp:include动作和include指令的主要区别在于:jsp:include是在运行时动态地包含另一个文件,而include指令是在JSP页面被转换成Servlet的时候静态地包含另一个文件。因此,jsp:include动作支持动态...

    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伪码实现,定不会检查所含文件的变化,...

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

    总结来说,Spring MVC中的`jsp:include`提供了静态和动态两种方式来嵌入子页面。静态嵌入适合不需动态变化的内容,而动态嵌入则适用于需要根据用户行为或系统状态变化的场景。理解这两种方法的区别和应用场景对于...

    j2ee15:jsp04,session的访问控制,分页查询,page include·

    有两种包含方式:`&lt;jsp:include&gt;`标签和`&lt;%@include%&gt;`指令。前者在每次请求时都包含页面,适合动态内容;后者在编译时静态合并,适用于不变的静态内容。例如,我们可以用`&lt;jsp:include page="header.jsp" /&gt;`来包含...

    2.4 include指令 JSP+AJAX

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

    JSP基础语法

    include 指令有两种形式:@include 指令和 &lt;jsp:include&gt; 指令。 3.4.1 @include 指令 @example: Includedemo01.jsp 3.4.2 &lt;jsp:include&gt; 指令 @example: Includedemo02.jsp、includedemo03.jsp 3.4.3 两种包含...

Global site tag (gtag.js) - Google Analytics