Differencebetween <Jsp:Include Page = ... > and <%@ Include File = ... >

What's the difference between including files with JSP include directive, JSP include action and using JSP Tag Files?

Overview of JSP Syntax Elements

First, to make things more clear, here is a short overview of JSP syntax elements:

  • Directives: These convey information regarding the JSP page as a
    whole.
  • Scripting elements: These are Java coding elements such as
    declarations, expressions, scriptlets, and comments.
  • Objects and scopes: JSP objects can be created either explicitly or
    implicitly and are accessible within a given scope, such as from
    anywhere in the JSP page or the session.
  • Actions: These create objects or affect the output stream in the JSP
    response (or both).


How content is included in JSP

There are several mechanisms for reusing content in a JSP file.

The following 4 mechanisms to include content in JSP can be categorized as direct reuse:

(for the first 3 mechanisms quoting from "Head First Servlets and JSP")

1) The include directive:

<%@ include file="header.html" %>

Static: adds the content from the value of the file attribute to the current page at translation time. The directive was
originally intended for static layout templates, like HTML headers.

2) The <jsp:include> standard action:

<jsp:include page="header.jsp" />

Dynamic: adds the content from the value of the page attribute to the current page at request time. Was intended more for dynamic
content coming from JSPs.

3) The <c:import> JSTL tag:

<c:import url=”http://www.example.com/foo/bar.html” />

Dynamic: adds the content from the value of the URL attribute to the current page, at request time. It works a lot like
<jsp:include>, but it’s more powerful and flexible: unlike the
other two includes, the <c:import> URL can be from outside the
web Container
!

4) Preludes and codas:

Static: preludes and codas can be applied only to the beginnings and ends of pages.

You can implicitly include preludes (also called headers) and codas
(also called footers) for a group of JSP pages by adding
<include-prelude> and <include-coda> elements respectively within
a <jsp-property-group> element in the Web application web.xml deployment descriptor.

Read more here:

• Configuring Implicit Includes at the Beginning and End of JSPs

• Defining implicit includes


Tag File is an indirect method of content reuse, the way of encapsulating reusable content.
A Tag File is a source file that contains a fragment of JSP code that is reusable as a custom tag.

The PURPOSE of includes and Tag Files is different.

Tag file (a concept introduced with JSP 2.0) is one of the options for creating custom tags. It's a faster and easier way to build custom tags.
Custom tags, also known as tag extensions, are JSP elements that allow custom logic and output provided by other Java components to be inserted into JSP pages. The logic provided through a custom tag is implemented by a Java object known as a tag handler.

Some examples of tasks that can be performed by custom tags include operating on implicit objects, processing forms, accessing databases and other enterprise services such as email and directories, and implementing flow control.



Regarding your Edit

Maybe in your example (in your "Edit" paragraph), there is no difference between using direct include and a Tag File. But custom tags have a rich set of features. They can

  • Be customized by means of attributes passed from the calling page.

  • Pass variables back to the calling page.

  • Access all the objects available to JSP pages.

  • Communicate with each other. You can create and initialize a JavaBeans component, create a public EL variable that refers to that bean in one tag, and then use the bean in another tag.

  • Be nested within one another and communicate by means of private variables.

Also read this from "Pro JSP 2": Understanding JSP Custom Tags.



Useful reading.

  • Difference between include directive and include action in
    JSP

  • JSP tricks to make templating
    easier

  • Very informative and easy to understand tutorial from coreservlet.com with beautiful
    explanations that include <jsp:include> VS. <%@ include %>
    comparison table:

    Including Files and Applets in JSP
    Pages

  • Another nice tutorial from coreservlets.com related to tag libraries and
    tag files:

    Creating Custom JSP Tag Libraries: The
    Basics

  • The official Java EE 5 Tutorial with examples:

    Encapsulating Reusable Content
    Using Tag
    Files
    .

  • This page from the official Java EE 5 tutorial should give you even
    more understanding:

    Reusing Content in JSP
    Pages
    .

  • This excerpt from the book "Pro JSP 2" also discuses why do you need
    a Tag File instead of using static include
    :

    Reusing Content with Tag
    Files

  • Very useful guide right from the Oracle documentation:

    Static Includes Versus Dynamic Includes



Conclusion

Use the right tools for each task.

Use Tag Files as a quick and easy way of creating custom tags that can help you encapsulate reusable content.

As for the including content in JSP (quote from here):

  • Use the include directive if the file changes rarely. It’s the fastest mechanism. If your container doesn’t automatically detect changes, you can force the changes to take effect by deleting the main page class file.
  • Use the include action only for content that changes often, and if which page to include cannot be decided until the main page is requested.

Is there any difference between a JSP file and a JSP page?

Usually, there is no difference when we speak about jsp file or jsp page but a good observation from LuiggiMendoza is:

JSP file is the physical file stored in your hard drive while the JSP
page is the result of evaluating the JSP file from an application
server.

But there is a difference when using directives:

<jsp:include page="page.html" /> and also <%@include file="page.html"%>.

You can find an interesting post about it:

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

And a nice explanation here:

<%@ include file="filename" %> is the JSP include directive. At JSP
page translation time, the content of the file given in the include
directive is ‘pasted’ as it is, in the place where the JSP include
directive is used. Then the source JSP page is converted into a java
servlet class. The included file can be a static resource or a JSP
page. Generally JSP include directive is used to include header
banners and footers. The JSP compilation procedure is that, the source
JSP page gets compiled only if that page has changed. If there is a
change in the included JSP file, the source JSP file will not be
compiled and therefore the modification will not get reflected in the
output.

< jsp:include page="relativeURL" /> is the JSP include action element.
The jsp:include action element is like a function call. At runtime,
the included file will be ‘executed’ and the result content will be
included with the soure JSP page. When the included JSP page is
called, both the request and response objects are passed as
parameters. If there is a need to pass additional parameters, then
jsp:param element can be used. If the resource is static, its content
is inserted into the calling JSP file, since there is no processing
needed.

One of the difference between JSP include directive and JSP include tag

First we shall take this,

<%@ include file="/jsp/C.jsp" %>

  1. include directive will paste the page as it is in the place where
    this statement is used.
  2. If you have made changes in your static content either its in A.jsp or C.jsp, it will be rendered with the changes that you made.

<jsp:include page="/jsp/C.jsp" />

  1. This include action tag is used to insert either a static or dynamic
    content.
  2. So the changes will reflect as it is i.e the C.jsp will
    executed by passing the request and response objects to it and
    include the executed page in the A.jsp.

EDIT :

For example,

In B.jsp,

<c:out value="${value}"/>

In A.jsp,

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="value" value="10"/>

<span>From @include <%@include file="B.jsp" %></span>
<span>From jsp:include <jsp:include page="B.jsp" /></span>

When the page is loaded it will be,

From @include 10
From jsp:include

if you inspect and see, it will look like,

<span>From @include 10</span>
<span>From jsp:include <c:out value/></span>

This is because, @include will replace the contents of B.jsp into A.jsp before the rendering starts, where as for jsp:include , A.jsp and B.jsp will execute separately.

As the taglib is not imported in B.jsp, the c:out tag is not executed.

Upon changing B.jsp to

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${value}"/>

if you inspect and see the rendered page, it will look like,

<span>From @include 10</span>
<span>From jsp:include </span>

variable value has its scope only in A.jsp.

In jsp:include

scope is page

In @include

scope is request

Include another JSP file

What you're doing is a static include. A static include is resolved at compile time, and may thus not use a parameter value, which is only known at execution time.

What you need is a dynamic include:

<jsp:include page="..." />

Note that you should use the JSP EL rather than scriptlets. It also seems that you're implementing a central controller with index.jsp. You should use a servlet to do that instead, and dispatch to the appropriate JSP from this servlet. Or better, use an existing MVC framework like Stripes or Spring MVC.

Include JSP files in a jsp page

I will suggest using/creating templates and keeping most of the configuration in tiles.xml

Sample Code

<definition name="mytemplate" template="/WEB-INF/jsp/common/my_template.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/WEB-INF/jsp/common/header.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/jsp/common/footer.jsp" />
</definition>

Definition using template created above

<definition name="myPage" extends="mytemplate">
<put-attribute name="title" value="My Page" />
<put-attribute name="body" value="/WEB-INF/jsp/common/my.jsp" />
</definition>

Confusion related with jsp:include and @ include? Dynamic include and friendly Containers

The <%@ include %> directive is static because it takes the content of the included file and simply "dumps it" in the place of the directive. This is done at translation time, so the content becomes part of the parent JSP as if you wrote it there yourself.

The <jsp:include> action is dynamic because you "call it" by sending it the request and it can generate a response that gets included into the output of the parent JSP. What makes it dynamic is that you can send it parameters with <jsp:param> sub-elements. So basically, instead of dumping some content in the page, it acts like a function that you can invoke with parameters. For this reason, the things you include with it are not just content but it's dynamic content resulting from using a template that you can invoke with various parameters at various moments of time.

As for your second question, this isn't related to the <%@ include %> directive or <jsp:include> action themselves, this is how the server works (and I find it silly that they would call this "a friendly container", but well, the books tries to be friendly, I guess). Tomcat implementers decided that they can make it detect when a page included at compile time has changed and then recompile the JSP that included it, but other servlet container may have not. The specification doesn't direct the implementers to handle this, so the behavior is not portable to other servlet containers.

If you want to take advantage of this fact, that's your choice, assuming you run the same server in all your environments and you can guarantee this fact won't change, a statement which can be valid in a development environment, or if you want to play with things, but most likely will not be valid in a professional setting running user facing production code, where using proprietary things that limit portability is a frowned upon practice.

How to include a file depending on a request attribute using the include directive

Not with <%@ include%> as that is a static/compile time include.
You could instead use <jsp:include> tag, which is evaluated at runtime, and includes the result of executing that page, rather than including the page itself at compile time.

To compare all the differences between the include directive and <jsp:include> check out JSP2.0 Reference

<jsp:include page="../themes/${theme}/jsp/content/welcome.jsp"> 

The <c:import> tag would also work if you are using JSTL.



Related Topics



Leave a reply



Submit