整个网站就这一个过滤器,我想设置全局的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 {
}
}
filter 在前servlet在后吧