Filter_Listener

过滤器 Filter @WebFilter(URL)

  1. 方法
    • void init(FilterConfig config) 初始化方法
    • void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) 对请求或响应过滤
    • void destroy() 销毁方法
  2. 多个filter,根据FilterMapping的顺序依次过滤
  3. 生命周期
    1. Server启动就init
    2. 过滤就doFilter
    3. 服务器关闭或应用被卸载就destroy
  4. FilterConfig 类似于ServletConfig
    // 获取Filter的名字
    public String getFilterName();
    // 获取Servlet上下文
    public ServletContext getServletContext();
    // 根据初始化参数的名称获取初始化参数的值
    public String getInitParameter(String paramName);
    // 获取所有初始化参数的名字
    public Enumeration getInitParameterNames();
    
  5. FilterChain
    // 放行
    // 相当于执行Serlvet中的Service(ServletRequest req, ServletResponse res)方法
    // 之后继续执行Filter类中的方法
    public void doFilter(ServletRequest req, ServletResponse res)
    
  6. 过滤器五种拦截行为
    • 过滤器默认拦截的是请求,但是在实际开发中,我们还有请求转发和请求包含,以及由服务器 出发调用的全局错误页面。默认情况下过滤器是不参数过滤的,要想使用,需要我们手动配置
    • 在配置文件中,filter-mapping 里使用 进行配置
      1. REQUEST 默认值,过滤请求
      2. ERROR 过滤全局错误页面,当服务器调用全局错误页面时,过滤器工作
      3. FORWORD 过滤请求转发,当请求转发时,过滤器工作 转发到Filter配置的URLpattern时
      4. INCLUDE 过滤请求包含,当请求包含时,过滤器工作。同转发只能过滤动态包含,JSP的include指令是静态包含
      5. ASYNC 过滤异步类型,它要求我们在Filter标签配置中开启异步支持

监听器 @WebListener

  1. 在程序中,我们可以对:对象的创建销毁,域对象中属性的变化,会话相关的内容进行监听
  2. Servlet规范中共计8个监听器,都是以接口形式提供,具体功能需要我们自己完成
  3. 分类
    • 监听对象创建和销毁
      1. ServletContextListener 用于监听ServletContext对象的创建和销毁
        • void contextInitialized(ServletContextEvent sce) 对象创建时执行该方法
        • void contextDestroyed(ServletContextEvent sce) 对象销毁时执行该方法
        • ServletContextEvent sce 代表事件对象
        • 事件对象中封装了事件源,也就是ServletContext
      2. HttpSessionListener 用于监听HttpSession对象的创建和销毁
        • void sessionCreated(HttpSessionEvent se) 对象创建时执行该方法
        • void sessionDestroyed(HttpSessionEvent se) 对象销毁时执行该方法
        • HttpSessionEvent se 代表事件对象
        • 事件对象中封装了事件源,也就是HttpSession
      3. ServletRequestListener 用于监听ServletRequest对象的创建和销毁
        • void requestInitialized(ServletRequestEvent sre) 对象创建时执行该方法
        • void requestDestroyed(ServletRequestEvent sre) 对象销毁时执行该方法
        • ServletRequestEvent sre 代表事件对象
        • 事件对象中封装了事件源,也就是ServletRequest
    • 监听域对象属性变化
      1. ServletContextAttributeListener 用于监听ServletContext应用域中属性的变化
        • void attributeAdded(ServletContextAttributeEvent scae) 域中添加属性时执行该方法
        • void attributeRemoved(ServletContextAttributeEvent scae) 域中移除属性时执行该方法
        • void attributeReplaced(ServletContextAttributeEvent scae) 域中替换属性时执行该方法
      2. HttpSessionAttributeListener 用于监听HttpSessio会话域中属性的变化
        • void attributeAdded(HttpSessionBindingEvent se) 域中添加属性时执行该方法
        • void attributeRemoved(HttpSessionBindingEvent se) 域中移除属性时执行该方法
        • void attributeReplaced(HttpSessionBindingEvent se) 域中替换属性时执行该方法
      3. ServletRequestAttributeListener 用于监听ServletContext应用域中属性的变化
        • void attributeAdded(ServletRequestAttributeEvent srae) 域中添加属性时执行该方法
        • void attributeRemoved(ServletRequestAttributeEvent srae) 域中移除属性时执行该方法
        • void attributeReplaced(ServletRequestAttributeEvent srae) 域中替换属性时执行该方法
    • 监听会话相关的感知型监听器 感知型监听器,不需要配置
      1. HttpSessionBindingListener 用于感知对象和会话域绑定的监听器
        • void valueBound(HttpSessionBindingEvent evnet) 添加数据到会话域中,执行该方法
        • void valueUnbound(HttpSessionBindingEvent evnet) 数据从会话域中移除,执行该方法
      2. HttpSessionActivationListener 用于感知会话域中对象钝化和活化的监听器
        • void sessionWillPassivate(HttpSessionEvent se) 会话域中数据钝化时执行该方法
        • void sessionDidActivate(HttpSessionEvent se) 会话域中数据活化时执行该方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值