和有什么不一样?和 <%@ 包含文件 = ...>?

发布于 12-11 09:29 字数 65 浏览 0 评论 0 原文

这两个标签都包含一个页面中另一页面的内容。

那么这两个标签之间的确切区别是什么呢?

Both tags include the content from one page in another.

So what is the exact difference between these two tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

喜你已久 2024-12-18 09:29:08

在一段可重用的代码中,我使用 指令 <%@include file="reuse.html"%> 在第二个我使用标准操作

让可重用文件中的代码为:

<html>
<head>
    <title>reusable</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <img src="candle.gif" height="100" width="50"/> <br />
    <p><b>As the candle burns,so do I</b></p>
</body>

运行这两个 JSP 文件后,您会看到相同的输出,并思考指令操作标记之间是否有任何差异。但如果您查看这两个 JSP 文件生成的 servlet,您就会发现差异。

以下是您在使用指令时将看到的内容:

out.write("<html>\r\n");
out.write("    <head>\r\n");
out.write("        <title>reusable</title>\r\n");
out.write("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("    </head>\r\n");
out.write("    <body>\r\n");
out.write("        <img src=\"candle.gif\" height=\"100\" width=\"50\"/> <br />\r\n");
out.write("        <p><b>As the candle burns,so do I</b></p>\r\n");
out.write("    </body>\r\n");
out.write("</html>\r\n");
 

这是您将在使用的标准操作中看到的内容em> 在第二个 JSP 文件中:

org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "reusable.html", out, false);

所以现在您知道 include 指令 在翻译时插入 reuse.html 的源代码,但是操作标签插入以下响应reuse.html 在运行时

如果您考虑一下,就会发现每个操作标记 () 都会带来额外的性能影响。这意味着您可以保证您始终拥有最新的内容,但它会增加性能成本。

In one reusable piece of code I use the directive <%@include file="reuse.html"%> and in the second I use the standard action <jsp:include page="reuse.html" />.

Let the code in the reusable file be:

<html>
<head>
    <title>reusable</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <img src="candle.gif" height="100" width="50"/> <br />
    <p><b>As the candle burns,so do I</b></p>
</body>

After running both the JSP files you see the same output and think if there was any difference between the directive and the action tag. But if you look at the generated servlet of the two JSP files, you will see the difference.

Here is what you will see when you use the directive:

out.write("<html>\r\n");
out.write("    <head>\r\n");
out.write("        <title>reusable</title>\r\n");
out.write("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("    </head>\r\n");
out.write("    <body>\r\n");
out.write("        <img src=\"candle.gif\" height=\"100\" width=\"50\"/> <br />\r\n");
out.write("        <p><b>As the candle burns,so do I</b></p>\r\n");
out.write("    </body>\r\n");
out.write("</html>\r\n");
 

And this is what you will see for the used standard action in the second JSP file :

org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "reusable.html", out, false);

So now you know that the include directive inserts the source of reuse.html at translation time, but the action tag inserts the response of reuse.html at runtime.

If you think about it, there is an extra performance hit with every action tag (<jsp:include>). It means you can guarantee you will always have the latest content, but it increases performance cost.

美人迟暮 2024-12-18 09:29:08

有很大的不同。正如已经提到的,<%@ include 是静态包含,< code=""> 是动态包含。将其视为宏和函数调用之间的区别(如果您熟悉这些术语)。换句话说,静态包含与将包含文件的确切内容(“代码”)复制粘贴到 <%@ include 语句的位置完全相同(这正是 JSP 编译器将执行的操作。

动态包含将发出一个请求(使用请求调度程序),该请求将执行指定的页面,然后包含来自该页面的输出。调用页面的输出中的页面,代替< code=""> 语句的

最大区别在于,使用动态包含时,包含的页面将在其自己的 pageContext 中执行,并且由于它是请求,因此您可以将参数发送到相同的页面。另一方面,静态包含只是在调用页面的上下文中执行的一段代码,如果您多次静态包含同一文件,则该代码。该文件将存在于调用的多个位置page 所以类似的东西

<%
int i = 0;
%>

会生成编译器错误(因为同一个变量不能声明多次)。

There's a huge difference. As has been mentioned, <%@ include is a static include, <jsp:include is a dynamic include. Think of it as a difference between a macro and a function call (if you are familiar with those terms). Another way of putting it, a static include is exactly the same thing as copy-pasting the exact content of the included file (the "code") at the location of the <%@ include statement (which is exactly what the JSP compiler will do.

A dynamic include will make a request (using the request dispatcher) that will execute the indicated page and then include the output from the page in the output of the calling page, in place of the <jsp:include statement.

The big difference here is that with a dynamic include, the included page will execute in it's own pageContext. And since it's a request, you can send parameters to the page the same way you can send parameters along with any other request. A static include, on the other hand, is just a piece of code that will execute inside the context of the calling page. If you statically include the same file more than once, the code in that file will exist in multiple locations on the calling page so something like

<%
int i = 0;
%>

would generate a compiler error (since the same variable can't be declared more than once).

烛影斜 2024-12-18 09:29:08

jGuru

<%@include file="abc.jsp"%> 指令的作用类似于 C "#include"
拉入包含文件的文本并编译它,就好像它一样
是包含文件的一部分。包含的文件可以是任何类型
(包括 HTML 或文本)。

tag 将文件编译为单独的文件
JSP 文件,并在编译的 JSP 中嵌入对其的调用。

一些JSP引擎支持非标准标签(NCSA-或.shtml-style)和<%@
vinclude="data.inc" %>
(JRun 风格),但这些没有在
JSP 规范因此不能依赖。

另请参阅 JSP FAQ 中的此问题。

jGuru:

The <%@include file="abc.jsp"%> directive acts like C "#include",
pulling in the text of the included file and compiling it as if it
were part of the including file. The included file can be any type
(including HTML or text).

The <jsp:include page="abc.jsp"> tag compiles the file as a separate
JSP file, and embeds a call to it in the compiled JSP.

Some JSP engines support the non-standard tags <!--#include
file="data.inc"-->
(NCSA-, or .shtml-style) and <%@
vinclude="data.inc" %>
(JRun-style), but these are not defined in the
JSP spec and thus cannot be relied on.

See also this question in the JSP FAQ.

眼眸 2024-12-18 09:29:08

1)什么时候使用include指令

为了防止在Web应用程序的多个jsp之间重复相同的输出逻辑,使用了include机制,即,为了促进表示逻辑的可重用性,

  <%@ include file="abc.jsp" %>

当jsp引擎接收到上述指令时,使用include指令,它检索源abc.jsp 的代码和当前 jsp 中的副本的内联代码相同。复制后对当前页面进行翻译

简单来说就是对jsp引擎的静态指令,即“abc.jsp”的整个源代码被复制到当前页面

2) 何时使用include动作

include 标签不会将被包含页面的源代码包含到当前页面中,而是将被包含页面在运行时生成的输出包含到当前页面中 响应

include 标签的功能类似于 servlet 的请求调度程序的 include 机制编程

包含标记是对 jsp 引擎的运行时指令,即,不是将整个代码复制到当前页面,而是从当前页面对“abc.jsp”进行方法调用

1) When to use include directive ?

To prevent duplication of same output logic across multiple jsp's of the web app ,include mechanism is used ie.,to promote the re-usability of presentation logic include directive is used

  <%@ include file="abc.jsp" %>

when the above instruction is received by the jsp engine,it retrieves the source code of the abc.jsp and copy's the same inline in the current jsp. After copying translation is performed for the current page

Simply saying it is static instruction to jsp engine ie., whole source code of "abc.jsp" is copied into the current page

2) When to use include action ?

include tag doesn't include the source code of the included page into the current page instead the output generated at run time by the included page is included into the current page response

include tag functionality is similar to that of include mechanism of request dispatcher of servlet programming

include tag is run-time instruction to jsp engine ie., rather copying whole code into current page a method call is made to "abc.jsp" from current page

沉睡月亮 2024-12-18 09:29:08

Java Revisited

  1. 加载 include 指令包含的资源在 jsp 翻译期间,而 include 操作包含的资源在请求期间加载。
  2. 在包含指令的情况下,在 jsp 文件再次编译之前,对包含资源的任何更改都将不可见。而在包含操作的情况下,包含资源的任何更改都将在下一个请求中可见。
  3. Include 指令是静态导入,而 include 操作是动态导入
  4. Include 指令使用文件属性来指定要包含的资源,而 include 操作使用页面属性来达到相同目的。

Java Revisited

  1. Resource included by include directive is loaded during jsp translation time, while resource included by include action is loaded during request time.
  2. Any change on included resource will not be visible in case of include directive until jsp file compiles again. While in case of include action, any change in included resource will be visible in next request.
  3. Include directive is static import, while include action is dynamic import
  4. Include directive uses file attribute to specify resource to be included while include action use page attribute for same purpose.
迎风吟唱 2024-12-18 09:29:08

一种是静态导入(<%=@ include...>"),另一种是动态导入(jsp:include)。它会影响您必须为包含文件指定的路径等。对谷歌的一些研究会告诉你更多信息。

One is a static import (<%=@ include...>"), the other is a dynamic one (jsp:include). It will affect for example the path you gonna have to specify for your included file. A little research on Google will tell you more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文