Example - Custom URI in JSP
This section will see an example of a custom URI in JSP in a custom tag. For this particular example, we will add four files to our application:
- index.jsp
- web.xml
- mycustomtags.tld
- PrintDate.java
Let’s get started with our example by adding taglib in our index.jsp file.
<%@ taglib uri="mycustomtags" prefix="m" %>
Date added: <m:today></m:today>

You can also try this code with Online Java Compiler
Run Code
Now we need to configure our JSP project by adding the following code in web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<jsp-config>
// Configuring JSP project with taglib
<taglib>
<taglib-uri>mycustomtags</taglib-uri>
<taglib-location>/WEB-INF/mycustomtags.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
Now, let’s write the logic for our custom tag in the mycustomtags.tld file.
<?xml version=" 1.0 " encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC " -//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version> 1.0 </tlib-version>
<jsp-version> 1.2 </jsp-version>
<short-name> example </short-name>
<uri>mycustomtags</uri>
<description>An example for tablib to make custom tags</description>
<tag>
<name> Today </name>
<tag-class>com.codingninjas.taghandler.PrintDate</tag-class>
</tag>
</taglib>
Finally, let’s write the code for our PrintDate class in the PrintDate.java.
package com.codingninjas.taghandler;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class PrintDate extends TagSupport{
public int doStartTag() throws JspException {
JspWriter out=pageContext.getOut();
try{
out.print(java.util.Calendar.getInstance().getTime());
}catch(Exception e){e.printStackTrace();}
return SKIP_BODY;
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, we successfully created a custom tag and used a custom URI in JSP to tell the web container about our TLD file.
Frequently Asked Questions
1. What is the difference between uri and tagdir in JSP taglib?
Ans. Both the directives have different concepts for the custom tag in JSP:
- uri: We can use uri to refer to a tag library, which can be delivered in a jar file or can be defined in a TLD file.
- tagdir: We can use tagdir to refer to the tagfiles within the WEB-INF/tags.
2. What is the XML equivalent taglib syntax?
Ans. The XML equivalent taglib syntax is as follow:
<jsp:directive.taglib uri = "uri" prefix = "prefixOfTag" />
Key Takeaways
We learned about custom URI in JSP in custom tags in this article. We used the taglib directive for doing the same. We implemented custom URI in JSP along with taglib directive and custom tags in our detailed example.
Don't stop here. Check out the blogs Introduction to JSP, JSP - Custom Tags, JSP Architecture and Lifecycle, and JSP Syntax and First App.
We hope you found this blog helpful. Liked the blog? Then feel free to upvote and share it.