On Wed, Oct 20, 2004 at 11:07:58AM -0400, Ben Souther wrote:
> Search the archives, I believe Tim Funk has actually written and
> published one....  Not sure though.

     It's pretty straight-forward.  This is the first filter I ever
coded, it was a snap.  I make no guarantees that the particular
headers are exactly what you need (browser caching is annoyingly
difficult to disable).

-config---------------------------------------------------------------------
    <filter>
        <filter-name>NoCacheFilter</filter-name>
        <filter-class>class.path.to.NoCacheFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>NoCacheFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
-source---------------------------------------------------------------------
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import javax.servlet.ServletException;

public class NoCacheFilter implements Filter {
    
    public void init(FilterConfig config) throws ServletException {
        this.filterConfig = config;
    }
    private FilterConfig filterConfig;
    public FilterConfig getFilterConfig() {
        return this.filterConfig;
    }
    public void setFilterConfig (FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }
    public void destroy() {
        this.filterConfig = null;
    }

    public void doFilter (ServletRequest request,
                          ServletResponse response,
                          FilterChain chain) {
        try {
            if (response instanceof HttpServletResponse) {
                HttpServletResponse httpresponse = (HttpServletResponse)response ;
                // Set the Cache-Control and Expires header
                httpresponse.setHeader("Cache-Control", "no-cache") ;
                httpresponse.setHeader("Expires", "0") ;
            }
            chain.doFilter (request, response);
        } catch (IOException e) {
            System.out.println ("IOException in NoCacheFilter");
            e.printStackTrace() ;
        } catch (ServletException e) {
            System.out.println ("ServletException in NoCacheFilter");
            e.printStackTrace() ;
        }
    }
}
----------------------------------------------------------------------

-- 
Steven J. Owens
[EMAIL PROTECTED]

"I'm going to make broad, sweeping generalizations and strong,
 declarative statements, because otherwise I'll be here all night and
 this document will be four times longer and much less fun to read.
 Take it all with a grain of salt." - http://darksleep.com/notablog


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

Reply via email to