[09] 监听器 Listener
1、事件
1.1 事件的概念

1.2 事件类型
- ServletContextEvent
- 该类表示上下文事件,当应用上下文对象发生改变,如创建或销毁上下文对象时,将触发上下文事件
- ServletContextAttributeEvent
- 该类表示上下文属性事件,当应用上下文的属性发生变化,如增加、删除、覆盖上下文中的属性时,将触发该事件
- ServletRequestEvent
- 该类表示请求事件,当请求对象发生改变,如创建或销毁请求对象时,触发请求事件
- ServletRequestAttributeEvent
- 该类表示请求属性事件,当请求中的属性发生改变,如增加、删除、覆盖请求中的属性时,触发请求属性事件
- HttpSessionEvent
- 该类表示会话事件,当会话对象发生改变,如创建或销毁会话对象,活化或钝化会话对象时,将触发会话事件
- HttpSessionBindingEvent
- 该类表示会话绑定事件,当会话中的属性发生变化时,如增加、删除、覆盖会话中的属性时,将触发会话绑定事件
2、监听器
- ServletContextListener
- 上下文监听器,监听ServletContextEvent事件
- ServletContextAttributeListener
- 上下文属性监听器,用来监听ServletContextAttribute事件
- ServletRequestListener
- 请求监听器,监听ServletRequestEvent事件
- ServletRequestAttributeListener
- 请求属性监听器,用来监听ServletRequestAttributeEvent事件
- HttpSessionListener
- 会话监听器,监听HttpSessionEvent事件
- HttpSessionActivationListener
- 会话活化监听器,监听HttpSessionEvent事件
- HttpSessionAttributeListener
- 会话属性监听器,监听HttpSessionAttributeEvent事件
- HttpSessionBindingListener
- 会话绑定监听器,监听HttpSessionAttributeEvent事件
3、事件和监听的关系
4、示例:用监听器修改登录计数器
- 当容器关闭时,把当前数值保存到文本文件中
- 当容器启动时,从文本文件中读取数值。
4.1 写一个监听器
public class VisitCountsListener implements ServletContextListener {
/**
* 容器启动时
* @param sce
*/
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
String path = context.getRealPath("/WEB-INF/temp/visit.txt");
File file = new File(path);
int count = 0;
try {
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(path));
count = Integer.valueOf(reader.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
context.setAttribute("count", count);
}
/**
* 容器关闭时
* @param sce
*/
public void contextDestroyed(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
String path = context.getRealPath("/WEB-INF/temp/visit.txt");
File file = new File(path);
try {
if (file.exists()) {
file.delete();
}
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(context.getAttribute("count").toString());
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class VisitCountsListener implements ServletContextListener {
/**
* 容器启动时
* @param sce
*/
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
String path = context.getRealPath("/WEB-INF/temp/visit.txt");
File file = new File(path);
int count = 0;
try {
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(path));
count = Integer.valueOf(reader.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
context.setAttribute("count", count);
}
/**
* 容器关闭时
* @param sce
*/
public void contextDestroyed(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
String path = context.getRealPath("/WEB-INF/temp/visit.txt");
File file = new File(path);
try {
if (file.exists()) {
file.delete();
}
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(context.getAttribute("count").toString());
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.2 在web.xml中配置监听器
<listener>
<listener-class>com.zker.VisitCountsListener</listener-class>
</listener>
<listener>
<listener-class>com.zker.VisitCountsListener</listener-class>
</listener>
[09] 监听器 Listener的更多相关文章
- javaweb学习总结(四十七)——监听器(Listener)在开发中的应用
监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...
- javaWeb学习总结(11)- 监听器(Listener)在开发中的应用
监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...
- javaWeb学习总结(11)- 监听器(Listener)学习
一.监听器介绍 1.1.监听器的概念 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其 实就是一个实现特定接口的普 ...
- java之Servlet监听器Listener
常用应用场景:单点登录.统计在线人数 一.简介 (一)概述 1.Listener 用于监听 java web程序中的事件,例如创建.修改.删除Session.request.context等,并触发响 ...
- 过滤器Filter与监听器Listener
过滤器Filter 过滤器也是一种servlet 它也可以对用户的请求进行处理 , 但是他所做的处理,只是一些轻量级的处理.Fileter就好像jsp页面与servlet之间的一道关卡,如果这个 ...
- JAVA监听器Listener
JAVA监听器Listener 一. 简介 监听器用于对web中内置对象的状态或者属性变化进行监听并做出相应响应的一种Servlet;在内置对象的生命周期中,产生.销毁等状态发生变化时,监听器就会进行 ...
- 过滤器(Filter)、拦截器(Interceptor)、监听器(Listener)
一.Filter 过滤器 1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servle ...
- JavaWeb学习 (二十七)————监听器(Listener)在开发中的应用
监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...
- Servlet 监听器Listener详解
转自:http://blog.csdn.net/u012228718/article/details/41730799 一.简介 (一)概述 1.Listener 用于监听 Javaweb程序中的事件 ...
随机推荐
- Fiddler设置代理(PC和Android)
Fiddler设置 打开Fiddler,进入Tools--->Fiddler Options,勾选以下信息: OK,就配置完成了. PC端设置: 以火狐为例,打开选项--->高级 ...
- JSP 页面传值方法总结(转)
原文地址:http://www.cnblogs.com/java-class/p/6358964.html 阅读目录 1. URL 链接后追加参数 2. Form 3. 设置 Cookie 4. 设置 ...
- js中的访问器属性中的getter和setter函数实现数据双向绑定
嗯,之前在读js红宝书的时候,在对象那一章有介绍属性类型.第一种数据类型指的是数据属性,第二种是访问器属性.在初识vue的时候,其双向数据绑定也是基于访问器属性中的getter和setter函数原理来 ...
- 【渗透笔记】利用逻辑漏洞批量拿GOV EDU
前言: 这个Oday是以前就有的,不过都没有人出过详细的使用教程,昨天帮群里某学院拿了他们的学校之后突然想起来这个Oday,而且实用性还很强,所以我就想分享到这里来了 关键字:inurl:sitese ...
- jquery表单验证源码
/**数据验证完整性**/$.fn.Validform = function () { var Validatemsg = ""; var Validateflag = ...
- SSH:dataSource配置问题
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- c++-STL:删除子串
void deletesub(string &str,const string &sub,int n) { int m,flag=0,num=0; //num是子串出现的次数 whil ...
- 报表 jasper + ireport5.6
下载 iReport-5.6.0,jdk7,以及众多lib , 这里我提供下资源(我的百度云) 安装好iReport-5.6.0和jdk7, 在安装目录的\etc\ireport.conf,修改其中 ...
- POI处理Excel中的日期数据类型
在POI处理Excel中的日期类型的单元格时,如果仅仅是判断它是否为日期类型的话,最终会以NUMERIC类型来处理. 正确的处理方法是先判断单元格 的类型是否则NUMERIC类型, 然后再判断单元格是 ...
- oracle字段由中文前缀加数字,数字自动增长的实现
table中有一个字段,id,它是由Yunsha_000001的规则组成的. 每当插入一条数据的时候,自动生成的id是自动增加的,如何实现数字部分的自动增长? select 'Yunsha_'||l ...