A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 魏先生 初级黑马   /  2017-12-27 16:34  /  709 人查看  /  0 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 小石姐姐 于 2017-12-27 18:59 编辑

        
### Filter过滤器的概述
        
        * 作用
                * 可以过滤从客户端向服务器发送的请求
        * 使用
                * IP过滤,自动登录,响应压缩,统一网站字符集
        * 入门使用(实现类+配置)
                * public class FilterDemo1 implements Filter {

                        public void init(FilterConfig filterConfig) throws ServletException {
                        
                        }
               
                        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                                        throws IOException, ServletException {
                                System.out.println("执行了");
                                chain.doFilter(request, response);
                        }
               
                        public void destroy() {
               
                        }
               
                }

                * <filter>
                          <filter-name>aaa</filter-name>
                          <filter-class>com.itheima.filter.FilterDemo1</filter-class>
                  </filter>
                  <filter-mapping>
                          <filter-name>aaa</filter-name>
                          <url-pattern>/demo1/demo1.jsp</url-pattern>
                  </filter-mapping>

### 过滤器的生命周期
        
        * 创建
                * 启动服务器
        * 销毁
                * 关闭服务器
               
### 过滤器的对象
        
        * FilterConfig
                * 过滤器的配置对象(和ServletContext对象类似)
        * Filter过滤器链(filterChain)
                * 过滤器链在职过滤器的执行顺序和<filtermapping>配置有关系,越往前就越早执行
                        * chain.doFilter(request, response);
                                * 放行,下方有过滤器则放至下一个过滤器,如果没有则放行至目标资源(jsp/servlet等)
### <dispatcher>的配置:
        * REQUEST        :默认值.
        * FORWARD        :转发.
        * INCLUDE        :包含.
        * ERROR                :错误页面跳转.(全局错误页面)

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马