filter(过滤器) 解决 response(响应)中文乱码 及 post 请求乱码

@WebFilter(filterName = "EncodingFilter",urlPatterns = "/*")
public class EncodingFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //1.将父接口转换成子接口
        //ServletRequest父接口,HttpServletRequest子接口
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //2.判断请求类型,如果是post请求,解决post乱码问题
        String method = request.getMethod();
        if(method.equalsIgnoreCase("post")){
            request.setCharacterEncoding("utf8");
        }
        //3.解决response输出中文乱码问题
        response.setContentType("text/html;charset=utf8");
        chain.doFilter(request, response);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}

你可能感兴趣的:(filter(过滤器) 解决 response(响应)中文乱码 及 post 请求乱码)