Table of contents
1.
Introduction
2.
Custom URI in JSP
3.
Example - Custom URI in JSP
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

Custom URI in JSP

Author Tanay kumar Deo
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JSP(Java Server Pages) is a server-side programming language that combines HTML,Java Servlet, XML, and JavaBeans technologies into a highly productive technology. It allows developers to develop reliable, platform-independent, high-performance web applications and dynamic websites.

The JSP API allows us to define our own custom JSP tags that look like XML or  HTML tags. This article will learn about custom URI in JSP ( Java Server Pages ). This article will focus on Custom URI (Uniform Resource Identifier) in JSP. Custom URI tells the web container about the TLD (Tag Library Descriptor) file. So, let's get started.

Custom URI in JSP

Custom URI is used to tell the web container about the TLD file. The web container gets the information about the TLD file from the web.xml file for the specified URI. To do this, we need to define the taglib directive in the web.xml. The taglib element declares that our JSP page will use a set of custom tags, identify the location of our library, and provide means for determining the custom tags on our JSP page.

Syntax for taglib directive:

<%@ taglib uri = "uriValue" prefix = "prefixOfTheTag" >
You can also try this code with Online Java Compiler
Run Code

The attributes used here are as follows:

  • uri:- This attribute takes a value that resolves to a location the container understands.
  • prefix:- This attribute informs the container what bits of markup are custom actions.

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:

  1. 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.
  2. 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 JSPJSP - Custom TagsJSP 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.

Live masterclass