Jeff Levine
Jeff Levine

Reputation: 2163

Custom Tag - JspWriter.write() produces no output

My custom tag (handler is printSomething.java, which implements Tag) is running, but not producing expected output. I was expecting to see "The tag works!", but only my template text is displaying. Am I misusing the JspWriter? Code and output below.

printSomething.java

package webcert.ch08.x0804;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class printSomething implements Tag{

private PageContext pc;
private Tag parent;

public void setPageContext(PageContext pc){
    this.pc = pc;
}
public void setParent(Tag parent){
    this.parent = parent;
}
public Tag getParent(){
    return parent;
}
public int doStartTag(){
    try{
        printMessage();
    }
    catch(IOException ioe){}
    return Tag.SKIP_BODY;
}
public int doEndTag(){
    return Tag.EVAL_PAGE;
}
public void release(){

}
public void printMessage() throws IOException{
    JspWriter out = pc.getOut();
    out.write("<b>The tag works!</b>");
    out.print("<b>The tag works!</b>");
    out.flush();
}

}

practice.jspx

<html xmlns:mytags="http://www.ets.com/mytags"
xmlns:jsp="http://java.sun.com/JSP/Page">

<jsp:output omit-xml-declaration="true"/>
<jsp:directive.page contentType="text/html"/>

<head><title>Practice JSPX</title></head>
  <body>
    BEFORE<br/>
    <mytags:printSomething/><br/>
    AFTER<br/>
  </body>
</html>

web.xml

<web-app>
  <jsp-config>
    <taglib-uri>http://www.ets.com/mytags</taglib-uri>
    <taglib-location>/WEB-INF/tags/mytags.tld</taglib-location>
  </jsp-config>
</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/web-
jsptaglibrary_2_0.xsd" 
version="2.0">

  <tlib-version>1.0</tlib-version>
  <short-name>some tags</short-name>
  <tag>
    <name>printSomething</name>
    <tag-class>webcert.ch08.x0804.PrintSomething</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

output

BEFORE

AFTER

Upvotes: 1

Views: 990

Answers (1)

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40298

Your code works for me (JBoss 7.1.1 - essentially a Tomcat for the web part).

A few changes that are important, unless they are typos in your code above:

  • The tag class name should be PrintSomething (capital 'P'), not printSomething (according to Java naming conventiona, but most importantly according to mytags.tld: <tag-class>webcert.ch08.x0804.PrintSomething</tag-class>)
  • The <jsp-config> of web.xml is syntactically wrong; should be:

    <jsp-config>
        <taglib>
            <taglib-uri>http://www.ets.com/mytags</taglib-uri>
            <taglib-location>/WEB-INF/tags/mytags.tld</taglib-location>
        </taglib>
    </jsp-config>
    
  • In mytags.tld there seems to be a line break between http://java.sun.com/xml/ns/j2ee/web- and jsptaglibrary_2_0.xsd. Make sure there is none in the real file!

Upvotes: 1

Related Questions