和?(复制)

时间:2022-02-22 20:08:10

This question already has an answer here:

这个问题已经有了答案:

Both tags include the content from one page in another.

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

So what is the exact difference between these two tags?

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

6 个解决方案

#1


94  

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

在一个可重用的代码段中,我使用了指令<%@include文件=“重用”。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 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 :

在运行了两个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");

and this is what you will see for the used tag in the second JSP file :

这是在第二个JSP文件中使用的标记所看到的:

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.

现在您知道include指令插入了重用的源。在翻译时html,但是动作标记插入重用的响应。html在运行时。

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

如果您考虑一下,每个动作标记(jsp:include文件)都会有一个额外的性能。这意味着你可以保证你永远拥有最新的内容,但它会增加性能成本。

#2


51  

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.

有一个巨大的差异。如前所述,<%@包括一个静态的include, 是一个动态的include。把它看作是宏和函数调用之间的区别(如果您熟悉这些术语)。另一种方法是,静态包含的内容与将包含的文件的确切内容(“代码”)粘贴在

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.

动态include将会发出一个请求(使用请求分派器),它将执行指定的页面,然后在调用页的输出中包含来自页面的输出,代替 语句。

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

这里最大的区别是,在动态include中,包含的页面将在它自己的pageContext中执行。由于它是一个请求,您可以向页面发送参数,就像您可以通过其他请求发送参数一样。另一方面,静态include只是在调用页的上下文中执行的一段代码。如果静态地包含同一文件不止一次,那么该文件中的代码将存在于调用页上的多个位置,因此类似于。

<%
int i = 0;
%>

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

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

#3


34  

jGuru:

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).

< % = " abc @include文件。jsp"%>指令像C "#include"一样,拉入包含文件的文本,并将其编译成包含文件的一部分。包含的文件可以是任何类型(包括HTML或文本)。

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.

< jsp:包括页面= " abc。jsp“>标记将文件编译为一个单独的jsp文件,并在编译的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.

一些JSP引擎支持非标准标记 (NCSA-, or .shtml-style)和<%@ vinclude="数据。inc“%> (jrunstyle),但是这些在JSP规范中没有定义,因此不能依赖。

See also this question in the JSP FAQ.

在JSP FAQ中也可以看到这个问题。

#4


27  

1) When to use include directive ?

1)何时使用include指令?

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

为了防止在web应用程序的多个jsp中重复相同的输出逻辑,包括使用ie的机制。为了提高表达逻辑的可重用性,我们使用了指令。

  <%@ 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

当jsp引擎接收上述指令时,它将检索abc的源代码。jsp和副本在当前jsp中是相同的。在对当前页面执行复制操作之后。

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

简单地说,它是jsp引擎的静态指令。,全部源代码的“abc”。jsp“被复制到当前页面中。

2) When to use include action ?

2)何时使用include动作?

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标签不包括包含页面的源代码到当前页面中,而是将包含的页面在运行时生成的输出包含到当前页面响应中。

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

包含标记功能类似于servlet编程请求分派器的包含机制。

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

包括标签是对jsp引擎的运行时指令。,而是将整个代码复制到当前页面中,方法调用是“abc”。从当前页面的jsp”

#5


1  

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.

一个是静态导入(<%=@ include…>),另一个是动态的导入(jsp:include)。它将影响您为包含的文件指定的路径。关于谷歌的一些研究将会告诉你更多。

#6


1  

Java Revisited

Java重新审视

  1. Resource included by include directive is loaded during jsp translation time, while resource included by include action is loaded during request time.
  2. 包含指令的资源在jsp翻译时加载,包括在请求时加载包含操作的资源。
  3. 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.
  4. 在jsp文件再次编译之前,包含的资源的任何更改都将不可见。在包含动作的情况下,包含的资源的任何变化都将在下一个请求中可见。
  5. Include directive is static import, while include action is dynamic import
  6. Include指令是静态导入,而Include动作是动态导入。
  7. Include directive uses file attribute to specify resource to be included while include action use page attribute for same purpose.
  8. Include指令使用file属性指定要包含的资源,同时包含用于相同目的的action use page属性。

#1


94  

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

在一个可重用的代码段中,我使用了指令<%@include文件=“重用”。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 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 :

在运行了两个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");

and this is what you will see for the used tag in the second JSP file :

这是在第二个JSP文件中使用的标记所看到的:

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.

现在您知道include指令插入了重用的源。在翻译时html,但是动作标记插入重用的响应。html在运行时。

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

如果您考虑一下,每个动作标记(jsp:include文件)都会有一个额外的性能。这意味着你可以保证你永远拥有最新的内容,但它会增加性能成本。

#2


51  

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.

有一个巨大的差异。如前所述,<%@包括一个静态的include, 是一个动态的include。把它看作是宏和函数调用之间的区别(如果您熟悉这些术语)。另一种方法是,静态包含的内容与将包含的文件的确切内容(“代码”)粘贴在

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.

动态include将会发出一个请求(使用请求分派器),它将执行指定的页面,然后在调用页的输出中包含来自页面的输出,代替 语句。

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

这里最大的区别是,在动态include中,包含的页面将在它自己的pageContext中执行。由于它是一个请求,您可以向页面发送参数,就像您可以通过其他请求发送参数一样。另一方面,静态include只是在调用页的上下文中执行的一段代码。如果静态地包含同一文件不止一次,那么该文件中的代码将存在于调用页上的多个位置,因此类似于。

<%
int i = 0;
%>

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

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

#3


34  

jGuru:

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).

< % = " abc @include文件。jsp"%>指令像C "#include"一样,拉入包含文件的文本,并将其编译成包含文件的一部分。包含的文件可以是任何类型(包括HTML或文本)。

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.

< jsp:包括页面= " abc。jsp“>标记将文件编译为一个单独的jsp文件,并在编译的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.

一些JSP引擎支持非标准标记 (NCSA-, or .shtml-style)和<%@ vinclude="数据。inc“%> (jrunstyle),但是这些在JSP规范中没有定义,因此不能依赖。

See also this question in the JSP FAQ.

在JSP FAQ中也可以看到这个问题。

#4


27  

1) When to use include directive ?

1)何时使用include指令?

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

为了防止在web应用程序的多个jsp中重复相同的输出逻辑,包括使用ie的机制。为了提高表达逻辑的可重用性,我们使用了指令。

  <%@ 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

当jsp引擎接收上述指令时,它将检索abc的源代码。jsp和副本在当前jsp中是相同的。在对当前页面执行复制操作之后。

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

简单地说,它是jsp引擎的静态指令。,全部源代码的“abc”。jsp“被复制到当前页面中。

2) When to use include action ?

2)何时使用include动作?

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标签不包括包含页面的源代码到当前页面中,而是将包含的页面在运行时生成的输出包含到当前页面响应中。

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

包含标记功能类似于servlet编程请求分派器的包含机制。

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

包括标签是对jsp引擎的运行时指令。,而是将整个代码复制到当前页面中,方法调用是“abc”。从当前页面的jsp”

#5


1  

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.

一个是静态导入(<%=@ include…>),另一个是动态的导入(jsp:include)。它将影响您为包含的文件指定的路径。关于谷歌的一些研究将会告诉你更多。

#6


1  

Java Revisited

Java重新审视

  1. Resource included by include directive is loaded during jsp translation time, while resource included by include action is loaded during request time.
  2. 包含指令的资源在jsp翻译时加载,包括在请求时加载包含操作的资源。
  3. 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.
  4. 在jsp文件再次编译之前,包含的资源的任何更改都将不可见。在包含动作的情况下,包含的资源的任何变化都将在下一个请求中可见。
  5. Include directive is static import, while include action is dynamic import
  6. Include指令是静态导入,而Include动作是动态导入。
  7. Include directive uses file attribute to specify resource to be included while include action use page attribute for same purpose.
  8. Include指令使用file属性指定要包含的资源,同时包含用于相同目的的action use page属性。