01: package example.filters;
02:
03: import javax.servlet.*;
04: import javax.servlet.http.*;
05: import java.io.IOException;
06:
07: import java.util.logging.Logger;
08: import java.util.logging.Level;
09:
10: /**
11: * A cut-and-paste template for implementing a Filter that set's response headers
12: */
13:
14: public class ExampleResponseHeadersFilter implements Filter {
15: private static final Logger log = Logger
16: .getLogger("example.filters.ExampleResponseHeadersFilter");
17:
18: /**
19: * Called once to initialize the Filter. If init() does not
20: * complete successfully (it throws an exception, or takes a really
21: * long time to return), the Filter will not be placed into service.
22: */
23: public void init(FilterConfig config) throws ServletException {
24: ServletContext app = config.getServletContext();
25:
26: // an example of getting an init-param
27: String myParam = config.getInitParameter("my-param");
28: if (log.isLoggable(Level.CONFIG))
29: log
30: .log(Level.CONFIG, "my-param value is `" + myParam
31: + "'");
32: }
33:
34: /**
35: * Called by Resin each time a request/response pair is passed
36: * through the chain due to a client request for a resource at the
37: * end of the chain. The FilterChain parameter is used by the
38: * Filter to pass on the request and response to the next Filter in
39: * the chain.
40: */
41: public void doFilter(ServletRequest request,
42: ServletResponse response, FilterChain nextFilter)
43: throws ServletException, IOException {
44: HttpServletRequest req = (HttpServletRequest) request;
45: HttpServletResponse res = (HttpServletResponse) response;
46:
47: // call the next filter in the chain
48: nextFilter.doFilter(req, res);
49:
50: // directly set headers on the response after invokation of the
51: // filter chain
52:
53: // this example stops the browser from caching the page
54: log.log(Level.FINER,
55: "setting response headers to stop browser caching");
56:
57: res.setHeader("Cache-Control",
58: "no-cache,post-check=0,pre-check=0,no-store");
59: res.setHeader("Pragma", "no-cache");
60: res.setHeader("Expires", "Thu,01Dec199416:00:00GMT");
61: }
62:
63: /**
64: * Any cleanup for the filter. This will only happen once, right
65: * before the Filter is released by Resin for garbage collection.
66: */
67:
68: public void destroy() {
69: }
70: }
|