tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Ludwig <mil...@gmx.de>
Subject Re: Filter and ServletResponseWrapper obligations
Date Sat, 27 Dec 2008 17:41:46 GMT
Christopher Schultz schrieb am 01.12.2008 um 14:15:31 (-0500):
> Michael Ludwig wrote:

Hi Chris,

thanks for your reply. Sorry for not getting back earlier.

> Is this the filter you wrote in your other thread?

Yes.

> Maybe you are not printing your output at the right time. Or maybe you
> aren't flushing buffers at the right time.

See below.

> > I can also make the included HTML page display correctly despite
> > the filter by resorting to res.getOutputStream() instead of
> > res.getWriter() in my servlet code. But it should work either way,
> > shouldn't it?
> 
> That is definitely odd.

The source code of DefaultServlet, which serves static HTML, explains
this behaviour.

> >  public ServletOutputStream getOutputStream() throws IOException {
> >   // getResponse().getOutputStream();
> >   return this.stream;
> >  }
> > 
> >  public PrintWriter getWriter() throws IOException {
> >   // getResponse().getWriter();
> >   return this.writer;
> >  }
> > 
> > You see there are calls to the underlying response object, which are
> > commented out. When I comment these lines back in, everything works
> > fine. No need to flush the buffer before the include, to resort to
> > SOS instead of PW, or to JSP instead of HTML.
> 
> Then you should definitely do this ;)

Yes, these calls are needed.

> > So these calls on the underlying object seem to have something to do
> > with the behaviour observed. Can anyone explain what's going on?
> 
> I'll bet that since the response hasn't had the writer/outputstream
> choice made, the DefaultServlet makes a random decision. If your
> wrapper response has made a different choice, things can get fouled
> up.

This is the relevant chunk from serveResource() in DefaultServlet (which
serves static HTML, among other things):

	try {
		ostream = response.getOutputStream();
	} catch (IllegalStateException e) {
		// If it fails, we try to get a Writer instead if we're
		// trying to serve a text file
		if ( (contentType == null)
				|| (contentType.startsWith("text"))
				|| (contentType.endsWith("xml")) ) {
			writer = response.getWriter();
		} else {
			throw e;
		}
	}

A filter intercepting the response and deviating it in its own buffers
has to make sure the call to getOutputStream() fails when it has already
issued a PrintWriter.

Michael Ludwig

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message