Customised tag URI attribute in web.xml

There are many way to deploy and using tag libraries with JSP engine. We can customised uri attribute using web application deployment descriptor, web.xml. The <taglib> element contains two sub-tags: <taglib-uri> specifies the URI , that is used in taglib directive uri value and <taglib-location> specifies the path to the tag library descriptor within the WAR.
Syntax of taglib directive
<%@ taglib uri="/inputName" prefix="mytag" %>
URI location in 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 > <display-name>CustomTagURIExample</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <taglib> <taglib-uri>/inputTag</taglib-uri> <taglib-location>/WEB-INF/tdls/inputName.tld </taglib-location> </taglib> </web-app>
Custom uri Tag Example using Tomcat6.0
We can customised uri attribute using web application deployment descriptor, web.xml. And hence there is no specifed uri in .tld file. These following files are used in customised uri example.
  1. InputNameTag.java
  2. web.xml
  3. inputName.tld
  4. index.jsp
InputNameTag.java
package ns; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class InputNameTag extends TagSupport { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public int doStartTag() throws JspTagException { return EVAL_BODY_INCLUDE; } public int doEndTag() throws JspTagException { try { JspWriter out=pageContext.getOut(); out.print("Customised uri in web.xml Example"); out.print("<br>"); out.println("User Name :" + name); } catch(Exception e) { } return EVAL_PAGE; } }
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 > <display-name>CustomTagURIExample</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <taglib> <taglib-uri>/inputTag</taglib-uri> <taglib-location>/WEB-INF/tdls/inputName.tld </taglib-location> </taglib> </web-app>
inputName.tld
<?xml version="1.0" encoding="UTF-8"?> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>hello</short-name> <tag> <name>example</name> <tag-class>ns.InputNameTag </tag-class> <body-content>EMPTY</body-content> <attribute> <name>name</name> <required> true </required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
index.jsp
<%@ page import="java.util.*"%> <%@ taglib uri="/inputTag" prefix="mytag" %> <html> <head> <title>Welcome customised uri in web.xml example </title> </head> <body> Welcome to customised uri in web.xml <h1> <mytag:example name="Sunil" /> </h1> <br> <h2> <mytag:example name="Kumar" /> </h2> </body> </html>
Output
custom tag uri

Download this example
1.) Deployed using Tomcat 6.0
2.) Deployed using Eclipse
 



 

Latest Tutorial


Most Visited Tutorial