java监听器实现在线人数统计

 更新时间:2019年11月20日 16:49:21   作者:夏木炎  
这篇文章主要为大家详细介绍了java监听器实现在线人数统计,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java在线人数统计的具体代码,供大家参考,具体内容如下

1. 项目结构

2. 代码

package com;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
 
/**
 * Application Lifecycle Listener implementation class MyContexxtLis
 *
 */
@WebListener
public class CountListen implements ServletContextListener {
 
  /**
   * Default constructor. 
   */
  public CountListen() {
    // TODO Auto-generated constructor stub
  }
 
 /**
   * @see ServletContextListener#contextInitialized(ServletContextEvent)
   */
  public void contextInitialized(ServletContextEvent arg0) { 
   arg0.getServletContext().setAttribute("count",100);
  }
 
 /**
   * @see ServletContextListener#contextDestroyed(ServletContextEvent)
   */
  public void contextDestroyed(ServletContextEvent arg0) { 
     // TODO Auto-generated method stub
  }
 
}
package com;
 
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
 
 
@WebListener
public class SessionListen implements HttpSessionListener, HttpSessionAttributeListener {
 
  public SessionListen() {
    // TODO Auto-generated constructor stub
  }
 
  public void attributeRemoved(HttpSessionBindingEvent arg0) { 
   System.out.println("remove"+"\t"+arg0.getName()+arg0.getValue());
  }
 
  public void attributeAdded(HttpSessionBindingEvent arg0) { 
   System.out.println("add"+"\t"+arg0.getName()+arg0.getValue());
  }
 
  public void attributeReplaced(HttpSessionBindingEvent arg0) { 
   System.out.println("replace"+"\t"+arg0.getName()+arg0.getValue());
  }
 
  public void sessionCreated(HttpSessionEvent arg0) { 
   System.out.println("session create");
 Integer i=(Integer)arg0.getSession().getServletContext().getAttribute("count");
 i++;
 arg0.getSession().getServletContext().setAttribute("count", i);
 
  }
 
  public void sessionDestroyed(HttpSessionEvent arg0) { 
 Integer i=(Integer)arg0.getSession().getServletContext().getAttribute("count");
 i--;
 arg0.getSession().getServletContext().setAttribute("count", i);
 System.out.println("session destroy"+i);
 
  }
 
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%session.setMaxInactiveInterval(3); %>
当前在线人数:${count}
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java Condition条件变量提高线程通信效率

    Java Condition条件变量提高线程通信效率

    这篇文章主要介绍了Java Condition条件变量提高线程通信效率,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • SpringBoot整合Redis将对象写入redis的实现

    SpringBoot整合Redis将对象写入redis的实现

    本文主要介绍了SpringBoot整合Redis将对象写入redis的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 解决spring懒加载以及@PostConstruct结合的坑

    解决spring懒加载以及@PostConstruct结合的坑

    这篇文章主要介绍了解决spring懒加载以及@PostConstruct结合的坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • springboot下实现RedisTemplate List 清空

    springboot下实现RedisTemplate List 清空

    我们经常会使用Redis的List数据结构来存储一系列的元素,当我们需要清空一个List时,可以使用RedisTemplate来实现,本文就来详细的介绍一下如何实现,感兴趣的可以了解一下
    2024-01-01
  • 记录一个使用Spring Data JPA设置默认值的问题

    记录一个使用Spring Data JPA设置默认值的问题

    这篇文章主要介绍了使用Spring Data JPA设置默认值的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 关于Java8新特性Optional类的详细解读

    关于Java8新特性Optional类的详细解读

    Optional类是一个容器类,它可以保存类型T的值,代表这个值存在。或者仅仅保存null,表示这个值不存在,原来用 null 表示一个值不存在,现在Optional 可以更好的表达这个概念。并且可以避免空指针异常,需要的朋友可以参考下
    2023-05-05
  • springboot集成开发实现商场秒杀功能

    springboot集成开发实现商场秒杀功能

    这篇文章主要介绍了springboot集成实现商品秒杀功能,秒杀系统业务流程,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • Java超详细讲解WebMvcConfigurer拦截器

    Java超详细讲解WebMvcConfigurer拦截器

    这篇文章将用实例来和大家介绍一下WebMvcConfigurer拦截器。文中的示例代码讲解详细,对我们学习Java有一定的帮助,需要的可以参考一下
    2022-06-06
  • Java读写文件创建文件夹多种方法示例详解

    Java读写文件创建文件夹多种方法示例详解

    这篇文章主要介绍了Java读写文件创建文件夹等多种操作的方法,大家参考使用吧
    2013-11-11
  • SpringBoot配置actuator的代码

    SpringBoot配置actuator的代码

    这篇文章主要介绍了SpringBoot配置actuator,这里大家需要注意可以监控SpringBoot 中的 Tomcat 性能数据, 以日志形式定期输出监控数据, 只需要配置一个Bean,需要的朋友可以参考下
    2022-03-03

最新评论