zoukankan      html  css  js  c++  java
  • 【Web入门系列】初探监听器Listener

    定义

    监听器主要用来监一些特定对象的创建或销毁以及属性变化的,是一个实现特定接口的普通java类。

    按照监听的对象划分,可以分为ServletContext对象监听器,HttpSession对象监听器,ServletRequest对象监听器;按照监听的事件划分,可以分为对象自身的创建和销毁的监听器,对象中属性的创建和消除的监听器,session中某个对象的状态变化的监听器。

    ServletRequestListener、ServletContextListener、HttpSessionListener

    <listener>
        <listener-class>ysdrzp.listener.MyServletRequestListener</listener-class>
    </listener>
    <listener>
        <listener-class>ysdrzp.listener.MyServletContextListener</listener-class>
    </listener>
    <listener>
        <listener-class>ysdrzp.listener.MyHttpSessionListener</listener-class>
    </listener>
    /**
     * 监听ServletRequest对象的创建和销毁
     */
    public class MyServletRequestListener implements ServletRequestListener {
    
        @Override
        public void requestInitialized(ServletRequestEvent servletRequestEvent) {
            System.out.println("MyServletRequestListener.requestInitialized()");
        }
    
        @Override
        public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
            System.out.println("MyServletRequestListener.requestDestroyed()");
        }
    
    }
    /**
     * 监听ServletContext对象的创建和销毁
     */
    public class MyServletContextListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            System.out.println("MyServletContextListener.contextInitialized()");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            System.out.println("MyServletContextListener.contextDestroyed()");
        }
        
    }
    /**
     * 监听HttpSession对象的创建和销毁
     */
    public class MyHttpSessionListener implements HttpSessionListener {
    
        @Override
        public void sessionCreated(HttpSessionEvent httpSessionEvent) {
            System.out.println("MyHttpSessionListener.sessionCreated()");
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
            System.out.println("MyHttpSessionListener.sessionDestroyed()");
        }
        
    }

     ServletRequestAttributeListener、ServletContextAttributeListener、HttpSessionAttributeListener

    /**
     * ServletRequest域对象属性监听器
     */
    public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {
    
        @Override
        public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {
            System.out.println(servletRequestAttributeEvent.getName()+"属性增加");
            System.out.println(servletRequestAttributeEvent.getValue());
        }
    
        @Override
        public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {
            System.out.println(servletRequestAttributeEvent.getName()+"属性移除");
            System.out.println(servletRequestAttributeEvent.getValue());
        }
    
        @Override
        public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {
            System.out.println(servletRequestAttributeEvent.getName()+"属性改变");
            System.out.println(servletRequestAttributeEvent.getValue());
        }
    }
    /**
     * ServletContext域对象属性改变监听器
     */
    public class MyServletContextAttributeLinstener implements ServletContextAttributeListener {
    
        @Override
        public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
    
        }
    
        @Override
        public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
    
        }
    
        @Override
        public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
    
        }
    }
    /**
     * HttpSession域对象属性监听器
     */
    public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {
    
        @Override
        public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
    
        }
    
        @Override
        public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
    
        }
    
        @Override
        public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
    
        }
    }

    HttpSessionBindingListener

    public class User implements HttpSessionBindingListener {
    
        private String name;
    
        private String sex;
    
        public User(String name,String sex){
            this.name=name;
            this.sex=sex;
        }
    
        @Override
        public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
            System.out.println("User对象已经放入session中");
        }
    
        @Override
        public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
            System.out.println("User对象从session中移除");
        }
    }
  • 相关阅读:
    npm修改为淘宝源
    将蓝牙rssi(信号强度)转换成距离
    goland 可用注册码(license)
    用爬虫实现验证码识别并模拟登陆和cookie操作、代理操作、线程池
    爬虫概述
    初识ES()
    ansible中的playbook(剧本)
    ansible中File模块、Fetch模块、Yum模块、Pip模块、Service模块、Cron模块、User模块、Group模块
    ansible的安装与介绍、host-pattern格式、ansible的command模块、ansible的shell模块、ansible的script模块、ansible的copy模块
    Flask中的before_request装饰器和after_request装饰器以及WTForms组件
  • 原文地址:https://www.cnblogs.com/ysdrzp/p/9917772.html
Copyright © 2011-2022 走看看