JSP include directive vs include action tag 1) Include directive includes the file at translation time (the phase of JSP life cycle where the JSP gets converted into the equivalent servlet) whereas the include action includes the file at runtime.
A tag file is a source file that contains a fragment of JSP code that is reusable as a custom tag. Tag files allow you to create custom tags using JSP syntax. Just as a JSP page gets translated into a servlet class and then compiled, a tag file gets translated into a tag handler and then compiled.
The key difference between include action and JSTL import tag is that the former can only include local resources but later can also include the output of remote resources, JSP pages, or HTML pages outside the web container. include action uses page attribute while import tag uses URL attribute.
Advertisements. The include directive is used to include a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. You may code include directives anywhere in your JSP page.
First, to make things more clear, here is a short overview of JSP syntax elements:
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.
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.
Difference between include directive and include action in JSP
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
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.
Possible Duplicate Question
<@include>
- The directive tag instructs the JSP compiler to merge contents of the included file into the JSP before creating the generated servlet code. It is the equivalent to cutting and pasting the text from your include page right into your JSP.
<jsp:include>
- The JSP Action tag on the other hand instructs the container to pause the execution of this page, go run the included page, and merge the output from that page into the output from this page.
Depending on your needs, you may either use
<@include>
or<jsp:include>
Main advantage of <jsp:include />
over <%@ include >
is:
<jsp:include />
allows to pass parameters
<jsp:include page="inclusion.jsp">
<jsp:param name="menu" value="objectValue"/>
</jsp:include>
which is not possible in <%@include file="somefile.jsp" %>
All three template options - <%@include>
, <jsp:include>
and <%@tag>
are valid, and all three cover different use cases.
With <@include>
, the JSP parser in-lines the content of the included file into the JSP before compilation (similar to a C #include
). You'd use this option with simple, static content: for example, if you wanted to include header, footer, or navigation elements into every page in your web-app. The included content becomes part of the compiled JSP and there's no extra cost at runtime.
<jsp:include>
(and JSTL's <c:import>
, which is similar and even more powerful) are best suited to dynamic content. Use these when you need to include content from another URL, local or remote; when the resource you're including is itself dynamic; or when the included content uses variables or bean definitions that conflict with the including page. <c:import>
also allows you to store the included text in a variable, which you can further manipulate or reuse. Both these incur an additional runtime cost for the dispatch: this is minimal, but you need to be aware that the dynamic include is not "free".
Use tag files when you want to create reusable user interface components. If you have a List of Widgets, say, and you want to iterate over the Widgets and display properties of each (in a table, or in a form), you'd create a tag. Tags can take arguments, using <%@tag attribute>
and these arguments can be either mandatory or optional - somewhat like method parameters.
Tag files are a simpler, JSP-based mechanism of writing tag libraries, which (pre JSP 2.0) you had to write using Java code. It's a lot cleaner to write JSP tag files when there's a lot of rendering to do in the tag: you don't need to mix Java and HTML code as you'd have to do if you wrote your tags in Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With