处理中文的过滤器
package filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * 处理中文的过滤器 * @author Administrator * */ public class GbkFilter implements Filter { private String charcode; public void init(FilterConfig config) throws ServletException { System.out.println("gbk init ...."); charcode=config.getInitParameter("charcode"); //得到容器 config.getServletContext(); } public void destroy() { System.out.println("gbk destroy ...."); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("gbk 服务 ...."); request.setCharacterEncoding(charcode); response.setCharacterEncoding(charcode); //放行 有两种情况 下面可能是下一个过滤器 也有可能是资源 servlet ,jsp ,html,jpg.... chain.doFilter(request, response); } }