Nearby lessons

20 of 30

JSP - Tag Files

📌 What You Will Learn
  • Understand what Tag Files are and how they differ from Java-based tags
  • Create and use a basic tag file
  • Declare attributes in tag files
  • Process tag body with jsp:doBody
  • Deploy tag files in JARs
  • Disable scripting language globally

Tag Files (JSP 2.0) let you create custom tags using JSP syntax instead of Java classes. A tag file is a simple JSP page saved with a .tag extension. This lesson covers creating, using, and deploying tag files, including attributes, body content, and JAR deployment.

What are Tag Files?

Tag Files were introduced in JSP 2.0. A Tag File is a JSP page or document designed to be used as a custom tag. The main advantage is simplicity — you can build custom tags using JSP syntax instead of writing Java classes.

Key characteristics:

  • Saved with .tag extension
  • Placed in /WEB-INF/tags folder
  • No TLD file required (when placed in the standard location)
  • Tag files are internally converted to Simple Tag Handlers by the web container
  • Limited processing power — not suitable for complex business logic

Creating a Basic Tag File

3 steps to create and use a tag file:

  1. Write a JSP page and save it with .tag extension
  2. Place it in /WEB-INF/tags folder
  3. Use taglib directive with tagdir attribute in your JSP

Project structure:

tagFileA/
├── test.jsp
└── WEB-INF/
    └── tags/
        └── mytag.tag

test.jsp:

<%@ taglib prefix="mine" tagdir="/WEB-INF/tags" %>
<h1>This is Tag File Demo Example</h1>
<mine:mytag />
<mine:mytag />

mytag.tag:

<h1>Hello....This is from the tag file</h1>

Output:

This is Tag File Demo Example
Hello....This is from the tag file
Hello....This is from the tag file

Declaring Attributes in Tag Files

Use the attribute directive to declare attributes for tag files:

<%@ attribute name="title" required="true" rtexprvalue="true" %>

Demo — Tag file with attribute:

<%@ taglib prefix="mine" tagdir="/WEB-INF/tags" %>
<h1>Tag File with Attributes Example</h1>
<mine:mytag title="kabali" />

mytag.tag:

<%@ attribute name="title" required="true" rtexprvalue="true" %>
<h1>Hello....${title} is big flop but business is good 600Cr..</h1>

Processing Tag Body in Tag Files

Tag file invocation can contain a body, but scripting elements are not allowed in the body.

Declaring body content:

<%@ tag body-content="tagdependent" %>

Allowed values: empty, tagdependent, scriptless. Default is scriptless.

To access the tag body inside the tag file, use <jsp:doBody/>.

Demo — Tag file with body:

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

<mine:header color="red">
  This is Body of tag file
</mine:header>

header.tag:

<%@ attribute name="color" required="true" rtexprvalue="true" %>
<%@ tag body-content="tagdependent" %>
<h1> Hello....This is from the tag file<br>
  <font color="${color}"> <jsp:doBody/></font>
</h1>

Where Web Container Searches for Tag Files

The web container searches for tag files in these locations:

  1. Directly or indirectly in /WEB-INF/tags folder
  2. Directly or indirectly in /META-INF/tags folder present in a JAR file under /WEB-INF/lib

Directory structure example:

webapps1/
└── WEB-INF/
    ├── tags/
    │   └── MyTags/
    └── lib/
        └── MyJar.jar
            └── META-INF/
                ├── MyTld.tld
                └── tags/
                    └── MyTags/

Note: When deploying tag files in a JAR, you must write a TLD file.

Deploying Tag Files in JARs

To deploy tag files inside a JAR file:

Step 1 — Create the JAR structure:

durga/
├── META-INF/
│   └── MyTld.tld
└── tags/
    └── MyTags.tag

Step 2 — Create the JAR:

jar -cvf durga.jar META-INF tags

Step 3 — Place JAR in WEB-INF/lib

Step 4 — TLD file must reference the tag file path:

<taglib version="2.1">
  <tlib-version>1.2</tlib-version>

  <tag-file>
    <name>mytagfile</name>
    <path>/META-INF/tags/mytag.tag</path>
  </tag-file>

</taglib>

Usage in JSP:

<%@ taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
<mine:mytagfile />

Disabling Scripting Language Globally

It is not recommended to use scripting elements in JSP. You can disable them globally using web.xml:

<web-app>
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
  </jsp-config>
</web-app>

Once scripting is invalidated, any use of scripting elements in JSP causes a translation time error.

Similarly, you can disable the Expression Language globally:

<web-app>
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <el-ignored>true</el-ignored>
    </jsp-property-group>
  </jsp-config>
</web-app>

Include Directive vs Include Action

Include DirectiveInclude Action
<%@ include file="second.jsp" %><jsp:include page="second.jsp" flush="true"/>
1 attribute: file2 attributes: page, flush
Translation time (static include)Runtime (dynamic include)
Single Servlet generated for both JSPsSeparate Servlets generated for each JSP
Code sharing is possibleCode sharing is NOT possible
Higher performanceLower performance
No guarantee of latest versionAlways includes latest version
Use when target resource won't changeUse when target resource changes frequently

JSP Implicit Objects

JSP pages have 9 implicit objects automatically available:

ObjectType
requestHttpServletRequest
responseHttpServletResponse
configServletConfig
applicationServletContext
sessionHttpSession
outJspWriter
pageObject (current Servlet instance)
pageContextPageContext
exceptionThrowable (error pages only)
📝 Key Takeaways
  • Tag Files are the simplest way to create custom tags
  • No TLD file needed when tag files are in WEB-INF/tags
  • Tag files are internally converted to Simple Tag Handlers

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3