Java Reference
In-Depth Information
The Intercepting Filter pattern is used for preprocessing and postprocessing of client requests and
also the responses by intercepting requests and responses. The filters are pluggable in the sense
that you can add or remove them without changing the code.
One of the use cases for which the Intercepting Filter pattern is deemed best fit is when you want
to enable theIE9 document mode of the browser by default. IE has two modes: browser mode and
document mode. The browser always sends browser mode data to the server, and the server always
responds with document mode data. The browser mode data consists of a user agent string with
a version and trident token information, while the document mode data consists of metatags that
dictate the mode in which the response will be rendered on the browser.
Listing 3-57 shows a simple response filter that enables the IE9 document mode of the browser
by default.
Listing 3-57. Simple Response Filter
1.package com.apress.filters
2.import javax.servlet.*;
3.import javax.servlet.http.HttpServletResponse;
4.import java.io.IOException;
5.import java.util.Enumeration;
6.
7./**
8. * filter for enabling IE9 document mode by default
9. *
10. */
11.public class ResponseHeaderFilter implements Filter {
12. private FilterConfig filterConfig = null;
13.
14. public void doFilter(ServletRequest aServletRequest, ServletResponse aServletResponse,
FilterChain chain)
15. throws IOException, ServletException {
16.
17. HttpServletResponse response = (HttpServletResponse) aServletResponse;
18.
19. // set the provided HTTP response parameters
20. for (Enumeration e = filterConfig.getInitParameterNames(); e.hasMoreElements();) {
21. String headerName = (String) e.nextElement();
22. response.addHeader(headerName, filterConfig.getInitParameter(headerName));
23. }
24.
25. // pass the request/response on
26. chain.doFilter(aServletRequest, response);
27. }
28.
29. public void init(FilterConfig aFilterConfig) {
30. filterConfig = aFilterConfig;
31. }
32.
33. public void destroy() {
34. filterConfig = null;
35. }
36.}
 
Search WWH ::




Custom Search