[ Team LiB ] Previous Section Next Section

The taglib Directive

The JavaServer Pages API has an extension mechanism known as a tag library. You can create custom JSP tags that look like HTML or XML tags. By segregating more of your Java code into custom tags, you reduce the amount of Java code that appears in the JSP. This topic is important enough to give it its own hour. You'll read about it in Hour 16.

The taglib directive loads a tag library for use in the JSP. When you load a tag library, you must specify the URI of the tag library and the prefix for the tags, like this:

<%@ taglib uri="http://usingjsp.wutka.com/taglib/testlib"
   prefix="testing" %>

When you use a custom tag, it is typically of the form <prefix:tagname>. The prefix is the same as the prefix you specify in the taglib directive, and the tagname is the name of a tag implemented in the tag library.

For example, suppose the testlib tag library contains a tag called checkmate. If you wanted to use the checkmate tag with a prefix of testing, your tag would be <testing:checkmate>.

    [ Team LiB ] Previous Section Next Section