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 < ...
随机推荐
- 基于c开发的全命令行音频播放器
cmus是一个内置了音频播放器的强大的音乐文件管理器.用它的基于ncurses的命令行界面,你可以浏览你的音乐库,并从播放列表或队列中播放音乐,这一切都是在命令行下. Linux上安装cmus 首先, ...
- windows7-maven配置
1.确认jdk安装 2.下载 3.解压缩 4.配置环境变量 (1)计算机属性--高级系统配置--高级--环境变量---系统变量--新建 (2)添加环境变量 MAVEN_HOME PATH中添加到mav ...
- 【ARM-LInux开发】如何运行wayland
Running Wayland 原文:https://jan.newmarch.name/Wayland/RunningWayland/ skip table of contents Show tab ...
- 【VS开发】CString 转为 char *方法大全
[VS开发]CString 转为 char *方法大全 标签(空格分隔): [VS开发] 方法1: CString strTemp; char szTemp[128]; strTemp = _T(&q ...
- python一个源文件调用另一个源文件的函数
使用软件:pychram 这个是使用了Dight.py的mai()函数,也已经成功运行,但是为什么pychram在下面划红色的波浪线呐.
- 一个基于Unix套接字的注册登录系统
2016/5/5 今天,我参考<Unix网络编程-卷1>第5章的TCP回射客户/服务器程序写了一个简单的注册登录系统,其功能如下:(1)注册.客户端向服务器发送个人信息请求注册,服务器查询 ...
- poj2318(叉积判断点在直线左右+二分)
题目链接:https://vjudge.net/problem/POJ-2318 题意:有n条线将矩形分成n+1块,m个点落在矩形内,求每一块点的个数. 思路: 最近开始肝计算几何,之前的几何题基本处 ...
- jsp页面报错,各种错误码意思
基本原则: 2xx = Success(成功) 3xx = Redirect(重定向) 4xx = User error(客户端错误) 5xx = Server error(服务器端错误) 状态码 ( ...
- IDEA的第一个java程序
import java.util.Scanner;public class 阶乘{ public static void main(String[] args) { int sum=1,i; Scan ...
- javascript字符串机油
1.创建字符串和数组的方法 1.1创建字符串的方法 a.直接数量:var str=“: b.字符串对象创建:新字符串(“): 1.2创建阵列的方法 a.var.arr=要素…. b.var arr=n ...