1 import java.io.IOException;
2 import javax.servlet.Filter;
3 import javax.servlet.FilterChain;
4 import javax.servlet.FilterConfig;
5 import javax.servlet.ServletException;
6 import javax.servlet.ServletRequest;
7 import javax.servlet.ServletResponse;
8
9 public class EncodingFilter implements Filter {
10
11 public void init(FilterConfig config) throws ServletException {}
12
13 public void destroy() {}
14
15 public void doFilter(ServletRequest request,
16 ServletResponse response,
17 FilterChain chain)
18 throws IOException, ServletException {
19
20 request.setCharacterEncoding("gb2312");
21 chain.doFilter(request, response);
22 }
23
24 }
在此EncodingFilter实现了Filter接口,Filter接口中定义的三个方法都要在EncodingFilter中实现,其中doFilter()的代码实现主要的功能:为请求设置gb2312编码并执行chain.doFilter()继续下面的操作。