11.6_Filter
一、什么是Filter? 过滤器 , 其实就是对客户端发出来的请求进行过滤。 浏览器发出, 然后服务器派servlet处理。 在中间就可以过滤, 其实过滤器起到的是拦截的作用。 二、作用 1. 对一些敏感词汇进行过滤 2. 统一设置编码 3. 自动登录 三、如何使用Filter 1. 定义一个类, 实现Filter public class FilterDemo implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("来到过虑器了。。。"); chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { } } 2. 注册过滤器 在web.xml里面注册,注册的手法与servlet基本一样。