首页 > 代码库 > 过滤器--过滤编码

过滤器--过滤编码

  • 在servlet3.0中可以在页面配置
     1 @WebFilter( 2         filterName= "encodingFilter" ,  3         urlPatterns = {"/*"} ,  4         initParams = { @WebInitParam(name ="encoding" , value= "http://www.mamicode.com/utf-8")} , 5         asyncSupported = true 6         ) 7 public class EncodingFilter implements Filter{ 8     private String encoding = null; 9     @Override10     public void destroy() {11         12         13     }14 15     @Override16     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     @Override25     public void init(FilterConfig config) throws ServletException {26         encoding = config.getInitParameter("encoding");27         System.out.println("过滤字符编码");28     }29 30 }

     

过滤器--过滤编码