监听器小解
来源:互联网 发布:linux下关闭防火墙 编辑:程序博客网 时间:2024/09/21 06:36
转载:http://www.cnblogs.com/CBDoctor/p/4232859.html
Java 监听器
1: 监听器的定义
2:监听器的用途
3:实例
4:监听器分类
5:Servlet3.0下监听器的使用
6:实际项目常用的案例
1: 监听器的定义:
监听器实际上是一个类,这个类实现了特定的接口,然后将这个类在 web.xml 文件中进行描述,这样服务器在启动的时候就可以实例化这个类,启动监听器。当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。例如统计用户在线人数。
web监听器是Servlet规范中定义的一种特殊类,用于监听ServletContext,HttpSession,ServletRequest等域对象的创建、销毁、以及属性的变化等,可以在事件发生前、发生后进行一些处理。
2、监听器的用途
- 1、统计在线人数和在线用户
- 2、系统加载时进行信息的初始化工作
- 3、统计网站的访问量
- 4、跟Spring结合
3、实例
在web.xml文件中进行配置
listenr-class中是自己定义的Listener的类路径
public
class
MyRequestListener implements ServletContextListener{
public
void
contextInitialized(ServletContextEvent
event
){
System.
out
.println(
"启动监听器"
);
}
public
void
contextDestroy(ServletContextEvent
event
){
System.
out
.println(
"结束监听器"
);
}
}
编译器会为我们自动生成contextInitialized和contextDestroy两个函数
监听器>过滤器>serlvet,在 web.xml 文件中配置的时候注意顺序
4 监听器的分类
按监听的对象划分,可以分为监听
- ServletContext对象
- HttpSession对象
- ServletRequest对象
按监听的事件划分
- 域对象自身的创建和销毁
- 域对象中属性的创建和消除
- 绑定到session中的某个对象的状态
由于很多监听的方式一致,因此我们只考虑其中的HttpSession对象:
在web.xml中配置session超时
<session-config>
<session-timeout> 30 </session-timeout>
</session-config>
当超过30秒后,session会自动过期
public
class
MyRequestListener implements HttpSessionListener{
public
void
sessionCreate(HttpSessionEvent
event
){
System.
out
.println(
"event 创建:"
);
}
public
void
sessionDestroy(HttpSessionEvent
event
){
System.
out
.println(
"event 销毁:"
);
}
}
另外,我们可以实现HttpSessionAttributeListener来实现session对象属性的增加(added)、删除(removed)、替换(replaced)的功能
public
class
MyRequestListener implements HttpSessionAttributeListener{
public
void
attributeAdded(HttpSessionBindEvent
event
){
}
public
void
attributeRemoved(HttpSessionBindEvent
event
){
}
public
void
attributeReplaced(HttpSessionBindEvent
event
){
}
}
5、Servlte3.0下的使用
在Servlet3.0下通过注解@WebListener("")就可以实现了,不需要在web.xml中进行配置
6、实战
public
class
MyRequestListener implements HttpSessionListener{
private
int
counts = 0;
//用于统计在线人数
public
void
sessionCreate(HttpSessionEvent
event
){
counts++;
event
.getSession().getServletContext.setAttribute(
"name"
,counts);
//用户人数+1
}
public
void
sessionDestroy(HttpSessionEvent
event
){
counts--;
event
.getSession().getServletContext.setAttribute(
"name"
,counts);
//用户人数-1
}
}
@WebListener
public
class
MyRequestListener2 implements ServletRequestListener{
private
ArrayList<User>list;
//创建一个在线用户的列表
public
void
requestCreate(ServletRequestEvent
event
){
HttpServletRequest request = (HttpServletRequest) request.getServletContext();
String sessionId = request.getSession().getSessionId();
if
(sessionId ...){
....
list.add(...);
}
}
public
void
requestDestroy(ServletRequestEvent
event
){
}
}
public
class
User{
private
int
sessionId;
}
注意,以上代码只是大致的代码,并不全面和正确。
参考:java Web开发技术应用 - 监听器
- 监听器小解
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 监听器
- 剑指offer-面试10:二进制中1的个数(位运算)
- USACO2011Open Bronze 3lines 题解
- unity3D游戏开发中如何用UGUI制作背包
- Eclipse项目转AndroidStudio遇到问题记录
- maven中json-lib库无法引入
- 监听器小解
- 结构体字节对齐
- javaweb热部署详解
- iOS 静态库开发
- CPU个数和核数的判断依据
- 解决java compiler level does not match the version of the installed java project facet
- Redis介绍及常用命令
- SAP FI/CO报表查询
- Java开发中的‘暗坑’