Java Listener
六、 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的更多相关文章
- Java Listener pattern 监听者模式
Java Listener pattern 监听者模式 2016-5-12 监听者模式(观察者模式)能降低对象之间耦合程度.为两个相互依赖调用的类进行解耦. 便于进行模块化开发工作.不同模块的开发者可 ...
- java listener实现定时任务
使用java ServletContextListener 实现各种简单定时任务. 1. 创建ServletContextListener,在3.0版本的web.xml中不再需要添加listener的 ...
- Java ---Listener监听器
在我们的web容器中,一直不断的触发着各种事件,例如:web应用启动和关闭,request请求到达和结束等.但是这些事件通常对于开发者来说是透明的,我们可以根据这些接口开发符合我们自身需求的功能.在w ...
- Java Listener中Spring接口注入的使用
在项目中使用Spring通常使用他的依赖注入可以很好的处理,接口与实现类之间的耦合性,但是通常的应用场景中都是Service层和DAO层,或者web层的话, 也是与Strust2来整合,那么如何在Li ...
- JavaWeb Listener
1. 监听器概述 1.1. 什么是监听器 做过Swing或者AWT图像界面Java程序开发的话,应该对Listener与Event非常熟悉.Swing或者AWT中通过Listener与Event来处理 ...
- Android5.1图库Gallery2代码分析数据加载流程
图片数据加载流程. Gallery---->GalleryActivity------>AlbumSetPage------->AlbumPage--------->Photo ...
- 使用Mongodb+Shiro+SpringMVC实现动态权限分配
此次的文档只对Mongodb整合Shiro并且实现动态权限分配做整理,其它的内容以后会补上. 第一步.创建在web.xml中配置 Spring .Shiro shiroFilter 过滤器是用来将请求 ...
- Spring boot 实现高吞吐量异步处理(适用于高并发场景)
技术要点 org.springframework.web.context.request.async.DeferredResult<T> 示例如下: 1. 新建Maven项目 asy ...
- Spring boot 集成Kafka
搭建Kafka集群,参考: https://www.cnblogs.com/jonban/p/kafka.html 源码示例如下: 1.新建 Maven 项目 kafka 2.pom.xml < ...
随机推荐
- OPC 数据交互环境配置
本文源自ioufev先生的博客<OPC和DCOM配置>(https://www.cnblogs.com/ioufev/p/9365919.html)及「geekc」先生的<OPC工作 ...
- Go语言中defer语句使用小结
defer是Go语言中的延迟执行语句,用来添加函数结束时执行的代码,常用于释放某些已分配的资源.关闭数据库连接.断开socket连接.解锁一个加锁的资源.Go语言机制担保一定会执行defer语句中的代 ...
- ElasticSearch 获取es集群信息
参考博客:https://www.cnblogs.com/phpshen/p/8668833.html es集群信息有些版本下如果证书过期就会查不到,有些版本貌似不需要,提供一个据说不需要证书的版本的 ...
- js 获取xxxx-xx-xx时间格式
function getdate() { var now = new Date(), y = now.getFullYear(), m = now.getMonth() + 1, d = now.ge ...
- 用户ID与权限
目录 用户ID与权限 文件系统查看 权限ID概览 设置位 黏着位 UMASK chmod与chown 代码附录 chmod title: 用户ID与权限 date: 2019/11/25 21:20: ...
- 线段树维护动态连续子段HDU1540
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1540 #define IOS ios_base::sync_with_stdio(0); cin.tie( ...
- FastAdmin
FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架. 感觉挺好用的. 开发文档https://doc.fastadmin.net/docs/index.html 根据 ...
- 牛客 216 C 小K的疑惑
大意: 给定树, 求多少个三元组(i,j,k), 满足dis(i,j)=dis(j,k)=dis(k,i). 刚开始想复杂了, 暴力统计了所有的情况. #include <iostream> ...
- List和Dictionary互转
// 声明Dictionary并初始化 Dictionary<string, string> dic = new Dictionary<string, string>() { ...
- R-corrplot相关性绘图,只有你想不到的
初步接触数据集,探索性分析后,经常需要做一个相关分析,得到各变量间的相关系数以及显著性水平. 本文介绍一下R-corrplot包进行相关可视化展示. 一 数据准备 载入所需的R包,利用公共数据集mtc ...