浏览器发出的任何请求,通过过滤器统一处理中文乱码 。
@WebFilter("/*")
public class EncodingFilter implements
Filter {
public void init(FilterConfig config) throws ServletException {}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws ServletException, IOException {
// 1.设置请求解码方式 utf-8
req.setCharacterEncoding("utf-8");
// 2.设置响应 mime类型和编码方式
resp.setContentType("text/html;charset=utf-8");
// 放行
chain.doFilter(req, resp);
}
public void destroy() {}
}