I've been playing with this code for hours, and I can't determine why my tag appears to be doing nothing.
lab08.jspx simply outputs whatever is entered in the Set Body
field in lab08J.html, seemingly ignoring the friend
entry altogether.
Code below.
lab08J.html
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/1999/xhtml
http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd">
<head><title>lab08J.html</title><head>
<body>
<form action="lab08J.jspx">
Set Friend:<input type="text" name="friend"/>
<p>Set Body:<input type="text" name="body"/>
<input type="submit"/>
</form>
</body>
</html>
lab08J.jspx
<html xmlns:j="http://ets.com/tags/j"
xmlns:jsp="http://java.sun.com/JSP/Page">
<jsp:output omit-xml-declaration="true" />
<jsp:directive.page contentType="text/html" />
<head><title>lab08.jspx</title></head>
<body><j:myTag friend="${param.friend}">${param.body}</j:myTag>
</body>
</html>
web.xml
<?xml version="1.0" ?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>
myTags.tld
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>lab08Jtaglib</short-name>
<uri>http://ets.com/tags/j</uri>
<tag>
<name>myTag</name>
<tag-class>webcert.ch08.lab08J.MyTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>friend</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
MyTag.java
package webcert.ch08.lab08J;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.IterationTag;
public class MyTag extends BodyTagSupport{
private PageContext pageContext;
private BodyTagSupport parent;
private String friend;
private BodyContent bc = getBodyContent();
public void setPageContext(PageContext pageContext){
super.setPageContext(pageContext);
log("setPageContext()");
}
public void setParent(BodyTagSupport parent){
super.setParent(parent);
log("setParent()");
}
public void setFriend(String friend)throws Exception{
this.friend = friend;
log("setFriend() as " + friend);
JspWriter out = pageContext.getOut();
out.write("setFriend as " + friend);
}
public int doStartTag(){
log("doStartTag()");
return BodyTagSupport.EVAL_BODY_BUFFERED;
}
public void setBodyContent(BodyContent bc){
super.setBodyContent(bc);
log("setBodyContent()");
}
public void doInitBody(){
log("doInitBody()");
}
public int doAfterBody(){
log("doAfterBody()");
try{
bc.write("Meet my friend " + friend);
}
catch(IOException ioe){
ioe.printStackTrace();
}
return IterationTag.EVAL_BODY_AGAIN;
}
public int doEndTag(){
log("doEndTag()");
try{
bc.writeOut(bc.getEnclosingWriter());
}
catch(IOException ioe){
ioe.printStackTrace();
}
return BodyTagSupport.EVAL_PAGE;
}
public void release(){
System.out.println("release()");
super.release();
}
protected void log(String s){
try {
bc.write(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
There are some things wrong in your MyTag.java. I have tried to fix it and it should be worked.