logo

Custom URI In JSP Custom Tag


Show

We can use the custom URI, to inform the web container about the tld report. In such a case, we want to define the taglib element withinside the web.xml. The web container receives the data about the tld document from the web.xml record for the desired URI.

Example to apply custom URI in JSP Custom Tag

In this example, we're going to use the custom URI withinside the JSP record. For this application, we want to pay attention to 4 files.

  • index.jsp
  • web.xml
  • mytags.tld
  • PrintDate.java

index.jsp

<%@ taglib uri="mytags" prefix="m" %>  
Today is: <m:today></m:today>

web.xml

<?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>  
<taglib>  
<taglib-uri>mytags</taglib-uri>  
<taglib-location>/WEB-INF/mytags.tld</taglib-location>  
</taglib>  
</jsp-config>  

</web-app>

mytags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>  
<!DOCTYPE taglib  
        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>simple</short-name>  
  <uri>mytags</uri>  
  <description>A simple tab library for the examples</description>  

  <tag>  
    <name>today</name>  
    <tag-class>com.intellinuts.taghandler.PrintDate</tag-class>  
  </tag>  
</taglib>

PrintDate.java

package com.javatpoint.taghandler;   
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
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;  
    }  


}  

download this example