Java Reference
In-Depth Information
The first method that you override is the
doEndTag()
method. You implement this method
because you are not concerned with the content of the body. In the
doEndTag()
method the
first thing you do is get a reference to a
JspWriter
that references the current stream being
used for client response. You then use the
print()
method to write the text
Hello
string to the
response stream.
The next method you override is the
release()
method. This is where you would release all of
the resources that you have allocated in the tag. For your purposes, because you did not allo-
cate any resources, you will simply call the parent tag's release method. This is just good prac-
tice as you start to get into more complex tag hierarchies.
To see this tag in action you need to first compile the
HelloTag.java
file and move the class
file into the
<SERVER_ROOT>/webapps/djs/WEB-INF/classes/com/djs/
directory.
You then need to create a
tld
that describes your tag library. Listing 19.2 contains the source
for your tag library descriptor.
L
ISTING
19.2
taglib.tld
<?xml version=”1.0” encoding=”ISO-8859-1” ?>
<!DOCTYPE taglib
PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN”
“http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd”>
<!-- a tag library descriptor -->
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>djs</shortname>
<uri>/djs</uri>
<info>
19
Developing Java Servlets 2
</info>
<tag>
<name>hello</name>
<tagclass>com.djs.HelloTag</tagclass>
<bodycontent>empty</bodycontent>
<info>Just Says Hello</info>
</tag>
</taglib>
You only need to examine two parts in this simple
tld
. The first is the
<uri>
entry, which is
/djs
for your library. This tells the container that this tag library should be referenced when a
JSP's
taglib
directive contains the same
uri
.