[ Team LiB ] Previous Section Next Section

Recipe 22.10 Using a Simple Tag Handler in a JSP


You want to use a custom tag based on a simple tag handler.


Use the taglib directive in the JSP, specifying the proper uri attribute for the tag library.


Make sure to package the tag library, the simple tag handler, and its associated TLD, as described by Recipe 22.4 and Recipe 22.5 and the note in Recipe 22.9. Example 22-8 shows the rest of the setup needed to use the tag in a JSP.

Simple tag handlers are designed to be easier to develop (by having only one method that you need to implement: void doTag( )). Use the associated tags in a JSP the same way you use the tags associated with classic tag handlers.

Example 22-8. A JSP uses a tag defined by a simple tag handler
<%@ taglib uri="jspservletcookbook.com.tags" prefix="cbck" %>
<head><title>Me Casa Su Casa</title></head>

<% session.setAttribute("imgDir",(request.getContextPath( ) +
 "/images/")); %>

<cbck:simplelogo heading=
  "<%=request.getParameter(\"level\") %>" image=
   "stamp.gif" width="42" height="54">
     Thanks for visiting here</cbck:simplelogo>

Here's all the other stuff this page contains...

The JSP in Example 22-8 obtains the value for the logo tag's heading attribute with a JSP expression. The JSP page user provides the value in the URL as in:


The JSP's output looks the same as the output shown in Figure 22-1 of Recipe 22.1.

See Also

The JSP 2.0 specification web page: http://jcp.org/en/jsr/detail?id=152; Recipe 22.2 and Recipe 22.3 on creating TLD files for tag libraries; Recipe 22.4 and Recipe 22.5 on packaging a tag library in a web application; Recipe 22.6 on using the custom tag in a JSP; Recipe 22.7 on handling exceptions in tags; Recipe 22.8 and Recipe 22.9 on creating a simple tag handler; Recipe 22.11-Recipe 22.14 on using a JSP tag file; Recipe 22.15 on adding a listener class to a tag library; the customtag sections of Hans Bergsten's JavaServer Pages, Third Edition (O'Reilly).

    [ Team LiB ] Previous Section Next Section