tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: [Http]ServletResponseWrapper.getOutputStream()
Date Mon, 01 Dec 2008 18:15:24 GMT
Hash: SHA1


Michael Ludwig wrote:
> [Handing out multiple objects] seems to have caused the effect observed, which was the
absence of
> the HTML file included via RequestDispatcher.include() in both the
> buffer used to capture the response and the output itself.
> Might this have to do with the fact that the PrintWriter is buffered?

Yes. But you want all client code to be working on the same wrapper
object as well as underlying object. There's no compelling reason to
create additional objects.

>>>  public void flushBuffer() throws IOException {
>>>   this.buffer.flush();
>>>  }
>> Flushing a ByteArrayOutputStream doesn't do anything. What you really
>> want to do is flush all the OutputStream and Writer objects you've
>> created when calls to getOutputStream and getWriter come in.
> I dropped this method.

I would put it back. You want the Response.flushBuffer method to work
and actually flush the buffer being used. You should add this method back.

>  public HttpResponseCatcher( HttpServletResponse res) {
>   super( res);
>   this.buffer = new ByteArrayOutputStream();
> = new CapturedServletOutputStream( this.buffer);
>   this.writer = new PrintWriter( new OutputStreamWriter(;

! No character encoding!

>  }

I would lazily instantiate these objects -- at least the stream and
writer. There's no need to create a writer if the caller doesn't ask for
one. Also, the caller might want to set the character encoding on the
response, and you have to allow that to occur before you create your writer.

>  public ServletOutputStream getOutputStream() throws IOException {
>   getResponse().getOutputStream();
>   return stream;
>  }

Here's where you'd create your stream (or writer).

 public ServletOutputStream getOutputStream() throws IOException {

  if(null == stream)
     stream = new CapturedServletOutputStream(this.buffer);

  return stream;

I would do the same with the PrintWriter. I still think that using a
unified buffer is non-ideal. But hey... if it works, it works.

>  public String getCapturedOutput() { return buffer.toString(); }
>  public byte[] getByteArray() { return buffer.toString().getBytes(); }

Yikes! You aren't checking the character encoding of the response! You
definitely need to do this in order to properly implement this method.

- -chris
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla -


To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message