Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
14.1 The Components That Make Up a Tag Library
313
The JSP File
Once you have a tag handler implementation and a tag library description,
you are ready to write a JSP file that makes use of the tag. Listing 14.3 gives
an example. Somewhere before the first use of your tag, you need to use the
taglib
directive. This directive has the following form:
<%@ taglib uri="..." prefix="..." %>
The required
uri
attribute can be either an absolute or relative URL
referring to a tag library descriptor file like the one shown in Listing 14.2.
To complicate matters a little, however, Tomcat 3.1 uses a
web.xml
file that
maps an absolute URL for a tag library descriptor to a file on the local sys
tem. I don't recommend that you use this approach, but you should be
aware of it in case you look at the Apache examples and wonder why it
works when they specify a nonexistent URL for the
uri
attribute of the
taglib
directive.
The
prefix
attribute, also required, specifies a prefix that will be used in
front of whatever tag name the tag library descriptor defined. For example, if
the TLD file defines a tag named
tag1
and the
prefix
attribute has a value
of
test
, the actual tag name would be
test:tag1
. This tag could be used in
either of the following two ways, depending on whether it is defined to be a
container that makes use of the tag body:
Arbitrary JSP
or just
To illustrate, the descriptor file of Listing 14.2 is called
csa
jsp taglib.tld
, and resides in the same directory as the JSP file shown in
Listing 14.3. Thus, the
taglib
directive in the JSP file uses a simple relative
URL giving just the filename, as shown below.
<%@ taglib uri="csajsp taglib.tld" prefix="csajsp" %>
Furthermore, since the
prefix
attribute is
csajsp
(for Core Servlets and
JavaServer Pages), the rest of the JSP page uses
csajsp:example
to refer to
the
example
tag defined in the descriptor file. Figure 14 1 shows the
result.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved