- 011 public class CharacterEncodingFilter implements Filter {
- 012 private FilterConfig config;
- 013 private String encoding = "ISO8859_1";
- 014
- 015 public void destroy() {
- 016 config = null;
- 017 }
- 018
- 019 public void doFilter(ServletRequest request,
- ServletResponse response, FilterChain chain)
- 020 throws IOException, ServletException {
- 021 request.setCharacterEncoding(encoding);
- 022 chain.doFilter(request, response);
- 023 }
- 024
- 025 public void init(FilterConfig config) throws ServletException {
- 026 this.config = config;
- 027 String param = config.getInitParameter("encoding");
- 028 if (param != null) {
- 029 encoding = param;
- 030 }
- 031 }
- 032 }
通过第025行~第031行的init()方法读取设在web.xml文件中encoding参数对应的参数值,该参数值设置编码方式,第019行~第023行的doFilter()设置POST表单传递的参数编码方式。