SimpleExample.jsp and cwp-taglib.tld from
the text's archive.corewebprogramming.com/Chapter20.html and store them anywhere in your CoreWebProgramming webapp. The top level (e.g., CoreWebProgramming/cwp) will do. (This example uses JSP-Styles.css. Make sure these files are in a directory with that file.)
SimpleExample.jsp to be SimpleTagExample.jsp to avoid confusion with the other examples.
ExampleTag.java from
the text's archive.corewebprogramming.com/Chapter20.html and store it in %TOMCAT_HOME%/webapps/CoreWebProgramming/cwp/tagstags directory is a new directory.
ExampleTag.java and put the
ExampleTag.class file into %TOMCAT_HOME%/webapps/CoreWebProgramming/WEB-INF/classes/cwp/tagstags directory is a new directory. We are defining a new tags package inside the cwp package.
SimpleTagExample.jsp, e.g.,
http://localhost:8080/CoreWebProgramming/cwp/SimpleTagExample.jsp
C:\jakarta-tomcat-3.2.4\webapps\CoreWebProgramming\cwp\SimpleTagExample.jsp(11,0)
Unable to open taglibrary cwp-taglib.tld:
C:\jakarta-tomcat-3.2.4\webapps\CoreWebProgramming\WEB-INF\web.xml
(The system cannot find the file specified)
Nothing we have done before required the web.xml file, which is normally located directly under the WEB-INF directory. After some experimentation, I found that if I put a skeletal web.xml file under the WEB-INF directory, the SimpleTagExample.jsp page would work.
Here is the web.xml file I used.
<!--
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
-->
<web-app>
</web-app>
Apparently, when opening a taglib descriptor file (.tld) for the first time from any .jsp file, jasper checks to see that the web.xml file exists and that it has a <web-app> root. (The web.xml file can be used to provide a local reference for .tld files, but that is not necessary.)SimpleTagExample.jsp, e.g.,
http://localhost:8080/CoreWebProgramming/cwp/SimpleTagExample.jsp
C:\jakarta-tomcat-3.2.4\webapps\CoreWebProgramming\cwp\SimpleTagExample.jsp(11,0)
C:\jakarta-tomcat-3.2.4\webapps\CoreWebProgramming\cwp\cwp-taglib.tld
(The system cannot find the file specified)
The problem was that when I downloaded cwp-taglib.tld, ftp had stored this file as cwp-taglib.tld.xml.
Change the name and try again. This time it worked for me.
SimpleTagExample.jsp
<%@ taglib uri="../cwp-taglib.tld" prefix="cwp" %> refers to the .tld file, which defines the tags used in this .jsp file.
<cwp:example />), two names are required: the library in which the tag is defined (in this case cwp) and the name of the tag itself (in this case example).
cwp-taglib.tld
.tld file for a library describes a (single) tag library and the various tags in that library. (See listing 20.25 in the text and the actual cwp-taglib.tld file.) Here is the example tag entry in the cwp-taglib.tld file.
<tag>
<!-- The tag name -->
<name>example</name>
<!-- The .Java file that implements the tag -->
<tagclass>cwp.tags.ExampleTag</tagclass>
<!-- A comment describing the tag -->
<info>Simplest example: inserts one line of output</info>
<!-- Whether there is a body, and how to process it.
If the body is standard jsp, the entry would be "jsp" instead of "empty" -->
<bodycontent>empty</bodycontent>
</tag>
ExampleTag.java
<cwp:example> tag.
Methods in tag classes may be called at three stages in the processing of a tag:
<cwp:example ... > is encountered,
</cwp:example> (if there is one), is processed.
</cwp:example>) is encountered.The .Java file that implements tags should have a method for each of these elements it wants to handle. The methods are called, respectively: doStartTag(), doAfterBody(), doEndTag().
In this example, the <cwp:example /> tag was self-terminating, so only the doStartTag() method was defined. All that method did was generate a line of output:
out.print("Custom tag example (cwp.tags.ExampleTag)");
Notice that doStartTag() returns an int. The constant SKIP_BODY, which is presumably defined in TagSupport (of which ExampleTag is a subclass, see http://java.sun.com/j2ee/sdk_1.3/techdocs/api/), tells the tag processor not to look for or use the body, i.e., the text, if any, between the start of this tag and the end of this tag. (In this case, there is nothing there.)
Do an experiment with SimpleTagExample.jsp and ExampleTag.java.
<H1><cwp:example /></H1>
<cwp:example /> to be
<dl>
<dt><h2>Original</h2>
<dd><H2><cwp:example /></H2>
<cwp:example />
<dt><h2>Revised</h2>
<dd><H2><cwp:example>text</cwp:example></H2>
<cwp:example>stuff</cwp:example>
</dl>
SimpleTagExample.jsp
http://localhost:8080/CoreWebProgramming/cwp/SimpleTagExample.jsp again.
doStartTag() from SKIP_BODY to EVAL_BODY_INCLUDE.
public int doEndTag() {
try {
JspWriter out = pageContext.getOut();
out.print("End tag";
}
catch(IOException ioe) {
System.out.println("Error in ExampleTag: " + ioe);
}
return (EVAL_PAGE);
}
<span style='background-color:teal'> at the end of the doStartTag().
</span> at the start of the doEndTag().
ExampleTag.java and put the
ExampleTag.class file into %TOMCAT_HOME%/webapps/CoreWebProgramming/WEB-INF/classes/cwp/tagsSimpleTagExample.jsp
http://localhost:8080/CoreWebProgramming/cwp/SimpleTagExample.jsp again.
.tld file.
setAttribute() method to the .Java file that implements the tag.
<cwp:example bgColor="red">text</cwp:example>bgColor attribute to the tag.
<tag>
<name>example</name>
<tagclass>cwp.tags.ExampleTag</tagclass>
<info>Simplest example: inserts one line of output</info>
<!-- TOMCAT 3.1 DOES NOT SUPPORT BODYCONTENT
<bodycontent>empty</bodycontent> -->
<!-- We now have a body, and we are using 3.2.4.
Use "jsp" instead of "empty". -->
<bodycontent>jsp</bodycontent>
<attribute>
<name>bgColor</name>
</attribute>
</tag>
bgColor variable and a setBgColor() method to ExampleTag.java.
private String bgColor = null;
public void setBgColor(String color) {bgColor = color;}
out.print("<span style='background-color:" + bgColor + "'>");ExampleTag.java and put the
ExampleTag.class file into %TOMCAT_HOME%/webapps/CoreWebProgramming/WEB-INF/classes/cwp/tagsSimpleTagExample.jsp file to pass the color to the tag.
<H1><cwp:example bgColor=red>text</cwp:example></H1>
<cwp:example bgColor=pink>stuff</cwp:example>
SimpleTagExample.jsp
http://localhost:8080/CoreWebProgramming/cwp/SimpleTagExample.jsp again.
org.apache.jasper.compiler.ParseException:
C:\jakarta-tomcat-3.2.4\webapps\CoreWebProgramming\cwp\Copy4TagExamples\SimpleTagExample.jsp(20,25)
Attribute value should be quoted
SimpleTagExample.jsp file.
SimpleTagExample.jsp file and refer to it from the tag.
<BODY>
<%! String bgc="orange"; %>
<H1><cwp:example bgColor="red">text</cwp:example></H1>
<cwp:example bgColor="<%= bgc %>">stuff</cwp:example>
</BODY>
SimpleTagExample.jsp
http://localhost:8080/CoreWebProgramming/cwp/SimpleTagExample.jsp again.
<rtexprvalue>true</rtexprvalue> to the cwp-taglib.tld file.
...
<attribute>
<name>bgColor</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
...
cwp-taglib.tld file again. Rename the SimpleTagExample.jsp file (or the directory it is in) and try again.
.jsp. Other than that, you should be able to follow this example pretty closely.