Custom tags allow a developer to give non-programmers access to sophisticated class libraries through a simple XML (similar to HTML) tag. In order to develop a custom tag a few guidelines must be followed, which are outlined in the JSP 1.1 specification. Once a custom tag has been created it must be added to the TLD (Tag Library Definition) file. An example of a TLD file with one entry would be:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>transformationbeans</shortname> <info>Tag Library For SAS Transformation Beans</info> <tag> <name>Checkbox</name> <tagclass>com.sas.servlet.beans.taglets.Checkbox</tagclass> <teiclass>com.sas.servlet.beans.taglets.tei.CheckboxInfo</teiclass> <info>SAS Checkbox Transformation Bean</info> <attribute> <name>id</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>
In addition to the tag class itself, you may also specify a TEI (Tag Extra Info) class. A TEI class allows you to specify variables that you would like to either instantiate and/or set values to with in a JSP page. Now that you have a TLD file specified, the next step is to tell the Servlet Engine where to find the file, and how to reference it. A sample Web Descriptor (web.xml) file would be:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<taglib>
<taglib-uri>http://localhost/transforms/transform-taglib</taglib-uri>
<taglib-location>/WEB-INF/transforms.tld</taglib-location>
</taglib>
</web-app>
The final step in making a custom tag available for use with in a JSP page is to add the JSP directive to the top of the JSP page. A simple JSP page that utilizes the examples previously specified would look like:
<%@ taglib uri="http://localhost/transforms/transform-taglib" prefix="sas" %>
<HTML>
<HEAD>
<TITLE>Sample JSP/Custom Tag Page</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE=”+4”>Sample JSP/Custom Tag Page</FONT>
</CENTER>
<P>
<FORM ACTION=”duh_submit.jsp” METHOD=”POST”>
<sas:Checkbox id=”cbox” />
</FORM>
</BODY>
</HTML>
The Checkbox tag could just have easily been written as:
<sas:Checkbox id=”cbox”>
Other Information Inside the Body of the Checkbox Tag
</sas:Checkbox>
The only reason to have a body inside the tag (the above example) is to specify/use variables that the custom tag produces.