zoukankan      html  css  js  c++  java
  • 关于Listener

    Listener
      [1]监听器简介
        > Listener是JavaWeb中三大组件之一。Servlet、Filter、Listener
        > 三大组件都有的共同特点,都需要实现一个接口,并在web.xml文件配置。
        监听器:朝阳群众
        监听对象:明星
        监听的事件:干坏事
        调用方法:报警

      JavaWeb中的监听器的监听对象是谁?
        ServletContext
        HttpSession
        ServletRequest

      [2]监听器分类
         JavaWeb中的监听器共有三种,共8个监听器
          1)生命周期监听器,监听三个对象的创建和销毁的事件。

          2)属性监听器,监听三个对象中属性的变化。

          3)session对象监听器,将它session中的属性,以及session的活化和钝化。

      生命周期监听器
        ServletContextListener
        ServletContext生命周期监听器

        方法
          void contextDestroyed(ServletContextEvent sce)
           该方法在ServletContext对象销毁前调用

           void contextInitialized(ServletContextEvent sce)
           该方法在ServletContext对象创建之后调用。

          ServletContextEvent 对象可以获取到ServletContext对象
          ServletContextEvent.getServletContext();

        HttpSessionListener
        HttpSession生命周期监听器

        方法
          void sessionCreated(HttpSessionEvent se)
          该方法在Session创建时调用

          void sessionDestroyed(HttpSessionEvent se)
          在session销毁时调用

          HttpSessionEvent
          可以获取HttpSession对象

        ServletRequestListener

        方法
          void requestDestroyed(ServletRequestEvent sre)
           在request对象销毁时调用

          void requestInitialized(ServletRequestEvent sre)
          在request对象创建时调用

          ServletRequestEvent
          可以获取ServletContext对象和ServletRequest

      编写一个监听器的步骤:
        1.创建一个类并实现一个接口。
        2.在web.xml文件中注册监听器。

      属性监听器
        属性监听器,监听三个域中的属性的变化:添加一个属性,替换一个属性,移除一个属性
        ServletContextAttributeListener
        监听ServletContext中的属性的变化

        方法
          void attributeAdded(ServletContextAttributeEvent scab)
          当向ServletContext中添加属性时调用

          attributeRemoved(ServletContextAttributeEvent scab)
          移除属性时调用

          attributeReplaced(ServletContextAttributeEvent scab)
          替换一个属性时调用

       HttpSessionAttributeListener

        方法
          void attributeAdded(HttpSessionBindingEvent se)
          void attributeRemoved(HttpSessionBindingEvent se)
          void attributeReplaced(HttpSessionBindingEvent se)

             HttpSessionBindingEvent
            可以获取到属性名 getName()
            可以获取属性的旧值 getValue()
            可以获取到HttpSession对象 getSession();

      ServletRequestAttributeListener
        void attributeAdded(ServletRequestAttributeEvent srae)
        void attributeRemoved(ServletRequestAttributeEvent srae)
        void attributeReplaced(ServletRequestAttributeEvent srae)

     以下两个监听器监听session域中的属性的。


      HttpSessionBindingListener
         监听session域中某一个类的实例的添加和移除。
        该接口由JavaBean来实现,不需要再web.xml文件配置
          valueBound(HttpSessionBindingEvent event)
          当该类的实例,作为属性设置进session域中时调用
          valueUnbound(HttpSessionBindingEvent event)
          当该类的实例,从session域中被移除时调用

      HttpSessionActivationListener
        监听session域中的某类属性,和session一起活化和钝化的事件。
        该接口由JavaBean来实现,同样不需要再web.xml中配置,但是该JavaBean需要实现Serializable接口!


        void sessionDidActivate(HttpSessionEvent se)
        当前对象和session一起被活化到内存时调用

        void sessionWillPassivate(HttpSessionEvent se)
        当前对象和session一起钝化到硬盘时调用

  • 相关阅读:
    Android Studio 优秀插件: Parcelable Code Generator
    Android Studio 优秀插件:GsonFormat
    DrawerLayout(抽屉效果)
    Python拼接字符串的七种方式
    Python爬虫教程-使用chardet
    Python爬虫教程-实现百度翻译
    Tensorflow分布式部署和开发
    简单的Python GUI界面框架
    用keras构建自己的网络层 TensorFlow2.0教程
    Python GUI教程一:Hello World
  • 原文地址:https://www.cnblogs.com/alternative/p/7380227.html
Copyright © 2011-2022 走看看