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 <[EMAIL PROTECTED]>:

> 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: [EMAIL PROTECTED] _/   _/  _/   _/  _/   _/
w: http://rpdavison.ca  _/_/_/   _/_/_/   _/   _/
c: 647 883 6486        _/  _/   _/       _/   _/
h: 416 699 2964       _/   _/  _/       _/_/_/



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to