[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tomcat-user
Subject:    Re: HttpServletResponseWrapper and buffering question
From:       William Shulman <will () mercedsystems ! com>
Date:       2002-05-31 16:12:29
[Download RAW message or body]


I am not sure if this will help, but in the past I have noticed that
catalina also wraps the request and response objects. When this
happens, you may get unexpected behavior. Sometimes you find that your
wrapper has been wrapped (not your case or else you would get a
ClassCastException in your code). Other times you find that your
wrapper is wrapping another wrapper. It sounds possible that your
wrapper is wrapping a Catalina wrapper that implements flush() in a
way you would not expect. Try introspecting your response:
response.getResponse().getClass() to see what it is. 

Not a real answer, but it may be a clue.

-will



Joe Tomcat writes:
 > Hello fellow Tomcatists,
 > 
 > I'm trying to do something clever: I need to write a HttpServletResponse 
 > object, which I am calling SuperResponse, that lets me have an escape(String) 
 > method.  That way, I can write a tag with code like this:
 > 
 > SuperResponse superResponse = (SuperResponse) pageContext.getResponse();
 > superResponse.escape("One is > two");
 > 
 > and it will send the string, properly escaped, to the output.  In this 
 > particular case, the output will be in various formats (html, xml, and plain 
 > text) which all may have different escaping rules.  The cool thing is that I 
 > can write my own custom Response object that has a custom 
 > setContentType(String) method which will capture the content type of the 
 > moment and do then it will know which escaping method to use.
 > 
 > To test out writing a response wrapper, I wrote this class called 
 > SuperResponse which will be the custom response wrapper:
 > 
 > public class SuperResponse extends HttpServletResponseWrapper {
 > 	private PrintWriter out;
 > 
 > 	public SuperResponse(ServletResponse inResp) throws IOException {
 > 		super((HttpServletResponse) inResp);
 > 		out = inResp.getWriter();
 > 	}
 > 
 > 	public void test() throws IOException {
 > 		out.print("HELLO FROM SUPERRESPONSE");
 > 	}
 > }
 > 
 > 
 > And then I wrote a simple tag:
 > 
 > public class Test extends TagSupport {
 > 	public int doEndTag() {
 > 		SuperResponse superResponse = (SuperResponse) pageContext.getResponse();
 > 		try {
 > 			superResponse.test();
 > 		}
 > 		catch(Exception e) { }
 > 	return EVAL_PAGE;
 > 	}
 > }
 > 
 > I also wrote a filter the wrap the response as it chains along.  (This filter 
 > works because otherwise I would get a cast exception at runtime in the tag, 
 > and I don't get that exception.)
 > 
 > And then I used it in a .jsp like this:
 > 
 > <%@ page language="java" contentType="text/html" %>
 > <%@ taglib uri="/WEB-INF/test.tld" prefix="test" %>
 > <html><body>
 > Hello there!  This is a test.<p>
 > This should show the result of the test tag: <test:Test/><p>
 > </body></html>
 > 
 > When I run it what I see is this:
 > 
 > % curl http://myurl
 > HELLO FROM SUPERRESPONSE
 > 
 > <html><body>
 > Hello there!  This is a test.<p>
 > This should show the result of the test tag: <p>
 > </body></html>
 > 
 > So what is happening is that for some reason, the output to the PrintWriter 
 > inside SuperResponse happens before anything is sent from the jsp.  I notice 
 > that if I include a flush in the PrintWriter in the tag, then it works the 
 > way I want it to, but if I include a flush() in SuperResponse, it doesn't 
 > change anything.
 > 
 > Anyway, I'm sure that I'm overlooking something very simple here, but I can't 
 > figure it out, so I am appealing to the experts here.  Thank you!
 > 
 > 
 > --
 > To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
 > For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>
 > 

--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic