What's the Difference Between Including Files with Jsp Include Directive, Jsp Include Action and Using Jsp Tag Files

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.

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

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.

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.

jsp include tag vs struts include action?

Right from the documentation:

This can be used to integrate Struts
with other business logic components
that are implemented as servlets (or
JSP pages), but still take advantage
of the Struts controller servlet's
functionality (such as processing of
form beans)

Struts is a front controller, that handles form bean population, locale detection, exception handling, form validation, authorization, etc. This might be useful to do before including the URI (or after, for exception handling).

But if you start a Struts application from scratch, you shouldn't have to use it. It's useful when you already have a servlet and want to include it inside a STruts application without rewriting it using Struts.

How should I include other jsp files in a jsp file and why?

The include directive, makes a copy of the included page and copies it into a JSP page (the "including page") during translation. This is known as a static include (or translate-time include) and uses the following syntax:

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

Two alternatives exist for the dynamic include

The jsp:include tag, described in "Standard Actions: JSP Tags", dynamically includes output from the included page within the output of the including page during execution. This is known as a dynamic include (or runtime include) and uses the following syntax:

<jsp:include page="/jsp/userinfopage.jsp" flush="true" />
<c:import url="header.jsp" />

Unlike jsp:include, the c:import action provides a mechanism to access resources that can be specified through a URL, thus allowing page authors to get access to resources that reside outside the Web application. On the other hand, it lacks the ability to flush the response. Finally, c:import is comparatively more heavyweight and is therefore not appropriate when a lightweight solution is sought.

tagfiles are basically templates, which are like generic and can render some common views, but internally they will themselves use html tags itself.but not much of use while including jsp pages.

JSP include directive tag

It really depends on the application server you are using.

For reference, the JSP specification states in section JSP.1.10.3:

A JSP container can include a mechanism for being notified if an
included file changes, so the container can recompile the JSP page.
However, the JSP 2.2 specification does not have a way of directing
the JSP container that included files have changed.

If you are using WebSphere Application Server, by default, an include via the include directive will not cause the parent JSP to be recompiled if only the child (included jsp) has changed. If a user wants that behavior then they would set the "trackDependencies" JSP attribute to "true" in the application's WEB-INF/ibm-web-ext.xml file, which is false by default.

If you are using Tomcat, then I believe this is the expected behavior when you are running in development mode (default) with Jasper 2.

The Tomcat docs here:
http://tomcat.apache.org/tomcat-8.0-doc/jasper-howto.html
state that:

Recompile JSP when included page changes - Jasper 2 can now detect
when a page included at compile time from a JSP has changed and then
recompile the parent JSP.

If you want to turn off that behavior then I believe you would need to set the development to false tomcat_home/conf/web.xml file, like this (in the jsp section of the file):

<init-param>
<param-name>development</param-name>
<param-value>false</param-value>
</init-param>


Related Topics



Leave a reply



Submit