Taglibs provide the preferred means for implementing Java function within a JSP. They do so by removing most of the actual code from the jsp, itself, and providing access to that function though a set of XML tags. The xml tags are processed by the jsp servlet and result in calls to proper Java classes and methods that implement the intended function.
More specifically, taglibs are collections of tags for a set of related functions. They follow XML syntax, with a prefix that identifies the set of tags, a colon, and the name for the specific tag. They follow the familiar pattern of requiring an end tag or an ending slash for a unary tag.
There are two basic types of taglibs; those that are public and/or included with the server and custom taglibs that the programmer develops. We won't talk much about public taglibs, except to review briefly the useBean actions that function as taglibs and are especially useful for developing custom taglibs. The majority of the discussion will be concerned with custom taglibs, illustrated with several simple examples followed by a more typical example -- a set of tags used to build a login jsp.
API documentation is available as part of the J2EE API. Useful tutorials include Sun's Taglib Tutorial and the Apache Jakarta tutorial.
jsp:useBean and related actions
Actions:
<jsp:useBean id="beanLogin" scope"request" class="jbs.ab2.databean.BeanLogin"/><jsp:getProperty name="beanLogin" property="loginName"/><jsp:setProperty name="beanLogin" property="loginName" value="jbs"/>
Taglib Mechanics
The Tag
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <%@ taglib uri="/WEB-INF/taglib/jbsTaglib.tld" prefix="jbs" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="IBM WebSphere Studio"> <META http-equiv="Content-Style-Type" content="text/css"> <LINK href="theme/Master.css" rel="stylesheet" type="text/css"> <TITLE>HelloWorldTag.jsp</TITLE> </HEAD> <BODY> <H1> <jbs:hello/> </H1> </BODY> </HTML>The Tag Handler (Implementation)
package jbs.taglib; import java.io.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.TagSupport; public class TagHelloWorld extends TagSupport { public int doStartTag() { try { JspWriter out = pageContext.getOut(); out.println( "Hello, World, from jbs.taglib.HelloWorld" ); } catch (IOException e) { System.out.println( "Error in HelloWorld taglib: " + e.toString() ); } return SKIP_BODY; } }The Binding (Tag Library Descriptor)
<?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> <!-- taglib descriptor --> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>jbstags</shortname> <info>Tag library for Web forms-based applications</info> <!-- hello tag descriptor --> <tag> <name>hello</name> <tagclass>jbs.taglib.TagHelloWorld</tagclass> <bodycontent>EMPTY</bodycontent> <info>Hello, World, tag</info> </tag> </taglib>Run the JSP
Tag with Attributes
The Tag
<jbs:itemLink url="/AB2Web/servlet/jbs.ab2.control.AB2" label="Return to Welcome"/>The Handler
package jbs.taglib; import java.io.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.TagSupport; public class TagItemLink extends TagSupport { private String url = null; private String label = null; public int doStartTag() { try { JspWriter out = pageContext.getOut(); out.println("<tr>"); out.println("<td align='right' width='20%'></td>"); out.println("<td align='left' width='60%'>"); out.println("<a href='" + url + "'><b>" + label + "</b></a>"); out.println("</td>"); out.println("</tr>"); } catch (IOException e) { System.out.println( "Error in TagItemLink: " + e.toString() ); } return EVAL_PAGE; } public void setUrl(String url) { this.url = url; } public void setLabel(String label) { this.label = label; } }The Binding
<!-- itemLink tag descriptor --> <tag> <name>itemLink</name> <tagclass>jbs.taglib.TagItemLink</tagclass> <bodycontent>JSP</bodycontent> <info>Item Link tag</info> <attribute> <name>url</name> <required>TRUE</required> </attribute> <attribute> <name>label</name> <required>TRUE</required> </attribute> </tag>
Tag with Body
The Tag
<jbs:itemText viewName="Login: " name="loginName"> <jsp:getProperty name="beanLogin" property="loginName"/> </jbs:itemText>The Handler
package jbs.taglib; import java.io.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; public class TagItemText extends BodyTagSupport { private String name = null; private String viewName = null; public int doStartTag() { try { JspWriter out = pageContext.getOut(); out.println("<TR>"); out.println("<TD align='right' width='20%'><b>" + viewName +"</b></td>"); out.println("<TD align='left' width='60%'>"); out.print("<INPUT type='text' name='" + name + "' "); out.print("value='"); } catch (IOException e) { System.out.println( "Error in TagItemtaglib: " + e.toString() ); } return EVAL_BODY_INCLUDE; } public int doAfterBody() { try { JspWriter out = pageContext.getOut(); BodyContent body = getBodyContent(); if ( ! (body == null) ) { out.print(body.getString()); body.clearBody(); } out.print("' size='20'/>"); out.println("</TD>"); out.println("</TD>"); } catch (IOException e) { System.out.println( "Error in TagItem: " + e.toString() ); } return SKIP_BODY; } public void setName(String name) { this.name = name; } public void setViewName(String viewName) { this.viewName = viewName; } }The Binding
<!-- itemText tag descriptor --> <tag> <name>itemText</name> <tagclass>jbs.taglib.TagItemText</tagclass> <bodycontent>JSP</bodycontent> <info>Item tag</info> <attribute> <name>viewName</name> <required>TRUE</required> </attribute> <attribute> <name>name</name> <required>TRUE</required> </attribute> </tag>
Example JSP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <%@ taglib uri="/WEB-INF/taglib/jbsTaglib.tld" prefix="jbs" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="IBM WebSphere Studio"> <META http-equiv="Content-Style-Type" content="text/css"> <LINK href="/AB2Web/theme/Master.css" rel="stylesheet" type="text/css"> <TITLE>TableTags.jsp</TITLE> </HEAD> <BODY> <H1> Please login to AB2 </H1> <form action='/AB2Web/servlet/jbs.ab2.control.AB2' method='post'> <jsp:useBean id="beanLogin" scope="request" class="jbs.ab2.databean.BeanLogin"/> <jbs:table> <jbs:itemText viewName="Login: " name="loginName"> <jsp:getProperty name="beanLogin" property="loginName"/> </jbs:itemText> <jbs:itemPassword viewName="Password: " name="loginPassword"> <jsp:getProperty name="beanLogin" property="loginPassword"/> </jbs:itemPassword> <jbs:itemSpace/> <jbs:itemButton action="userAction" label="Login" color="green"/> <jbs:itemSpace/> <jbs:itemMessage viewName="Message: " name="message"> <jsp:getProperty name="beanLogin" property="message"/> </jbs:itemMessage> <jbs:itemSpace/> <jbs:itemLink url="/AB2Web/servlet/jbs.ab2.control.AB2" label="Return to Welcome"/> </jbs:table> </form> </BODY> </HTML>