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.14. Using a Custom Tag Associated with a Tag File

Problem

You want to use a custom tag associated with a tag file.

Solution

Use the taglib directive in the JSP, before the code that uses the tag file related tag.

Discussion

The taglib directive identifies the tag with its tagdir attribute, which is the web application path to the tags directory. Example 22-10 uses the tag from a tag file stored at /WEB-INF/tags/logo.tag.

The tag name in the JSP is the same as the tag filename, without the .tag extension. The prefix attribute represents the custom tag's namespace, so the entire tag is used in the JSP as "<cbck:logo heading=...> ...tag content...</cbck:logo>."

Example 22-10. A JSP uses a tag defined in a tag file

<%@ taglib prefix="cbck" tagdir="/WEB-INF/tags" %>

<html>
<head><title>Me Casa Su Casa</title></head>
<body>

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

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

Here's all the other stuff this page contains...
</body>
</html>

Tip

I use the same basic logo tag throughout this chapter to illustrate the various custom-tag syntax differences. See Recipe 22.2 for details on the logo tag itself.

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

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