views:

215

answers:

2

Hi,

I want to display the body value of JSP in custom tag name Simple from the tag handler by extending the class, BodyTagSupport, but getting a runtime exception ;o)

The JSP code is:

<html><body>
<%@ taglib prefix="mine" uri="simpleTags" %>

Advisor page
<mine:simple>
Balle Balle
</mine:simple>
</html></body>

and the Tag handler class is:

package foo;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class SelectTagHandler extends BodyTagSupport
{
public int doStartTag() throws JspException{

return EVAL_BODY_BUFFERED;
}

public int EndTag()
{
try{
pageContext.getOut().print(bodyContent);
}
catch(Exception e)
{
}
return EVAL_PAGE;
}

and the exception, which I'm getting is:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: foo.SelectTagHandler.setJspContext(Ljavax/servlet/jsp/JspContext;)V
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

javax.servlet.ServletException: foo.SelectTagHandler.setJspContext(Ljavax/servlet/jsp/JspContext;)V
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774)
    org.apache.jsp.new_jsp._jspService(new_jsp.java:60)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.NoSuchMethodError: foo.SelectTagHandler.setJspContext(Ljavax/servlet/jsp/JspContext;)V
    org.apache.jsp.new_jsp._jspx_meth_mine_005fsimple_005f0(new_jsp.java:73)
    org.apache.jsp.new_jsp._jspService(new_jsp.java:51)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.27 logs.

Edit: The code of my TLD file is:

<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.2</tlib-version>
<uri>simpleTags</uri>
<tag>
<name>simple</name>
<tag-class>foo.SelectTagHandler</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>

What's wrong I'm doing here?

A: 

setJspContext only applies to SimpleTag and SimpleTagSupport, not BodyTag (don't ask me why). Tomcat is treating your tag as a SimpleTag for some reason. The problem may lie in your TLD file, can you modify your question to add that?

skaffman
A: 

You're printing out bodyContent directly in your code:

pageContext.getOut().print(bodyContent);

bodyContent is not a String, it's an object of javax.servlet.jsp.tagext.BodyContent class and it doesn't define a suitable toString() method (nor should it) to be used in such a way. You should instead obtain the actual body content from it via getString() method:

if (bodyContent!=null) {
    String bodyText = bodyContent.getString();
    pageContext.getOut().print(bodyText); // or whatever you want to do with it
}
ChssPly76