解决post乱码的过滤器配置

首先,在web.xml中配置如下代码:

   
   
   EncodingFilter
   com.
sun.filter.GuoLvQi
   
   encoding
   utf-8
   

   
 
    
   EncodingFilter 
   /* 
   
 
   

然后,新建过滤器如下:package com.hygj.filter;
 
import java.io.IOException;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class GuoLvQi implements Filter{
 
    protected String encoding = null;          // 接收字符编码
    public void destroy() {
        // TODO Auto-generated method stub
        this.encoding = null;
    }
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        if (this.encoding != null)
        {
            request.setCharacterEncoding(this.encoding);
            response.setCharacterEncoding(this.encoding);
            System.out.println(this.encoding);
        }
        chain.doFilter(request, response);
         
    }
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        this.encoding = filterConfig.getInitParameter("encoding"); //此处为web.xml设置的字符格式
         
    }
 
 
}

你可能感兴趣的:(SpringMVC)