O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

22.10. Using a Simple Tag Handler in a JSP

Problem

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

Solution

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

Discussion

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.

Tip

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" %>
<html>
<head><title>Me Casa Su Casa</title></head>
<body>

<% 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...
</body>
</html>

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:

http://localhost:8080/home/logoTest.jsp?level=1

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required