字体替换过滤器继承HttpServletRequestWrapper,重定义了: getParameter(),getParameterValues(),getParameterMap(),增强字节替换功能,满足更多的请求情况.
一: 字节替换过滤器:
1 package cc.openhome.controler; 2 3 import javax.servlet.*; 4 import javax.servlet.annotation.WebFilter; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 9 @WebFilter("/*") 10 public class HtmlFilter implements Filter { 11 @Override 12 public void init(FilterConfig filterConfig) throws ServletException { 13 14 } 15 16 @Override 17 public void doFilter(ServletRequest sreq, ServletResponse sresp, FilterChain chain) throws IOException, ServletException { 18 HttpServletRequest req = (HttpServletRequest) sreq; 19 HttpServletResponse resp = (HttpServletResponse) sresp; 20 MyHtmlRequest mreq = new MyHtmlRequest(req); 21 chain.doFilter(mreq,resp); 22 } 23 @Override 24 public void destroy() { 25 26 } 27 }
字节替换处理部分:
1 package cc.openhome.controler; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletRequestWrapper; 5 import java.util.*; 6 7 public class MyHtmlRequest extends HttpServletRequestWrapper { 8 9 private HttpServletRequest req; 10 public MyHtmlRequest(HttpServletRequest req){ 11 12 super(req); 13 this.req = req; 14 } 15 16 public String getParameter(String name){ 17 String value = getRequest().getParameter(name); 18 if(value == null){ 19 return null; 20 } 21 return filter(value); 22 } 23 24 public String [] getParameterValues(String name){ 25 String [] values = getRequest().getParameterValues(name); 26 String [] res = new String[values.length]; 27 if(values == null){ 28 return null; 29 } 30 for(int i = 0;i < values.length;i++){ 31 res[i] = filter(values[i]); 32 } 33 return res; 34 35 } 36 37 public Map<String,String[]> getParameterMap(){ 38 Map<String,String[]> values = getRequest().getParameterMap(); 39 Map<String,String[]> res = new HashMap<String, String[]>(); 40 List <String> listv= new ArrayList<String>(); 41 String [] arrv; 42 for(Iterator<String> it = values.keySet().iterator();it.hasNext();){ 43 String k = it.next(); 44 String [] v = values.get(k); 45 if(v == null || v.length == 0){ 46 continue; 47 } 48 else { 49 for (int i = 0;i < v.length;i++){ 50 listv.add(filter(v[i])); 51 } 52 arrv = new String[listv.size()]; 53 arrv = listv.toArray(arrv); 54 } 55 res.put(k,arrv); 56 } 57 if(res == null){ 58 return null; 59 } 60 return res; 61 } 62 63 public String filter(String message){ 64 if(message == null){ 65 return null; 66 } 67 char content[] = new char[message.length()]; 68 message.getChars(0,message.length(),content,0); 69 StringBuilder result = new StringBuilder(content.length + 50); 70 for(int i = 0;i < content.length;i++){ 71 switch (content[i]){ 72 case '<': 73 result.append("<"); 74 break; 75 case '>': 76 result.append(">"); 77 break; 78 case '&': 79 result.append("&"); 80 break; 81 case '"': 82 result.append("""); 83 break; 84 default: 85 result.append(content[i]); 86 } 87 } 88 return result.toString(); 89 } 90 }
对请求参数进行扩充,Filter方法处理请求到值.Filter方法可以扩充为功能更强大的方法.
二,总结:
(1).对getParameter(),getParameterValues(),getParameterMap(),方法有了更深入的理解.对于前端传入的复杂数据类型如checkbox数据,密码用户名等复杂数据类型处理
有了概念.
(2),首次使用List容器.