解决表单post传递乱码

利用filter过滤器将字符编码转变为utf-8。

解决表单post传递乱码_第1张图片
过滤器位置

1.新建EncodingFilter.java文件

package encoding;

import java.io.*;
import javax.servlet.*;

public class EncodingFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
        } catch (Exception e) {
        }

        chain.doFilter(request, response);
    }

    public void destroy() {

    }
}

2.修改web.xml文件(如果没有就自己新建一个)



    
        encoding
        encoding
        encoding.EncodingFilter
    
    
        encoding
        /*
    


你可能感兴趣的:(解决表单post传递乱码)