1 @WebFilter(
2 filterName= "encodingFilter" ,
3 urlPatterns = {"/*"} ,
4 initParams = { @WebInitParam(name ="encoding" , value= "utf-8")} ,
5 asyncSupported = true
6 )
7 public class EncodingFilter implements Filter{
8 private String encoding = null;
9 @Override
10 public void destroy() {
11
12
13 }
14
15 @Override
16 public void doFilter(ServletRequest request, ServletResponse response,
17 FilterChain chain) throws IOException, ServletException {
18 request.setCharacterEncoding(encoding);
19 response.setCharacterEncoding(encoding);
20 response.setContentType("text/html");
21 chain.doFilter(request, response);
22 }
23
24 @Override
25 public void init(FilterConfig config) throws ServletException {
26 encoding = config.getInitParameter("encoding");
27 System.out.println("过滤字符编码");
28 }
29
30 }