zoukankan      html  css  js  c++  java
  • JavaWeb之监听器

    时间:2016-12-13 00:58

    ——JavaWeb监听器概述

    在JavaWeb中被监听的事件源为:ServletContext、HttpSession、ServletRequest,即三大域对象。
        *   监听域对象“创建”与“销毁”的监听器。
        *   监听域对象“操作域属性”的监听器。
        *   监听HttpSession的监听器。


    监听器:
        它是一个接口,内容由程序员实现。
        它需要注册,例如注册在按钮上(ActionListener)。
        监听器中的方法,会在特殊事件发生时被调用。(SAX解析XML)


    观察者:
        事件
        事件源
        监听器

    JavaWeb中的监听器:
        事件源:三大域
            1)ServletContext
                >   生命周期监听
                    *   ServletContextListener,它有两个方法, 一个在出生时调用,一个在死亡时调用。
                    *   void contextInitialized(ServletContextEvent sc):创建ServletContext时执行。
                    *   void contextDestroyed(ServletContextEvent sc):销毁ServletContext时执行。

                >   属性监听:
                    *   ServletContextAttributeListener,它有三个方法,一个在添加属性时调用,一个在修改属性时调用,一个在移除属性时调用。
                    *   void attributeAdded(ServletContextAttributeEvent scab)
                    *   void attributeRemoved(ServletContextAttributeEvent scab)
                    *   void attributeReplaced(ServletContextAttributeEvent scab)

            2)HttpSession
                >   生命周期监听
                    *   HttpSessionListener,它有两个方法, 一个在出生时调用,一个在死亡时调用。
                    *   void sessionCreated(HttpSessionEvent se):创建Session时执行
                    *   void sessionDestroyed(HttpSessionEvent se):销毁Session时执行

                >   属性监听:
                    *   HttpSessionAttributeListener,它有三个方法,一个在添加属性时调用,一个在修改属性时调用,一个在移除属性时调用。
                    *   void attributeAdded(HttpSessionBindingEvent se)
                    *   void attributeRemoved(HttpSessionBindingEvent se)
                    *   void attributeReplaced(HttpSessionBindingEvent se)


            3)ServletRequest
                >   生命周期监听
                    *   ServletRequestListener,它有两个方法, 一个在出生时调用,一个在死亡时调用。
                    *   void requestInitialized(ServletRequestEvent sre):创建request时执行。
                    *   void requestDestroyed(ServletRequestEvent sre):销毁request时执行。

                >   属性监听:
                    *   ServletRequestAttributeListener,它有三个方法,一个在添加属性时调用,一个在修改属性时调用,一个在移除属性时调用。
                    *   void attributeAdded(ServletRequestAttributeEvent srae)
                    *   void attributeRemoved(ServletRequestAttributeEvent srae)
                    *   void attributeReplaced(ServletRequestAttributeEvent srae)

    ——JavaWeb中编写监听器


    1、写一个监听器类类,要求必须实现某个监听器的接口。


        import javax.servlet.ServletContextEvent;
        import javax.servlet.ServletContextListener;
     
        /**
         * ServletContext生命周期监听
         * 
         * 可以在这个监听器中存放一些需要在服务器启动时就需要运行的代码
         * 
         * @author 31067
         * 
         */
        public class AListener implements ServletContextListener {
     
            @Override
            public void contextInitialized(ServletContextEvent arg0) {
                System.out.println("出生前");
            }
     
            @Override
            public void contextDestroyed(ServletContextEvent arg0) {
                System.out.println("死之后");
            }
        }


    2、在web.xml中来完成注册。

        <listener>
            <listener-class>com.wyc.web.listener.AListener</listener-class>
        </listener>


    ——监听器、事件对象及方法

    ServletContext:

        ServletContextListenr:
            *   void contextInitialized(ServletContextEvent sc):创建ServletContext时执行。
            *   void contextDestroyed(ServletContextEvent sc):销毁ServletContext时执行。

        ServletContextEvent:
            ServletContext getServletContext()

        ServletContextAttributeListener:
            在ServletContext域进行增、删、改属性时调用该方法。 
            *   void attributeAdded(ServletContextAttributeEvent scab)
            *   void attributeRemoved(ServletContextAttributeEvent scab)
            *   void attributeReplaced(ServletContextAttributeEvent scab)

        ServletContextAttributeEvent:
            *   String getName()
            *   Object getValue()
            *   ServletContextgetServletContext()

    HttpSession:

        HttpSessionListener:
            *   void sessionCreated(HttpSessionEvent se):创建Session时执行
            *   void sessionDestroyed(HttpSessionEvent se):销毁Session时执行

        HttpSessionEvent:
            *   HttpSession getSession();

        HttpSessionAttributeListener:
            在ServletContext域进行增、删、改属性时调用该方法。 
            *   void attributeAdded(HttpSessionBindingEvent se)
            *   void attributeRemoved(HttpSessionBindingEvent se)
            *   void attributeReplaced(HttpSessionBindingEvent se)

        HttpSessionBindingEvent:
            *   String getName()
            *   Object getValue()
            *   HttpSession getSession()

    ServletRequest:

        ServletRequestListener:
            *   void requestInitialized(ServletRequestEvent sre):创建request时执行。
            *   void requestDestroyed(ServletRequestEvent sre):销毁request时执行。

        ServletRequestEvent:
            *   ServletContext getServletContext();
            *   ServletRequest getServletRequest();

        ServletRequestAttributeListener:
            在ServletContext域进行增、删、改属性时调用该方法。 
            *   void attributeAdded(ServletRequestAttributeEvent srae)
            *   void attributeRemoved(ServletRequestAttributeEvent srae)
            *   void attributeReplaced(ServletRequestAttributeEvent srae)

        ServletRequestAttributeEvent:
            *   String getName()
            *   Object getValue()
            *   ServletRequest getServletRequest()
            *   ServletContext getServletContext()

    ——感知监听(都与HttpSession相关)

        还有两个与HttpSession相关的特殊监听器,这两个监听器的特点如下:
            *   都与HttpSession相关
            *   不需要在web.xml文件中进行相关配置
            *   这两个监听器不是给Session添加,而是给JavaBean添加, 即让JavaBean类实现该监听器接口,然后再把JavaBean对象添加到Session域中。

        下面对这两个监听器介绍一下:

            1、HttpSessionBindingListener:
                当某个类实现了该接口后,可以感知本类对象被添加到Session中,以及感知从Session域中移除。例如让Person类实现了HttpSessionBindingListener接口,那么当把Person对象添加到Session域中,或者把Person对象从Session域中移除时会调用以下两个方法:
                >   public void valueBound(HttpSessionBindingEvent event)
                    当把监听器对象添加到session中会调用监听器对象的本方法。

                >   public void valueUnbound(HttpSessionBindingEvent event)
                    当把监听器对象从Session中移除时会调用监听器对象的本方法。

    public class User implements HttpSessionBindingListener, HttpSessionActivationListener  {
        /**
         * 绑定时调用
         */
        @Override
        public void valueBound(HttpSessionBindingEvent arg0) {
            System.out.println("被绑定");
        }
     
        /**
         * 解绑时调用
         */
        @Override
        public void valueUnbound(HttpSessionBindingEvent arg0) {
            System.out.println("被解绑");
        }
    }



            2、HttpSessionActivationListener:
                Tomcat会在Session长时间不被使用时钝化Session对象,所谓钝化Session就是把Session通过序列化的方式保存到本地硬盘文件中,当用户再使用Session时,Tomcat会把钝化的Session对象再次活化,所谓活化就是把硬盘中的Session再反序列化回到内存中。当Session被Tomcat钝化时,Session中存储的对象也被钝化,当Session被活化时,也会把Session中存储的对象活化,如果某个类实现了HttpSessionActivationListener接口后,当对象随着Session被钝化和活化时,会调用以下两个方法:
                >   public void sessionWillPassivate(HttpSessionEvent event)
                    当对象感知被活化时调用本方法。

                >   public void sessionDidActivate(HttpSessionEvent event)
                    当对象感知被钝化时调用本方法。


        HttpSessionActivationListener监听器与HttpSessionBindingListener监听器相似,都是感知型的监听器,例如让Person类实现了HttpSessionActivationListener监听器接口,并把Person对象添加到了Session中后,当Tomcat钝化Session时,同时也会钝化Session中的Person对象,这时Person对象就会感知到自己被钝化了,然后就调用Person对象的sessionWillPassivate()方法,当用户再次使用Session时,Tomcat会活化Session,这时Person会感知到自己被活化,然后调用sessionDidActivate()方法。

        注意:
            钝化和活化Session,其实就是使用序列化和反序列化技术把Session从内存中保存到硬盘,和把session从硬盘加载到内存的过程,这说明如果Person类没有实现Serializable(可序列化接口),那么当Session被钝化时就不会钝化Person了,而是把Person从Session中移除再钝化Session,这就说明Session活化后,Session中就不会存在Person对象了。

        步骤:
            配置Tomcat钝化Session的参数,把下面的配置放到tomcatconfcatalinalocalhost目录下,文件名称为项目名称。
            如果将配置信息写到:confcontext.xml文件中,则对全部项目都会生效。

            图片

            指定最大不活动时间为1,保存的文件夹名称为mysession。

            访问项目的index.jsp页面,这会使Tomcat创建一个Session对象,然后等待一分钟,查看TomcatworkCatalinalocalhostListenerDemomysession目录下是否会生成该文件,如果生成了,说明钝化Session的配置成功了。

        /**
       *任何想要被序列化的类,必须要实现序列化接口,否则该类不会随Session被序列化保存到本地硬盘
         * @author 31067
         *
         */
        public class User implements HttpSessionBindingListener, HttpSessionActivationListener, Serializable {
            /**
             * 被活化
             */
            @Override
            public void sessionDidActivate(HttpSessionEvent arg0) {
                System.out.println("被活化");
            }
            /**
             * 钝化时调用
             */
            @Override
            public void sessionWillPassivate(HttpSessionEvent arg0) {
                System.out.println("被钝化");
            }
        }
  • 相关阅读:
    ES6 => 箭头函数
    从零开始, 探索transition(vue-2.0)
    从零开始,零基础,一点一点探索vue-router(vue2.0)
    解决Vue请求 ‘No 'Access-Control-Allow-Origin' header is present on the requested resource’错误
    超详细,用canvas在微信小程序上画时钟教程
    钱兔
    天天飞燕
    小鱼
    键盘处理
    兼容iOS 10 资料整理笔记
  • 原文地址:https://www.cnblogs.com/wwwwyc/p/6375330.html
Copyright © 2011-2022 走看看