tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joe Tomcat <tom...@mobile.mp>
Subject HttpServletResponseWrapper and buffering question
Date Fri, 31 May 2002 07:26:26 GMT
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>


Mime
View raw message