首先写一个过滤器的类,如下:
1 package com.util; 2 3 import java.io.IOException; 4 import java.io.UnsupportedEncodingException; 5 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletRequestWrapper; 14 import javax.servlet.http.HttpServletResponse; 15 import javax.servlet.http.HttpSession; 16 17 18 //过滤器处理表单传到servlet的乱码问题 19 public class MyFilter implements Filter{ 20 //自写一个request换掉原来的request,重写里面的getParemeter方法,可以设置编码 21 class MyRequest extends HttpServletRequestWrapper{ 22 23 @Override 24 public String getParameter(String param) { 25 String value = null; 26 try { 27 //post 28 super.setCharacterEncoding(encoding);//把编码转换为encoding 29 value = super.getParameter(param); 30 if(super.getMethod().equalsIgnoreCase("GET")){ 31 if(value!=null){ 32 value = new String(value.getBytes("iso8859-1"),encoding); 33 } 34 } 35 } catch (UnsupportedEncodingException e) { 36 // TODO Auto-generated catch block 37 e.printStackTrace(); 38 } 39 return value; 40 } 41 42 public MyRequest(HttpServletRequest request) { 43 super(request); 44 } 45 46 } 47 protected String encoding=null; 48 public void destroy() { //销毁 49 // TODO Auto-generated method stub 50 this.encoding=null; 51 } 52 //对编码问题进行转换 53 public void doFilter(ServletRequest request, ServletResponse response, 54 FilterChain chain) throws IOException, ServletException { 55 // TODO Auto-generated method stub 56 response.setContentType("text/html;charset="+encoding); 57 //过滤未登录用户 58 HttpServletRequest req = (HttpServletRequest) request; 59 HttpServletResponse resp = (HttpServletResponse) response; 60 String path=req.getServletPath(); 61 String param=req.getQueryString(); 62 if(path!=null){ 63 path=path+"?"+param;//全请求路径 64 } 65 if(path.endsWith("myAddress")||path.endsWith("myJingDong")||path.indexOf("myShouCang")!=-1||path.endsWith("updateUser")||path.indexOf("showOrder")!=-1||path.indexOf("showValidOrder")!=-1||path.indexOf("showCancelOrder")!=-1||path.indexOf("fillOrder")!=-1){ 66 HttpSession session = req.getSession(); 67 String userName = (String) session.getAttribute("username"); 68 if(userName == null){ 69 session.setAttribute("url", path.replaceFirst("/", "")); 70 System.out.println(session.getAttribute("url")); 71 resp.sendRedirect("user.do?op=loginAction"); 72 return; 73 } 74 } 75 //把过滤器给下一个过滤器或资源处理器 76 chain.doFilter(new MyRequest((HttpServletRequest) request), response); 77 } 78 79 public void init(FilterConfig filterConfig) throws ServletException { 80 // TODO Auto-generated method stub 81 this.encoding=filterConfig.getInitParameter("encoding");//encoding在web.xml中指定 82 } 83 84 }
然后在web.xml对该过滤器进行注册和映射:
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.util.MyFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
上面写的过滤器MyFilter类,本来只能处理post提交的数据(post是先处理后接收,get是先接收后处理)。
但是MyFilter里面在对任何页面过滤的时候,来了一个偷梁换柱:把原来客户端请求的request给换掉了,换成自己定义的一个request了,即内部类MyRequest,不过该类要继承一个类HttpServletRequestWrapper。
在自定义的一个内部类MyRequest里面,实现了一个好强大的功能,就是重写了request的getParameter()方法。该方法里面即处理了post提交,又能处理get提交,返回的值就是处理后的值,所以该过滤器就能实现处理post和get提交的乱码问题!