一、目标
1.用Filter模拟处理Request、Response
2.思路细节技巧:
(1)Filter的doFilter方法改为doFilter(Request,Resopnse,FilterChain),有FilterChain引用,为利用FilterChain调用下一个Filter做准备
(2)FilterChain继承Filter,这样,FilterChain既是FilterChain又是Filter,那么FilterChain就可以调用Filter的方法doFilter(Request,Resopnse,FilterChain)
(3)FilterChain的doFilter(Request,Resopnse,FilterChain)中,有index标记了执行到第几个Filter,当所有Filter执行完后request处理后,就会return,以倒序继续执行response处理
二、代码
1.Filter.java
2.HTMLFilter.java
3.SensitiveFilter.java
4.FilterChian.java
5.Request.java
6.Response.java
7.Test.java
1.Filter.java
1 public interface Filter { 2 public void doFilter(Request request, Response response, FilterChain filterChain); 3 }
2.HTMLFilter.java
1 public class HTMLFilter implements Filter { 2 3 @Override 4 public void doFilter(Request request, Response response, 5 FilterChain filterChain) { 6 request.setRequestStr(request.getRequestStr().replace('<', '[').replace(">", "]")+"---HTMLFilter()"); 7 filterChain.doFilter(request, response, filterChain); 8 response.setResponseStr(response.getResponseStr()+"---HTMLFilter()"); 9 } 10 11 }
3.SensitiveFilter.java
1 public class SensitiveFilter implements Filter { 2 3 @Override 4 public void doFilter(Request request, Response response, 5 FilterChain filterChain) { 6 request.setRequestStr(request.getRequestStr().replace("敏感", "幸福")+"---SensitiveFilter()"); 7 filterChain.doFilter(request, response, filterChain); 8 response.setResponseStr(response.getResponseStr()+"---SensitiveFilter()"); 9 } 10 11 }
4.FilterChian.java
1 public class FilterChain implements Filter { 2 3 private List<Filter> filters = new ArrayList<Filter>(); 4 int index = 0; //标记执行到第几个filter 5 6 //把函数的返回值设为FilterChain,返回this,就能方便链式编写代码 7 public FilterChain addFilter(Filter filter) { 8 filters.add(filter); 9 return this; 10 } 11 12 public void doFilter(Request request, Response response, FilterChain fc) { 13 if(index == filters.size()) return ; 14 Filter f = filters.get(index); 15 index++; 16 f.doFilter(request, response, fc); 17 } 18 19 }
5.Request.java
1 public class Request { 2 3 private String requestStr; 4 5 public String getRequestStr() { 6 return requestStr; 7 } 8 9 public void setRequestStr(String requestStr) { 10 this.requestStr = requestStr; 11 } 12 13 }
6.Response.java
1 public class Response { 2 3 private String responseStr; 4 5 public String getResponseStr() { 6 return responseStr; 7 } 8 9 public void setResponseStr(String responseStr) { 10 this.responseStr = responseStr; 11 } 12 13 }
7.Test.java
1 public class Test { 2 3 @org.junit.Test 4 public void testFilter(){ 5 6 String msg = "<html>敏感字眼</html>"; 7 8 Request request = new Request(); 9 request.setRequestStr(msg); 10 Response response = new Response(); 11 response.setResponseStr("response------------"); 12 13 FilterChain fc = new FilterChain(); 14 fc.addFilter(new HTMLFilter()).addFilter(new SensitiveFilter()); 15 16 fc.doFilter(request, response, fc); 17 System.out.println(request.getRequestStr()); 18 System.out.println(response.getResponseStr()); 19 20 } 21 22 }
三、执行结果