You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Bill Davidson <bi...@gmail.com> on 2011/04/15 02:11:37 UTC

Filters and JSP

Tomcat 6.0.29

I've written a response filter for my web app and it seems to work fine
for servlets but not for JSP's.  I

     public void doFilter( ServletRequest request, ServletResponse 
response, FilterChain chain ) throws IOException, ServletException {
         chain.doFilter(request, new 
MyResponseWrapper((HttpServletResponse)response));
     }

     private class MyResponseWrapper extends HttpServletResponseWrapper {
         private MyOutputStream myOutputStream;
         private PrintWriter printWriter;

         public MyResponseWrapper( HttpServletResponse response ) throws 
IOException  {
             super(response);
             myOutputStream = new MyOutputStream(response);
             OutputStreamWriter osw = new 
OutputStreamWriter(myOutputStream, response.getCharacterEncoding());
             printWriter = new PrintWriter(osw);
         }
         public ServletOutputStream getOutputStream() throws IOException  {
             return myOutputStream;
         }
         public PrintWriter getWriter() throws IOException {
             return printWriter;
         }
     }

MyOutputStream extends ServletOutputStream and wraps the methods in
the stream returned by response.getOutputStream().  The write() routines
do some manipulation of the output and send the modified output to the
wrapped stream.

The problem seems to be that it only calls write(byte[], int, int) once when
it's a JSP.  It will get called multiple times for a servlet but just 
once for
the JSP.   It's getting called with offset = 0 and len = 8192, and 
that's about
how much output I get.  My page is always truncated.  Obviously there is
some sort of buffered writer calling my write() routine.  All of the output
methods from ServletOutputStream are overridden and logged, so I know
what is getting called.

Also, flush() gets called at the end for servlets but never gets called 
for a JSP.

I don't get any exception messages in my log.

Is it something wrong in my config?

<filter>
<display-name>MyFilter</display-name>
<filter-name>MyFilter</filter-name>
<filter-class>com.mycompany.filters.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/servlets/*</url-pattern>
<url-pattern>/jsp/*</url-pattern>
</filter-mapping>

Is there something I need to do in my JSP's?


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


Re: Filters and JSP

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill,

On 4/14/2011 8:11 PM, Bill Davidson wrote:
>         public MyResponseWrapper( HttpServletResponse response ) throws
> IOException  {
>             super(response);
>             myOutputStream = new MyOutputStream(response);
>             OutputStreamWriter osw = new
> OutputStreamWriter(myOutputStream, response.getCharacterEncoding());
>             printWriter = new PrintWriter(osw);
>         }

My experience has been that handling cases where the servlet can call
either response.getOutputStream or response.getWriter is a pain in the
neck: you've got to be very careful to handle either scenario.

Are any of your methods catching IOException and not logging them?

> The problem seems to be that it only calls write(byte[], int, int)
> once when it's a JSP.

Regardless of the amount of data that the JSP /should/ be returning?

> It will get called multiple times for a servlet but just
> once for
> the JSP.   It's getting called with offset = 0 and len = 8192, and
> that's about
> how much output I get.  My page is always truncated.

If you remove your filter, does the page return the expected content to
the client? If so, it's your filter that is broken :(

> Obviously there is
> some sort of buffered writer calling my write() routine.

JSPs support (and probably always use) buffered output. It's not a
surprise that you are getting a large amount of data in a single write()
call... though if there were more than 8192 bytes of total content, I
would expect that multiple write() calls would be made.

> All of the output methods from ServletOutputStream are overridden and
> logged, so I know what is getting called.

Can you give us an example of a log dump during JSP evaluation?

> Also, flush() gets called at the end for servlets but never gets called
> for a JSP.

A JSP is just a servlet that has been compiled from a JSP source, so
they're really not that different. A difference I can think of is that
many servlets might explicitly call flush() while a JSP-generated one
does not, and Tomcat calls flush() before closing the stream. Since your
filter has lost control /before/ Tomcat flushes and closes the stream
back to the client, you may be missing this flush() call altogether.


> Is there something I need to do in my JSP's?

You could try disabling buffering, just to see if things improve. Your
performance will suffer, of course.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk2onQgACgkQ9CaO5/Lv0PCcUwCgmp9KnTDeti7Bd25gyy8LHjL4
ICwAn1FVokAtb1YRqwPpxoTRbKqKvk7a
=6JRm
-----END PGP SIGNATURE-----

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