Custom JSP tags allow you to create your own new JSP tag (like <my:hello /> or <my:showDate />) that encapsulates Java logic cleanly,
instead of mixing Java code (<% %>) directly into your JSP pages.
✅ It helps you extend JSP functionality, reuse common logic, and keep JSPs clean and readable.
✅ Custom tags are server-side components that are executed during JSP processing.
🔥 Why Write Custom Tags?
| Benefit | Reason |
|---|---|
| Cleaner JSP code | No messy Java code (<% %>) |
| Reusable components | Write once, use everywhere |
| Separation of concerns | Keep logic out of presentation |
| Easy for designers | Designers can use tags without needing Java knowledge |
| Consistency | Same logic, same output everywhere |
🛠️ How to Write a Simple Custom Tag?
There are two main ways:
🔵 1. Using a Java Class (Tag Handler Class)
✅ You write a Java class that extends one of the following:
| Class | Use Case |
|---|---|
SimpleTagSupport | For simple tags (JSP 2.0 and newer) — recommended |
TagSupport | For older-style, more complex tags |
BodyTagSupport | For tags that operate on body content |
Simple Example:
Step 1: Java Class
package mytags;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;
public class HelloTag extends SimpleTagSupport {
public void doTag() throws IOException {
JspWriter out = getJspContext().getOut();
out.print("Hello from a custom tag!");
}
}
Step 2: Create a Tag Library Descriptor (TLD) File
Create a mytags.tld inside /WEB-INF/.
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>MyTags</short-name>
<uri>http://example.com/mytags</uri>
<tag>
<name>hello</name>
<tag-class>mytags.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
Step 3: Use Your Tag in a JSP
<%@ taglib uri="http://example.com/mytags" prefix="my" %>
<my:hello />
✅ Outputs:
Hello from a custom tag!
🔵 2. Using Tag Files (.tag)
✅ A faster and simpler way to create tags without Java coding if your logic is simple.
How?
- Create a
.tagfile inside/WEB-INF/tags/. - Write normal JSP content inside it.
Example:
File: /WEB-INF/tags/hello.tag
Hello from a tag file!
Use it in JSP:
<%@ taglib tagdir="/WEB-INF/tags" prefix="my" %>
<my:hello />
✅ Much quicker — no Java classes needed!
🎯 Summary: Two Ways to Write Custom Tags
| Approach | Best When | Notes |
|---|---|---|
| Tag Handler Class (Java class + TLD) | More complex, reusable, parameterized logic | Full power |
Tag File (.tag) | Simple reusable templates | Quick and easy |
📢 Important Advanced Points:
- You can pass attributes to custom tags (like
<my:hello name="Stanley" />) by writing setters in Java class or by usingattributedirective in.tagfiles. - You can handle body content (the text between start and end tags) using
BodyContentin classic tag handlers or<jsp:doBody />in tag files. - Modern JSP prefers SimpleTagSupport + .tag files style (easier and cleaner).
🚀 Real-World Usage
You typically create custom tags for:
- UI components (menus, buttons, panels)
- Formatting (dates, currencies)
- Custom access control (show/hide content for users with specific roles)
- Common layouts (headers, footers, sections)
📜 Quick Comparison to Built-in Tags
| Type | Example |
|---|---|
| Built-in tag (JSTL) | <c:forEach>, <c:if> |
| Your custom tag | <my:hello>, <my:showDate> |
✅ Same usage style, but you control the behavior!