Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
14.1 The Components That Make Up a Tag Library
311
coreservlets.tags
class and is called
ExampleTag
. Thus, with Tomcat 3.1,
the class file would be in
install_dir/webapps/ROOT/WEB INF/classes/core
servlets/tags/ExampleTag.class
.
Listing 14.1 ExampleTag.java
package coreservlets.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
/** Very simple JSP tag that just inserts a string
* ("Custom tag example...") into the output.
* The actual name of the tag is not defined here;
* that is given by the Tag Library Descriptor (TLD)
* file that is referenced by the taglib directive
* in the JSP file.
*/
public class ExampleTag extends TagSupport {
public int doStartTag() {
try {
JspWriter out = pageContext.getOut();
out.print("Custom tag example " +
"(coreservlets.tags.ExampleTag)");
} catch(IOException ioe) {
System.out.println("Error in ExampleTag: " + ioe);
}
return(SKIP_BODY);
}
}
The Tag Library Descriptor File
Once you have defined a tag handler, your next task is to identify the class to
the server and to associate it with a particular XML tag name. This task is
accomplished by means of a tag library descriptor file (in XML format) like
the one shown in Listing 14.2. This file contains some fixed information, an
arbitrary short name for your library, a short description, and a series of tag
descriptions. The nonbold part of the listing is the same in virtually all tag
library descriptors and can be copied verbatim from the source code archive
at
http://www.coreservlets.com/
or from the Tomcat 3.1 standard exam
ples (
install_dir/webapps/examples/WEB INF/jsp
).
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