Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9266045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:13:38+00:00 2026-06-18T14:13:38+00:00

It seems that there are two methods for templating with JSP. Including files with

  • 0

It seems that there are two methods for templating with JSP. Including files with one of these statements

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

or using JSP tag files

// Save this as mytag.tag
<%@ tag description="Description" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
    <jsp:doBody/>
</body>
</html>

And in another JSP page call it with

<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:mytag>
    <h1>Hello World</h1>
</t:mytag>

So which method should I use? Is one now considered deprecated or are they both valid and cover different use cases?

Edit

Isn’t using this tag file the same as using an include?

// Save this as product.tag
<%@ tag description="Product templage" pageEncoding="UTF-8"%>
<%@ tag import="com.myapp.Product" %>
<%@ attribute name="product" required="true" type="com.myapp.Product"%>

Product name: ${product.name} <br/>
Quantity: ${product.quantity} <br/>

And call it on another JSP with

<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:product>
    <c:forEach items="${cart.products}" var="product">
        <t:product product="${product}"/>
    </c:forEach>
</t:product>

That seems to me to be the very same as using an include and passing parameters to it. So are Tag Files the same as includes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:13:39+00:00Added an answer on June 18, 2026 at 2:13 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that these two operators are pretty much the same - is there
It seems that there are two methods for auto generating web service proxies in
It seems to me that there are two scenarios in which to use JOINs:
It seems that there is a implementation of rope in my /usr/include/c++/4.5.1/ext/rope (and ropeimpl.h
It seems that there are several really fast prime factorization algorithms around (one that
There are two things that seem to be popular nowadays and I was wondering
It seems that there is a guidance that a model should not expose its
After looking at RescueTime for windows/mac, it seems that there's a version for linux
How can I add an URL to the trusted site? It seems that there
I know, I know, its sounds silly, but it seems that there are no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.