1 package example.filters; 2 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 import java.io.IOException ; 6 7 import java.util.logging.Logger ; 8 import java.util.logging.Level ; 9 10 13 14 public class ExampleResponseFilter implements Filter { 15 private static final Logger log = Logger.getLogger("example.filters.ExampleResponseFilter"); 16 17 22 public void init(FilterConfig config) 23 throws ServletException 24 { 25 ServletContext app = config.getServletContext(); 26 27 String myParam = config.getInitParameter("my-param"); 29 if (log.isLoggable(Level.CONFIG)) 30 log.log(Level.CONFIG,"my-param value is `" + myParam + "'"); 31 } 32 33 40 public void doFilter(ServletRequest request, ServletResponse response, 41 FilterChain nextFilter) 42 throws ServletException, IOException 43 { 44 HttpServletRequest req = (HttpServletRequest) request; 45 HttpServletResponse res = (HttpServletResponse) response; 46 47 res = new ExampleResponseWrapper(res); 51 52 nextFilter.doFilter(req, res); 54 } 55 56 60 public void destroy() 61 { 62 } 63 64 65 71 class ExampleResponseWrapper extends HttpServletResponseWrapper { 72 ExampleResponseWrapper(HttpServletResponse response) 73 { 74 super(response); 75 } 76 77 82 public void setStatus(int sc) 83 { 84 super.setStatus(sc); 85 } 86 87 public void setStatus(int sc, String msg) 88 { 89 super.setStatus(sc, msg); 90 } 91 92 97 public void sendError(int sc, String msg) 98 throws IOException 99 { 100 super.sendError(sc, msg); 101 } 102 103 108 public void sendError(int sc) 109 throws IOException 110 { 111 super.sendError(sc); 112 } 113 114 119 public void sendRedirect(String location) 120 throws IOException 121 { 122 super.sendRedirect(location); 123 } 124 125 132 public void setHeader(String name, String value) 133 { 134 super.setHeader(name, value); 135 } 136 137 144 public void addHeader(String name, String value) 145 { 146 super.addHeader(name, value); 147 } 148 149 154 public boolean containsHeader(String name) 155 { 156 return super.containsHeader(name); 157 } 158 159 171 public void setDateHeader(String name, long date) 172 { 173 super.setDateHeader(name, date); 174 } 175 176 182 public void addDateHeader(String name, long date) 183 { 184 super.addDateHeader(name, date); 185 } 186 187 193 public void setIntHeader(String name, int value) 194 { 195 super.setIntHeader(name, value); 196 } 197 198 204 public void addIntHeader(String name, int value) 205 { 206 super.addIntHeader(name, value); 207 } 208 209 212 public void addCookie(Cookie cookie) 213 { 214 super.addCookie(cookie); 215 } 216 217 224 public String encodeURL(String url) 225 { 226 return super.encodeURL(url); 227 } 228 229 236 public String encodeRedirectURL(String name) 237 { 238 return super.encodeRedirectURL(name); 239 } 240 241 } 242 } 243 | Popular Tags |