tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andre Van Klaveren <nyb...@gmail.com>
Subject Re: filter question
Date Thu, 24 Feb 2005 04:02:44 GMT
Peter, I don't think Scott wants to modify every JSP in his
application.  Sounds like he has a lot!

Scott, the reason your filter isn't working is because the response
from your servlets is getting sent to the client before your filter
has a chance to add it's text to it (using the PrintWriter).

To have this work you need to "wrap" the original HttpServletResponse
object with your own.  You do this by writing your own object that
extends the HttpServletResponse interface.  This "custom" response
object is then passed to the doFilter() method of your filter class. 
Your custom HttpServletResponse object needs to also implement it's
own output stream so that you can capture any output that any
downstream code adds to the response.  Once the doFilter() method
returns from the stack you can modify the output in your custom
object's output buffer any way you want.  Then you just send the
contents of that buffer to the original output stream.

This probably sounds pretty complicated.  It's late (for me) and I'm
tired.  Sorry.  Try doing some research on the
HttpServletResponseWrapper class.  It's a convenience class that
already implements the HttpServletResponse interface and implements
the methods of that interface (actually it just delegates the calls to
the underlying response object).  All you need to do is extend this
class and override the methods you need to do any custom work.  In
your case you'd extend the getOutputStream() method and return your
code's version of the output stream.

To give you an idea of how to do it, do a Google on Servlet filters
(try compression filters).  Somebody out there has probably written an
article and/or posted their code.  Your code would be similar in
nature.

Good luck!

Virtually,
Andre Van Klaveren
SCP


On Wed, 23 Feb 2005 17:06:06 -0500, Peter Davison <peterdavison@tfnet.ca> wrote:
> Hi Scott.
> 
> Your filter should probably set an attribute in the request or perhaps the
> session, that your jsp could display, rather than writing to the response's
> writer object.  By opening up the writer and writing to it you are effectively
> setting the response to the request to be the contents of your call to the
> printwriter.println() method.
> 
> For example: your doFilter method could have something like:
> 
> request.setAttribute("IPAddress", getIPAddress());
> 
> You'll have to write the getIPAddress() method. :-)
> 
> In your jsp, you could pull the stored value out of the request and display it
> in the page:
> 
> IP Address: <%= request.getAttribute("IPAddress") %>
> 
> Hope that helps...
> 
> Regards,
> Pete.
> 
> Quoting Scott Purcell <spurcell@vertisinc.com>:
> 
> > Hello,
> > I am having trouble with a filter. Code below.
> >
> > What I am trying to achieve is as follows: We have a current web-site, with a
> > lot of jsp pages, etc. We are moving the code to a load-balanced environment,
> > and I would like to put a hidden "IP" address into each display page. This
> > way I can know which environment the page came from when debugging, etc.
> >
> > I figured I could use a 'filter' and have each page insert the IP into itself
> > in a hidden field. I am unable to achieve my goal and could use a hand if
> > anyone has experience with this. Is this possible, or will I have to write
> > into a header and then edit each jsp page to show the value?
> >
> >
> >
> > Here is what I have, but it does not print anything on pages.
> >
> > web.xml
> > <filter>
> >    <filter-name>HelloWorld</filter-name>
> >    <filter-class>chapter18.HelloWorldFilter</filter-class>
> >   </filter>
> >
> >   <filter-mapping>
> >    <filter-name>HelloWorld</filter-name>
> >    <url-pattern>/*.jsp</url-pattern>
> >   </filter-mapping>
> >
> > And I have the servlet code from SCWCD book
> > package chapter18;
> >
> > import java.io.*;
> > import javax.servlet.*;
> >
> >
> > public class HelloWorldFilter
> >     implements Filter
> > {
> >
> >     public void destroy()
> >     {
> >     }
> >
> >     public void doFilter(ServletRequest servletrequest, ServletResponse
> > servletresponse, FilterChain filterchain)
> >         throws ServletException, IOException
> >     {
> >         PrintWriter printwriter = servletresponse.getWriter();
> >         printwriter.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
> >     }
> >
> >     public void init(FilterConfig filterconfig)
> >     {
> >         filterConfig = filterconfig;
> >         System.out.println("Chapter 18: HelloWorldFilter initialized");
> >     }
> >
> >     public HelloWorldFilter()
> >     {
> >     }
> >
> >     private FilterConfig filterConfig;
> > }
> >
> >
> > Thanks,
> > Scott
> >
> 
> --
> Peter Davison             _/_/_/   _/_/_/   _/_/_/
> e: peterdavison@tfnet.ca _/   _/  _/   _/  _/   _/
> w: http://rpdavison.ca  _/_/_/   _/_/_/   _/   _/
> c: 647 883 6486        _/  _/   _/       _/   _/
> h: 416 699 2964       _/   _/  _/       _/_/_/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
>

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


Mime
View raw message