Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Custom URI


We can use the custom URI, to tell the web container about the tld file. In such case, we need to define the taglib element in the web.xml. The web container gets the information about the tld file from the web.xml file for the specified URI.

Example to use custom URI in JSP Custom Tag

In this example, we are going to use the custom uri in the JSP file. For this application, we need to focus on 4 files.

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

index.jsp

  1. <%@ taglib uri=“mytags” prefix=“m” %>
  2. Today is: <m:today></m:today>

web.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <!DOCTYPE web-app
  3.   PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
  4.   “http://java.sun.com/dtd/web-app_2_3.dtd”>
  5. <web-app>
  6. <jsp-config>
  7. <taglib>
  8. <taglib-uri>mytags</taglib-uri>
  9. <taglib-location>/WEB-INF/mytags.tld</taglib-location>
  10. </taglib>
  11. </jsp-config>
  12. </web-app>

mytags.tld

  1. <?xml version=“1.0” encoding=“ISO-8859-1” ?>
  2. <!DOCTYPE taglib
  3.         PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN”
  4.         “http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd”>
  5. <taglib>
  6.   <tlib-version>1.0</tlib-version>
  7.   <jsp-version>1.2</jsp-version>
  8.   <short-name>simple</short-name>
  9.   <uri>mytags</uri>
  10.   <description>A simple tab library for the examples</description>
  11.   <tag>
  12.     <name>today</name>
  13.     <tag-class>com.Kreationnext.taghandler.PrintDate</tag-class>
  14.   </tag>
  15. </taglib>

PrintDate.java

  1. package com.Kreationnext.taghandler;
  2. import javax.servlet.jsp.JspException;
  3. import javax.servlet.jsp.JspWriter;
  4. import javax.servlet.jsp.tagext.TagSupport;
  5. public class PrintDate extends TagSupport{
  6. public int doStartTag() throws JspException {
  7.     JspWriter out=pageContext.getOut();
  8.     try{
  9.         out.print(java.util.Calendar.getInstance().getTime());
  10.     }catch(Exception e){e.printStackTrace();}
  11.     return SKIP_BODY;
  12.     }
  13. }

Comments

comments