Nearby lessons
20 of 30JSP - Tag Files
- 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
.tagextension - Placed in
/WEB-INF/tagsfolder - 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:
- Write a JSP page and save it with
.tagextension - Place it in
/WEB-INF/tagsfolder - Use
taglibdirective withtagdirattribute 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:
- Directly or indirectly in
/WEB-INF/tagsfolder - Directly or indirectly in
/META-INF/tagsfolder 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 Directive | Include Action |
|---|---|
<%@ include file="second.jsp" %> | <jsp:include page="second.jsp" flush="true"/> |
| 1 attribute: file | 2 attributes: page, flush |
| Translation time (static include) | Runtime (dynamic include) |
| Single Servlet generated for both JSPs | Separate Servlets generated for each JSP |
| Code sharing is possible | Code sharing is NOT possible |
| Higher performance | Lower performance |
| No guarantee of latest version | Always includes latest version |
| Use when target resource won't change | Use when target resource changes frequently |
JSP Implicit Objects
JSP pages have 9 implicit objects automatically available:
| Object | Type |
|---|---|
request | HttpServletRequest |
response | HttpServletResponse |
config | ServletConfig |
application | ServletContext |
session | HttpSession |
out | JspWriter |
page | Object (current Servlet instance) |
pageContext | PageContext |
exception | Throwable (error pages only) |
- 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