利用监听器实现网站在线人数统计
来源:互联网 发布:软件提速精灵 编辑:程序博客网 时间:2024/05/05 22:27
ServletContextListenerImpl.java代码
public class ServletContextListenerImpl implements ServletContextListener {
public void contextDestroyed(ServletContextEvent event) {
ServletContext application = event.getServletContext();
application.removeAttribute("onLineNum");
}
public void contextInitialized(ServletContextEvent event) {
//利用servletContextEvent对象创建servletContext
int num = 0;
ServletContext application = event.getServletContext();
application.setAttribute("onLineNum", num);//初始默认在线人数为0
}
}
HttpSessionListnerImpl.java代码
public class HttpSessionListnerImpl implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
//通过event获得servletContext对象
ServletContext application = event.getSession().getServletContext();
//拿到servletContext空间中储存的onLineNum
Integer num = (Integer)application.getAttribute("onLineNum");
if(num != null){
//如果存在,则将拿到的数量加一并到重新放入到servletContext空间中
num = num + 1;
application.setAttribute("onLineNum", num);
} else {
//如果不存在,则设置onLineNum为1
application.setAttribute("onLineNum", 1);
}
}
public void sessionDestroyed(HttpSessionEvent event) {
System.out.println("一个session关闭了");
//通过event获得servletContext对象
ServletContext application = event.getSession().getServletContext();
//拿到servletContext空间中储存的onLineNum
Integer num = (Integer)application.getAttribute("onLineNum");
num = num - 1;
//将拿到的数量减一并到重新放入到servletContext空间中
application.setAttribute("onLineNum", num);
}
}
对应的在web.xml文件中进行监听器配置
<listener>
<listener-class>
com.hk.listener.ServletContextListenerImpl
</listener-class>
</listener>
<listener>
<listener-class>
com.hk.listener.HttpSessionListnerImpl
</listener-class>
</listener>
jsp代码
<body>
当前在线人数:${onLineNum }
</body>
测试结果
- 利用监听器实现网站在线人数统计
- Servlet监听器实现网站在线人数统计
- 使用监听器实现一个统计网站在线人数的示例
- 使用监听器实现一个统计网站在线人数的示例
- 如何使用Servlet监听器实现网站在线人数统计
- Servlet监听器,统计网站在线人数实例
- 监听器统计在线人数
- 使用java 监听器实现在线人数统计
- 监听器(实现在线统计人数,踢人)
- 监听器--ServletRequestListener接口实现统计在线人数
- jsp监听器实现在线人数统计
- 通过监听器实现在线人数统计
- 利用HttpSessionListener实现网站在线人数统计功能
- 利用HttpSessionListener实现网站在线人数统计功能
- 利用HttpSessionListener实现网站在线人数统计功能[摘]
- 利用HttpSessionListener实现网站在线人数统计功能
- 利用HttpSessionListener实现网站在线人数统计功能
- 利用HttpSessionListener实现网站在线人数统计功能
- gdb调试命令
- apache+JK+tomcat负载均衡配置(windows)
- 大争之世,不在竞争中崛起,就在竞争中被吃掉
- 《Linux》天影linux系列笔记一——linux常用命令详解:gunzip
- 关于make sure class name exists, is public, and has an empty constructor that is public解决办法
- 利用监听器实现网站在线人数统计
- 多业务线受挫 小米深陷滑铁卢泥潭
- Linux笔记--5 --- shell脚本
- android UI 之shape
- Matlab调用DLL
- 对‘pthread_create’未定义的引用
- wifi softap(hotspot) porting
- 精简 mysq
- 140_leetcode_Regular Expression Matching