EncodingFilter

Posted NEU-2015

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EncodingFilter相关的知识,希望对你有一定的参考价值。

1.EncodingFilter.java

 

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;
import javax.servlet.http.HttpServletRequest;

/**
 * Servlet Filter implementation class EncodingFilter
 */
public class EncodingFilter implements Filter {

    /**
     * Default constructor. 
     */
    public EncodingFilter() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        //1 创建我们包装之后的request对象
        ServletRequest req = new MyRequest((HttpServletRequest)request);
        //2放行(放行的request是我们包装后的)
        chain.doFilter(req, response);
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

 

2.MyRequest.java

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestWrapper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
//将已经是乱码的参数,重新编码回中文
public class MyRequest extends  HttpServletRequestWrapper{
    
    private Map<String,String[]> params =new HashMap<String, String[]>();
    
    public MyRequest(HttpServletRequest request) {
        super(request);
    // 在构造中获得封装参数的map, 将map中所有乱码解决
    //-----------------------------------------------
        //1 获得map
        Map<String,String[]> map = super.getParameterMap();
        //2 遍历map
        if(map!=null && map.size()>0){
            for(Entry<String, String[]> en : map.entrySet()){
                //3 取出每个键值对,并解决值得乱码(逆推法)
                String[] values = en.getValue();
                if(values!=null){
                String[] newValues = new String[values.length];
                    for(int i = 0; i < values.length ;i++){
                        try {
                            newValues[i] = new String(values[i].getBytes("ISO-8859-1"),"UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                    //4 将解决的键值对 放入新的map
                    params.put(en.getKey(), newValues);
                    }
            }
        }
        
    }

    @Override
    public String getParameter(String name) {
        String[] values = params.get(name);
        
        if(values==null){//获得的参数键不存在
            return null;
        }
        
        return values[0];
    }

    @Override
    public Map<String,String[]> getParameterMap() {
        
        return params;
    }
    
    @Override
    public String[] getParameterValues(String name) {
        return params.get(name);
    }

    

}

 

以上是关于EncodingFilter的主要内容,如果未能解决你的问题,请参考以下文章

tomcat异常[1]--java.lang.ClassNotFoundException: XXXX.EncodingFilter

Exception starting filter encodingFilter

servlet3.0 @webfilter 过滤顺序

JQuery发送ajax请求时中文乱码

使用spring框架自带的字符拦截器

使用spring框架自带的字符拦截器