使用filter对请求设置编码
一、复习
加强方法:
1)继承
2)装饰模式
3)动态代理。
使用装饰模式:
1)要求装饰者和被装饰者实现同一个接口或者继承同一个类。
2)装饰者要求被装饰者的引用。
3)对于要加强的方法进行加强,对于不需要的加强,执行原先的方法。
二、需求分析
1)使用filter来对所有的请求进行过滤。需要设置的url-pattern设置成/* 并且位置是在所有的filter的过滤器(filter-mapping)的前面。
2)对原先的方法进行加强。
1、加强的方法---request类-->通过filter的放行方法 重写request。
自己写的版本:
1 package jd.com.coding; 2 3 import javax.servlet.*; 4 import javax.servlet.annotation.WebFilter; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletRequestWrapper; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 10 @WebFilter(filterName = "FilterAllSeting") 11 public class FilterAllSeting implements Filter { 12 public void destroy() { 13 } 14 15 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { 16 HttpServletRequest re=(HttpServletRequest) req; 17 HttpServletResponse res=(HttpServletResponse) resp; 18 chain.doFilter(new SetCoding(re), resp); 19 } 20 21 public void init(FilterConfig config) throws ServletException { 22 } 23 24 } 25 26 class SetCoding extends HttpServletRequestWrapper{ 27 private HttpServletRequest request; 28 29 30 public SetCoding(HttpServletRequest request){ 31 super(request); 32 this.request=request; 33 } 34 35 @Override 36 public String getParameter(String name) { 37 String method=request.getMethod(); 38 String val=request.getParameter(name); 39 40 41 42 if( method.equalsIgnoreCase("get")) 43 { try { 44 return new String(val.getBytes("ISO_8859_1"), "UTF-8"); 45 }catch (IOException ex){ 46 ex.printStackTrace(); 47 } 48 } 49 50 else if(method.equalsIgnoreCase("POST")){ 51 try { 52 request.setCharacterEncoding("utf-8"); 53 }catch (IOException ex){ 54 ex.printStackTrace(); 55 } 56 57 } 58 return super.getParameter(name); 59 } 60 }
最终版本:
1 package cn.itcast.web.filter; 2 3 import java.io.IOException; 4 import java.io.UnsupportedEncodingException; 5 import java.util.Map; 6 7 import javax.servlet.Filter; 8 import javax.servlet.FilterChain; 9 import javax.servlet.FilterConfig; 10 import javax.servlet.ServletException; 11 import javax.servlet.ServletRequest; 12 import javax.servlet.ServletResponse; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletRequestWrapper; 15 import javax.servlet.http.HttpServletResponse; 16 /** 17 * 统一编码 18 * @author Administrator 19 * 20 */ 21 public class EncodingFilter implements Filter { 22 23 @Override 24 public void init(FilterConfig filterConfig) throws ServletException { 25 // TODO Auto-generated method stub 26 27 } 28 29 @Override 30 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { 31 //1.强转 32 HttpServletRequest request=(HttpServletRequest) req; 33 HttpServletResponse response=(HttpServletResponse) resp; 34 35 //2.放行 36 chain.doFilter(new MyRequest(request), response); 37 } 38 39 @Override 40 public void destroy() { 41 // TODO Auto-generated method stub 42 43 } 44 45 } 46 class MyRequest extends HttpServletRequestWrapper{ 47 private HttpServletRequest request; 48 private boolean flag=true; 49 50 51 public MyRequest(HttpServletRequest request) { 52 super(request); 53 this.request=request; 54 } 55 56 @Override 57 public String getParameter(String name) { 58 if(name==null || name.trim().length()==0){ 59 return null; 60 } 61 String[] values = getParameterValues(name); 62 if(values==null || values.length==0){ 63 return null; 64 } 65 66 return values[0]; 67 } 68 69 @Override 70 /** 71 * hobby=[eat,drink] 72 */ 73 public String[] getParameterValues(String name) { 74 if(name==null || name.trim().length()==0){ 75 return null; 76 } 77 Map<String, String[]> map = getParameterMap(); 78 if(map==null || map.size()==0){ 79 return null; 80 } 81 82 return map.get(name); 83 } 84 85 @Override 86 /** 87 * map{ username=[tom],password=[123],hobby=[eat,drink]} 88 */ 89 public Map<String,String[]> getParameterMap() { 90 91 /** 92 * 首先判断请求方式 93 * 若为post request.setchar...(utf-8) 94 * 若为get 将map中的值遍历编码就可以了 95 */ 96 String method = request.getMethod(); 97 if("post".equalsIgnoreCase(method)){ 98 try { 99 request.setCharacterEncoding("utf-8"); 100 return request.getParameterMap(); 101 } catch (UnsupportedEncodingException e) { 102 // TODO Auto-generated catch block 103 e.printStackTrace(); 104 } 105 }else if("get".equalsIgnoreCase(method)){ 106 Map<String,String[]> map = request.getParameterMap(); 107 if(flag){ 108 for (String key:map.keySet()) { 109 String[] arr = map.get(key); 110 //继续遍历数组 111 for(int i=0;i<arr.length;i++){ 112 //编码 113 try { 114 arr[i]=new String(arr[i].getBytes("iso8859-1"),"utf-8"); 115 } catch (UnsupportedEncodingException e) { 116 e.printStackTrace(); 117 } 118 } 119 } 120 flag=false; 121 } 122 //需要遍历map 修改value的每一个数据的编码 123 124 return map; 125 } 126 127 return super.getParameterMap(); 128 } 129 130 }
学习是一种态度,坚持是质变的利器!