servlet filter中设置content-type无效

整个网站就这一个过滤器,我想设置全局的ContentType为application/json; charset=utf-8,但这样子无效,如果在servlet中设置,即可以正常使用(但这样意味着每个servlet都要设置下,太麻烦)。我的过滤器代码如下

public class GlobalFilter implements Filter {
    public void destroy() {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json; charset=utf-8");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
阅读 6.3k
1 个回答

filter 在前servlet在后吧

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进