k k wrote:
>
> hi!!
> I'm kind of new to tag libs, I wanted to create a
> Body tag that could zip the contents in the tags to
> browser. the code is ....
>
> public class Content
> extends
> javax.servlet.jsp.tagext.BodyTagSupport {
> public int doAfterBody()
> throws
> javax.servlet.jsp.JspTagException{
>
> BodyContent bodyContent = getBodyContent();
> String body = bodyContent.getString();
> bodyContent.clearBody();
> try{
> javax.servlet.http.HttpServletResponse res =
>
> (javax.servlet.http.HttpServletResponse)pageContext.getResponse();
>
> res.setHeader("Content-Encoding", "gzip");
> //JspWriter myOut = pageContext.getOut();
> //Problem at run time "ClassCastException "
> JspWriter myOut = (JspWriter)
> getPreviousOut().getClass().newInstance();
> bodyContent = (BodyContent) myOut;
> java.io.OutputStream browser =
> res.getOutputStream();
> java.io.PrintWriter writer = new
> java.io.PrintWriter(new
> java.util.zip.GZIPOutputStream(browser),false);
> bodyContent.writeOut(writer);
> bodyContent.print(body);
> writer.flush();
> bodyContent.flush();
> writer.close();
> }catch(Exception e){
> throw new
> javax.servlet.jsp.JspTagException(e.getMessage());
> }
> return javax.servlet.jsp.tagext.Tag.SKIP_BODY;
> }
> }
>
> Problems...!!
> 1) In line 15, I'm trying to access JspWriter like
> JspWriter myOut = (JspWriter)
> getPreviousOut().getClass().newInstance();
> this throws a ClassCast Exception, cause at run
> time
> getPreviousOut() returns me a "JspWriterImpl"
> object. When I try to type cast to BodyContent
> (subclass of JspWriter) it gives me a Class Cast
> exception.
getPreviousOut() returns a reference to the "parent
tag handler's out". If you call it from a tag handler
that implements BodyTag, and that is on the "top level"
in the page, it returns a reference to the container's
implementation of the JspWriter interface.
I'm not sure why you're trying to create a new instance
and trying to cast it to BodyContent. That will not do
you any good at all (the container is responsible for
maintaining instances of all API classes). All you need
to do is this:
JspWriter out = getPreviousOut();
out.write(theZippedBytes);
> [...]
>
> 2) When I tried to use pageContext.getOut(),
> There is no output in the browser. Is it
> anything wrong in the "response" object. Or is it
> possible to pass a PrintWriter object to
> BodyContent.writeOut();
pageContext.getOut() returns a reference to the "current
out object". In a tag handler that implements BodyTag, this
happens to be the tag handler's own instance of the
BodyContent. Use the method above for what you want to
do.
>
> Please let me know is there any solution for this
> problem. I'm running on wheels for last couple of
> days.
>
> my work enviroment is weblogic 5.1 on NT workstation
> 4.0
You may want to read a book about JSP that describes how to
develop custom action tag handlers. I can recommend mine ;-)
<http://TheJSPBook.com/>
Hans
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets