• Servlet监听器笔记总结


    监听器Listener的概念

      监听器的概念很好理解,顾名思义,就是监视目标动作或状态的变化,目标一旦状态发生变化或者有动作,则立马做出反应.

      Servlet中的也有实现监听器的机制,就是Listener,Listener是一个实现特定接口的普通java程序,专门用来监听另一个java对象的方法调用或属性改变,当被监听对象发生变动时,监听器某个方法立即被执行.

    监听器Listener的使用场景

      理解了概念,接下来问题就来了,监听器Listener有哪些使用场景呢?

      在使用Spring框架开发javaWeb项目时,我们都会在web.xml中加入一个Listener,叫ContextLoaderListener.

     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

      它的作用是,在启动web容器时,自动装配ApplicationContext的配置信息,初始化好Spring的IOC容器,这样就可以在项目启动的时候马上就可以去IOC容器中取到我们需要的对象.

      这个ContextLoaderListener就扮演的是一个监听器的角色,它实现了一个ServletContextListener接口,ServletContextListener中有两个方法:

    public interface ServletContextListener extends EventListener {
        void contextInitialized(ServletContextEvent var1);
        void contextDestroyed(ServletContextEvent var1);
    }

      当ServletContext对象创建,即容器启动时,监听器监听到了这一动作,就会调用contextInitialized这个方法,完成ApplicationContext的装配初始化工作.

    /**
         * Initialize the root web application context.
         */
        public void contextInitialized(ServletContextEvent event) {
            this.contextLoader = createContextLoader();
            if (this.contextLoader == null) {
                this.contextLoader = this;
            }
            this.contextLoader.initWebApplicationContext(event.getServletContext());
        }

      initWebApplicationContext就是用来初始化IOC容器的方法.

      大概理通了这个流程,我们来看看更多Listener的使用场景:

      1.在系统启动时加载初始化信息

      2.统计网站的访问量

      3.统计在线人数和在线用户

      4.运用在一些框架(如Spring)中完成特定功能.

    使用Listener的步骤

    1.新建一个java类实现XXXListener接口,实现代码逻辑

    2.在Web.xml注册自己新建的Listener

    Listener的启动顺序

      Listener的启动优先级是大于过滤器的,即Listener>Filter>Servlet

      如果有很多Listener类,那么Listener的作用时机是根据你在web.xml中注册的顺序来决定的,即按照从上往下的顺序来加载.

    Servlet中常用的Listener接口

    1.按照域对象的创建和销毁来分类

    public class MyServletContextListener implements ServletContextListener{
    
    //servletContext创建时调动此方法(容器启动)
        public void contextInitialized(ServletContextEvent servletContextEvent) {
        }
    //servletContext销毁时调用此方法(容器关闭)
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
        }
    }

       相同类型的Listener的还有HttpSessionListener,ServletRequestListener.

    2.按照域中属性的增加移除来分类

    public class MyApplicationAttributeListener implements ServletContextAttributeListener {
       //当Application域中属性增加时调用此方法
        public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
        }
    //当Application域中属性移除的时候调用此方法
        public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
        }
    //当Application域中对象被更新时调用此方法
        public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
        }
    }

      相同类型的Listener还有HttpSessionAttributeListener,ServletRequestAttributeListener

    3.监听HttpSession域中某个具体对象状态的Listener

      这种Listener比较特别,以上介绍的几种都需要在web.xml中注册监听器,而这种不需要,为什么呢,看代码比较好理解:

    public class User implements HttpSessionBindingListener {
        private String username;
        private String password;
    //当session域中绑定了User对象时调用此方法
        @Override
        public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
        }
    //当session域中移除了User对象绑定时调用此方法
        @Override
        public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
        }
    }

    相同类型的还有还有一种 HttpSessionActivationListener,涉及session域中对象的序列化和反序列化.

  • 相关阅读:
    Sqlserver的Transaction做Rollback的时候要小心(转载)
    注意Sqlserver中使用with(nolock)后实际上还是会加架构锁,只是不对要查询的数据加S锁而已(转载)
    为什么Sql Server的查询有时候第一次执行很慢,第二次,第三次执行就变快了
    Sql Server 中如果使用TransactionScope开启一个分布式事务,使用该事务两个并发的连接会互相死锁吗
    Css中路径data:image/png;base64的用法详解 (转载)
    android获取mp4视频文件总时长和视频宽高<转>
    “Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle)instead”
    android 除法运算保留小数点
    Directshow 采集音视频数据H264+AAC+rtmp效果还不错
    VS2010中将CString转换为const char*
  • 原文地址:https://www.cnblogs.com/fingerboy/p/6184261.html
Copyright © 2020-2023  润新知