利用Filter解决Post请求中文乱码

利用Filter解决Post请求中文乱码

一、创建一个拦截所有请求的拦截器

@WebFilter(filterName = "login",
        dispatcherTypes = DispatcherType.REQUEST,
        urlPatterns = {"/*"})
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    }

    @Override
    public void destroy() {

    }
}

二、进行统一响应编码处理

@Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletResponse.setCharacterEncoding("UTF-8");
    }
利用Filter解决Post请求中文乱码利用Filter解决Post请求中文乱码 我好凶的 发布了8 篇原创文章 · 获赞 0 · 访问量 70 私信 关注
上一篇:Servlet 学习笔记


下一篇:HTTP协议