KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > filters > ExampleResponseHeadersFilter


1 package example.filters;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5 import java.io.IOException JavaDoc;
6
7 import java.util.logging.Logger JavaDoc;
8 import java.util.logging.Level JavaDoc;
9 /**
10  * A cut-and-paste template for implementing a Filter that set's response headers
11  */

12
13 public class ExampleResponseHeadersFilter implements Filter {
14   private static final Logger JavaDoc log = Logger.getLogger("example.filters.ExampleResponseHeadersFilter");
15
16   /**
17    * Called once to initialize the Filter. If init() does not
18    * complete successfully (it throws an exception, or takes a really
19    * long time to return), the Filter will not be placed into service.
20    */

21   public void init(FilterConfig config)
22     throws ServletException
23   {
24     ServletContext app = config.getServletContext();
25
26     // an example of getting an init-param
27
String JavaDoc myParam = config.getInitParameter("my-param");
28     if (log.isLoggable(Level.CONFIG))
29       log.log(Level.CONFIG,"my-param value is `" + myParam + "'");
30   }
31
32   /**
33    * Called by Resin each time a request/response pair is passed
34    * through the chain due to a client request for a resource at the
35    * end of the chain. The FilterChain parameter is used by the
36    * Filter to pass on the request and response to the next Filter in
37    * the chain.
38    */

39   public void doFilter(ServletRequest request, ServletResponse response,
40                        FilterChain nextFilter)
41     throws ServletException, IOException JavaDoc
42   {
43     HttpServletRequest req = (HttpServletRequest) request;
44     HttpServletResponse res = (HttpServletResponse) response;
45     
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,"setting response headers to stop browser caching");
55
56     res.setHeader("Cache-Control","no-cache,post-check=0,pre-check=0,no-store");
57     res.setHeader("Pragma","no-cache");
58     res.setHeader("Expires","Thu,01Dec199416:00:00GMT");
59   }
60   
61   /**
62    * Any cleanup for the filter. This will only happen once, right
63    * before the Filter is released by Resin for garbage collection.
64    */

65
66   public void destroy()
67   {
68   }
69 }
70
Popular Tags