ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between <jsp:include page = ... > and <%@ include file = ... >? [duplicate]

This question already has answers here: What's the difference between including files with JSP include directive, JSP include action and using JSP Tag Files? (5 answers) Closed 8 years ago.

Both tags include the content from one page in another.

So what is the exact difference between these two tags?


i
informatik01

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.


Can you be more specific on which include result in which output? You mentioned your second include is using directive (@include). Then in the first output result, you mentioned you used directive (@include), but on the second output result, you also said that it's from second include (which, from your earlier explanation, is @include).
In case somebody else is confused by this answer: <jsp:include is dynamic (JspRuntimeLibrary), <%@include is static (out.write).
@eis in <%@include by is static(out.write) you mean any thing in write like lets say EL that reads from request will be dynamic or static please can you help me here thanks or it will be treated as string ${someVar}
@eis is right, it's not very clear what is what in the answer!
This answer is very good at source code explanation!!! I can use <%@include for global variable sharing purpose, use <jsp:include for jsp scope variable individually without naming conflict issue. While for text output only, both are same. for variable scope usage, they are quite different.
p
pap

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


This type of answer is a true gift of SO, which saves a few if not many hours for someone else.
U
Uchenna Nwanyanwu

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


A
Ajay Takur

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


Great explanation! At translation time(static) directives<%@ include file="file.jsp" %> OR At runtime(action)
A
Abhijeet Ashok Muneshwar

Java Revisited

Resource included by include directive is loaded during jsp translation time, while resource included by include action is loaded during request time. 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. Include directive is static import, while include action is dynamic import Include directive uses file attribute to specify resource to be included while include action use page attribute for same purpose.


t
talnicolas

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.