What is TLD (Tag Library Descriptor ) file with Sample code

What is TLD flie? TLD is Tag Library Descriptor which is an XML document that describes a  tag library.  Tag library contains a list of its validator, listeners, tags, and functions. A TLD contains information about the library and tags contained in the library. A TLD may have some header information and followed by elements like <taglib>, <tlibversion>, etc which is used to define the tag library. The extension of the TLD file name is .tld. A TLD must begin with following two lines

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE taglib PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN” “http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd”>

where the first line specifies the version of XML and the document type definition (DTD). The new version of the tag library is 1.2 DTD which supports more elements and also having different names like tlib-version, jsp-version, etc…. TLDs are used by a JSP container to validate the tags. The following are the elements used in the TLD file in the version 1.1 DTD.

<taglib> – to mention tag library name.
<tlibversion> – to mention the tag library’s version
<jspversion> – to mention Jsp version.
<shortname>– This is used as a prefix value in taglib directives.
<uri> – used to identify the tag library. This is optional.
<info>– information about the tag library

Example TLD file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
 <info>Sample Tag library  Created for javaonlineguide</info>
  <!-Tag Starts -->
    <tagclass>javaonline.IpAddress </tagclass>
       This is a example tag for displaying ipaddress.

In the above tld file, <required>true</required>  tells this attribute is required. To accept runtime expression for this attribute, you need to add the line  <rtexprvalue>true</rtexprvalue>

Note: You need to copy TLD files in the WEB-INF directory or in a subdirectory of WEB-INF so that the application can access.

For complete tutorial to create Custom Tags in JSP, you can visit the post Create and implement custom tags in JSP


Reference : Tag Library Descriptor

Leave a Reply