tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <>
Subject RE: inconsistencies in Tomcat handling servlet filters...
Date Tue, 01 Apr 2003 18:08:04 GMT

Many thanks to Jonathan Leech for pointing out how to fix my problem 
(below).  See the original thread here:
and then the code I forgot to attach to the original message here:

He asked me to forward it to the list since he isn't a subscribed 
member.  So, here it is (below).  Now the only issue left is why the third 
example I mentioned still doesn't work ...

Given this code....

         CharResponseWrapper wrapper = new 
CharResponseWrapper((HttpServletResponse) response);
         chain.doFilter(request, wrapper);

It prints out everything for jsp pages but only the following for html page...

     <title>Lesson 3</title>

<p>This is a pure test page</p>


The missing data is this...


I guess this is the one last mystery to solve.   It must have something to 
do with not setting the content length, but why would it affect the 
filtering of html pages and not jsp pages?  Hmmmm....  Anyone have a guess 
as to why this is happening?


>From: "Leech, Jonathan" <>
>To: "''" <>
>Subject: RE: inconsistencies in Tomcat handling servlet filters...
>Date: Mon, 31 Mar 2003 11:19:57 -0700
>I think I can help you out....  Do me a favor though and post/forward this
>response to the tomcat mailing list because
>I'm too lazy to figure out how to subscribe and reply properly.
>The simple answer is you need to flush() your PrintWriter.  Do this:
>PrintWriter writer = null;
>public byte[] toByteArray() {
>    if (writer != null) writer.flush();
>    return output.toByteArray();
>public String toString() {
>    if (writer != null) writer.flush();
>    return output.toString();
>public PrintWriter getWriter() {
>    writer = new PrintWriter(getOutputStream(), true);
>    return writer;
> > Questions to answer...
> >
> > 1.  Why don't all 3 examples produce exactly the same output?  It
> >     seems to me that they should.
>The first example will work fine with the modifications I suggest above.
>I don't know what's wrong in the third example, but perhaps it will work as
> > 2.  Why does using a response wrapper that uses a ByteArrayOutputStream
> >     (as in GenericResponseWrapper) not work with JSP's?  The original data
> >     is always unavailable.
>flush()ing the PrintWriter cures this.  Incidentally, if your .jsp writes
>enough data you will get some data but the rest truncated.
> > 3.  Why do I have to use a PrintWriter when filtering JSP's?  Why
> >     can't I use an OutputStream?  Using an OutputStream gets me the same
> >     as #2.
>The spec says in the Servlet you've got to use one or the other, but not
>both.  You
>should be able to relax this constraint in your filter if everything is
>kosher.  Perhaps
>flushing the PrintWriter properly will fix this as well.
> > So, is there anything wrong with my code or is something just goofy in
> > Tomcat?
>Looks to me like your code would have worked just fine, as mine did, in
>older versions
>of Tomcat, but that they stopped flushing the PrintWriter for you after the
>.jsp runs, at
>some point between 4.0.3 and 4.1.18
>Jonathan Leech
>Senior Software Engineer
>Virtela Communications, Inc.

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message