Java Web开发中的监听器(listener)就是application、session、request三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件,如下所示:
①ServletContextListener:对Servlet上下文的创建和销毁进行监听。
②ServletContextAttributeListener:监听Servlet上下文属性的添加、删除和替换。
③HttpSessionListener:对Session的创建和销毁进行监听。
补充:session的销毁有两种情况:1). session超时(可以在web.xml中通过<session-config>/<session-timeout>标签配置超时时间);2). 通过调用session对象的invalidate()方法使session失效。
④HttpSessionAttributeListener:对Session对象中属性的添加、删除和替换进行监听。
⑤ServletRequestListener:对请求对象的初始化和销毁进行监听。
⑥ServletRequestAttributeListener:对请求对象属性的添加、删除和替换进行监听。
监听器常用的用途
它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量
通常使用Web监听器做以下的内容:
统计在线人数,利用HttpSessionLisener
加载初始化信息:利用ServletContextListener
统计网站访问量
实现访问监控
有时候后台需要统计最多在线人数以及当前在线人数,这里通过监听器实现这一功能。
图片仅供参考。。。
实例一
下面是一个统计网站最多在线人数监听器的例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
上下文监听器,在服务器启动时初始化onLineCount和maxOnLineCount两个变量
并将其置于服务器上下文(ServletContext)中,其初始值都是0
*/
@WebListener
public class InitListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent evt) {
}
@Override
public void contextInitialized(ServletContextEvent evt) {
evt.getServletContext().setAttribute( "onLineCount" , 0 );
evt.getServletContext().setAttribute( "maxOnLineCount" , 0 );
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
会话监听器,在用户会话创建和销毁的时候根据情况
修改onLineCount和maxOnLineCount的值
*/
@WebListener
public class MaxCountListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent event) {
ServletContext ctx = event.getSession().getServletContext();
int count = Integer.parseInt(ctx.getAttribute( "onLineCount" ).toString());
count++;
ctx.setAttribute( "onLineCount" , count);
int maxOnLineCount = Integer.parseInt(ctx.getAttribute( "maxOnLineCount" ).toString());
if (count > maxOnLineCount) {
ctx.setAttribute( "maxOnLineCount" , count);
DateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
ctx.setAttribute( "date" , df.format( new Date()));
}
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
ServletContext app = event.getSession().getServletContext();
int count = Integer.parseInt(app.getAttribute( "onLineCount" ).toString());
count--;
app.setAttribute( "onLineCount" , count);
}
}
|
说明:这里使用了Servlet 3规范中的@WebListener注解配置监听器,当然你可以在web.xml文件中用<listener>标签配置监听器。
实例二
在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听器技术来实现这个功能了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package me.gacl.web.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* @ClassName: OnLineCountListener
* @Description: 统计当前在线用户个数
* @author: 孤傲苍狼
* @date: 2014-9-10 下午10:01:26
*
*/
public class OnLineCountListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext context = se.getSession().getServletContext();
Integer onLineCount = (Integer) context.getAttribute( "onLineCount" );
if (onLineCount== null ){
context.setAttribute( "onLineCount" , 1 );
} else {
onLineCount++;
context.setAttribute( "onLineCount" , onLineCount);
}
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext context = se.getSession().getServletContext();
Integer onLineCount = (Integer) context.getAttribute( "onLineCount" );
if (onLineCount== null ){
context.setAttribute( "onLineCount" , 1 );
} else {
onLineCount--;
context.setAttribute( "onLineCount" , onLineCount);
}
}
}
|
总结
以上就是本文关于Java监听器的作用及用法代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们长久以来对本站的支持!
原文链接:http://blog.csdn.net/troubleshooter/article/details/78416857
相关文章
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-27 60
-
Mybatis分页插件PageHelper配置及使用方法详解
2025-05-29 58 -
2025-05-29 2
-
2025-05-25 81
-
2025-05-25 34