Creating Taglibs

Another feature of the webdoclet task is the ability to create custom tags using appropriate XDoclet tags. Custom tags provide a high level of reuse for the JSP page developer. In this section we will create an example custom tag and show how to use webdoclet to handle most of the details. Consider the custom tag defined in the following listing.

package customtag;
import javax.servlet.jsp.*; import javax.servlet.jsp.taxext.*;
* @jsp.tag 
* @jsp.variable name-given="currentTry" * class="java.lang.Integer" * scope="NESTED" * declare="true"
public class NameTag implements Tag { private PageContext pc = null; private Tag parent = null; private String name = null; private int try = 0;
 public int doStartTag() throws JspException { try {
 if (name != null) {
 pc.getOut().write("Your name is " + name); pc.setAttribute("currentTry", new Integer(0)); } else {
 pc.getOut().write("You aren't using the tag correctly. You need to set the name attribute");
 pc.setAttribute("currentTry", new Integer(try));
 } catch(Exception e) { throw new JspException(e);
 return SKIP BODY;
 public int doEndTag()
 throws JspException {
 return EVAL PAGE;
 * @jsp.attribute required="true"
 * rtexprvalue=""
 * descripton="The name of the thing"
 public String getName() { return name;
 public void setName(String s) { name = s;
 public void setPageContext(PageContext p) pc = p;
 public setParent(Tag t) { parent = t;
 public Tag getParent() { return parent; }

All custom tags must have a corresponding Tag Library Descriptor (TLD) in order for the engine to know about the tag. The next listing shows the start of the TLD for the simple tag above.

 <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>Tags</shortname> <tag>
 <name>NameTag</name> <tagclass>example.NameTag</tagClass> <bodycontent>empty</bodycontent> <info>First tag</info>
 <name>name</name> <required>true</required>

Clearly, you can see why it is important that we have another option for creating the TLD. Although the file isn't complex, it can become quite large if you have many custom tags. It's easy to make mistakes. Our solution is to use Ant and webdoclet to generate the TLD for us. The next listing shows that task we will need to add to an Ant build script to handle the creation of our tag library. The subtask should be added to the GenerateDD target.

<webdoclet desdir="./web/WEB-INF">
<fileset dir="./src">
 <include name="* */*" /> <include name="* */*Tag.j ava" /> </fileset>
<deploymentdescriptor servletspec="2.3"
 destdir="./web/WEB-INF" >
<taglib uri="mytablib"
 jspversion= 1.2" destdir="./web/WEB-INF/tlds" short filename="mytablib.tld"

Testing the Custom Tag

Now that we have a custom tag and an Ant build script that will handle the creation of the TLD as well as the custom tag class itself, it is time to write a JSP page to test the tag. The following listing shows an example JSP.

%@page contacntType="text/html"% %@taglib uri="mytaglib" prefix="mytag"% <html>
<title>JSP Tag</title>
<mytag:NameTag >
 Total tries = <%=currentTry%> <br/> </mytag:NameTag>
</body> </html>

In this example JSP, we use the NameTag tag and supply a specific name for the name attribute. We also access the currentTry variable defined by the custom tag.