六、  Java Listener

1.  Java Listener 简介

* Java Listener  1. Java Listener(即:Java 监听器):用于监听 ServletRequest、HttpSession、ServletContext 作用域对象的创建、销毁、及其属性修改变化  2. 在 Web 项目中可以有多个 Java 监听器  3. 应用场景:       a.统计在线人数       b.统计页面访问量       c.服务器加载时数据初始化

* Listener 的使用  1. 在 web.xml 中配置监听器:       <listener>         <listener-class>com.ncdx.listener.MyListener</listener-class>       </listener>

  2. 创建 Java 类,根据需求实现相应的接口:       1)ServletRequestListener 接口 //声明了 ServletRequest 作用域对象的创建、销毁的监听方法       2)ServletRequestAttributeListener 接口 //声明了 ServletRequest 作用域对象中属性的添加、修改、删除的监听方法

       3)HttpSessionListener 接口 //声明了 HttpSession 作用域对象的创建、销毁的监听方法       4)HttpSessionAttributeListener 接口 //声明了 HttpSession 作用域对象中属性的添加、修改、删除的监听方法

       5)ServletContextListener 接口 //声明了 ServletContext 作用域对象的创建、销毁的监听方法       6)ServletContextAttributeListener 接口 //声明了 ServletContext 作用域对象中属性的添加、修改、删除的监听方法 

  3. 重写相应的监听方法
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyListener implements ServletRequestListener,ServletRequestAttributeListener,
                                   HttpSessionListener,HttpSessionAttributeListener,
                                   ServletContextListener,ServletContextAttributeListener{

    @Override//监听request对象销毁
    public void requestDestroyed(ServletRequestEvent sre) {

    }

    @Override//监听request对象创建
    public void requestInitialized(ServletRequestEvent sre) {

    }

    @Override//监听request作用域数据的添加
    public void attributeAdded(ServletRequestAttributeEvent srae) {

    }

    @Override//监听request作用域数据的删除
    public void attributeRemoved(ServletRequestAttributeEvent srae) {

    }

    @Override//监听request作用域数据的修改
    public void attributeReplaced(ServletRequestAttributeEvent srae) {

    }

/*.....................................................................*/

    @Override//监听session对象的创建
    public void sessionCreated(HttpSessionEvent se) {

    }

    @Override//监听session对象的销毁
    public void sessionDestroyed(HttpSessionEvent se) {

    }

    @Override//监听session作用域数据的添加
    public void attributeAdded(HttpSessionBindingEvent se) {

    }

    @Override//监听session作用域数据的删除
    public void attributeRemoved(HttpSessionBindingEvent se) {

    }

    @Override//监听session作用域数据的更改
    public void attributeReplaced(HttpSessionBindingEvent se) {

    }

/*.........................................................................*/

    @Override//监听application对象销毁
    public void contextDestroyed(ServletContextEvent sce) {

    }

    @Override//监听application对象创建
    public void contextInitialized(ServletContextEvent sce) {

    }

    @Override//监听application作用域数据的添加
    public void attributeAdded(ServletContextAttributeEvent scae) {

    }

    @Override//监听application作用域数据的删除
    public void attributeRemoved(ServletContextAttributeEvent scae) {

    }

    @Override//监听application作用域数据的更改
    public void attributeReplaced(ServletContextAttributeEvent scae) {

    }

}

Listener

2.  Java 监听器的实例

/**
 * 模拟统计在线人数
 */

public class MyListener implements ServletRequestListener,ServletRequestAttributeListener,
                                   HttpSessionListener,HttpSessionAttributeListener,
                                   ServletContextListener,ServletContextAttributeListener{

    @Override
    public void contextInitialized(ServletContextEvent sce) {//在ServletContext对象中存储变量用来统计在线人数
        //获取ServletContext
        ServletContext sc = sce.getServletContext();
        //在ServletContext对象中存储变量并初始化
        sc.setAttribute("count", 0);
    }

    @Override
    public void sessionCreated(HttpSessionEvent se) {//session被创建时人数自增
        //获取SerevletContext对象
        ServletContext sc = se.getSession().getServletContext();
        //获取在线人数的变量
        int count = (int)sc.getAttribute("count");
        //存储
        sc.setAttribute("count", ++count);
        //显示在线人数
        System.out.println("目前在线人数:" + se.getSession().getServletContext().getAttribute("count"));
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {//session被销毁时人数自减
        //获取SerevletContext对象
        ServletContext sc = se.getSession().getServletContext();
        //获取在线统计人数的变量
        int count = (int)sc.getAttribute("count");
        //存储
        sc.setAttribute("count", --count);
        //显示在线人数
        System.out.println("目前在线人数:" + se.getSession().getServletContext().getAttribute("count"));
    }

}

Java Listener的更多相关文章

  1. Java Listener pattern 监听者模式

    Java Listener pattern 监听者模式 2016-5-12 监听者模式(观察者模式)能降低对象之间耦合程度.为两个相互依赖调用的类进行解耦. 便于进行模块化开发工作.不同模块的开发者可 ...

  2. java listener实现定时任务

    使用java ServletContextListener 实现各种简单定时任务. 1. 创建ServletContextListener,在3.0版本的web.xml中不再需要添加listener的 ...

  3. Java ---Listener监听器

    在我们的web容器中,一直不断的触发着各种事件,例如:web应用启动和关闭,request请求到达和结束等.但是这些事件通常对于开发者来说是透明的,我们可以根据这些接口开发符合我们自身需求的功能.在w ...

  4. Java Listener中Spring接口注入的使用

    在项目中使用Spring通常使用他的依赖注入可以很好的处理,接口与实现类之间的耦合性,但是通常的应用场景中都是Service层和DAO层,或者web层的话, 也是与Strust2来整合,那么如何在Li ...

  5. JavaWeb Listener

    1. 监听器概述 1.1. 什么是监听器 做过Swing或者AWT图像界面Java程序开发的话,应该对Listener与Event非常熟悉.Swing或者AWT中通过Listener与Event来处理 ...

  6. Android5.1图库Gallery2代码分析数据加载流程

    图片数据加载流程. Gallery---->GalleryActivity------>AlbumSetPage------->AlbumPage--------->Photo ...

  7. 使用Mongodb+Shiro+SpringMVC实现动态权限分配

    此次的文档只对Mongodb整合Shiro并且实现动态权限分配做整理,其它的内容以后会补上. 第一步.创建在web.xml中配置 Spring .Shiro shiroFilter 过滤器是用来将请求 ...

  8. Spring boot 实现高吞吐量异步处理(适用于高并发场景)

    技术要点 org.springframework.web.context.request.async.DeferredResult<T> 示例如下: 1.   新建Maven项目  asy ...

  9. Spring boot 集成Kafka

    搭建Kafka集群,参考: https://www.cnblogs.com/jonban/p/kafka.html 源码示例如下: 1.新建 Maven 项目 kafka 2.pom.xml < ...

随机推荐

  1. 基于c开发的全命令行音频播放器

    cmus是一个内置了音频播放器的强大的音乐文件管理器.用它的基于ncurses的命令行界面,你可以浏览你的音乐库,并从播放列表或队列中播放音乐,这一切都是在命令行下. Linux上安装cmus 首先, ...

  2. 解决升Win10系统后VMware虚拟机不能联网的问题

    刚升级到Win10系统,打开虚拟机发现不能联网,其实是系统服务项里缺少两个用到的服务,不能联网了,下面教大家解决联网问题. 1.打开VMware虚拟机主页,点击“编辑——虚拟网络编辑器”. 2.点击左 ...

  3. ETH&EOS开发资源及工具集合(完整汇总版)

    ETH&EOS开发资源及工具集合(完整汇总版) 3113 ETH开发资源篇 一.开发语言 ·         Solidity - 官方推荐以太坊智能合约开发语言,也是目前最为主流的智能合约语 ...

  4. winform 更新文件上传(一)

    using Common; using DevExpress.XtraEditors; using FileModel.UpLoad; using System; using System.Colle ...

  5. 【miscellaneous】如何利用硬盘号和CPU序列号为软件加密

    原文:http://www.jiamisoft.com/blog/index.php/3469-yingpanhaocpuruanjianjiami.html 计算机软件是一种特殊的产品,为了防止软件 ...

  6. C/C++.字符串分割

    1.ZC:只测试使用了 自己改编的函数SplitStr_ZZ(...),其它的 未测试 #include <string> #include <vector> #include ...

  7. Charles系列一:Charles功能介绍、下载安装和界面简介

    一:Charles主要功能介绍 Charles是一个HTTP代理/HTTP监视器/反向代理,使开发和测试人员能够查看机器和Internet之间所有的HTTP和SSL/HTTPS流量,这包括请求,响应. ...

  8. JavaSE基础(五)--Java运算符

    Java 运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运算符 ...

  9. 服务器TIME_WAIT和CLOSE_WAIT区别及解决方案

    系统上线之后,通过如下语句查看服务器时,发现有不少TIME_WAIT和CLOSE_WAIT. netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) ...

  10. Python 解leetcode:3. Longest Substring Without Repeating Characters

    题目描述:求一个字符串的不含重复字符的最长连续子串的长度: 思路: 使用一个哈希表保存字符出现的位置: 使用left和right分别表示子串的最左和最右字符的下标: 遍历字符串,如果当前字符在哈希表中 ...