温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在java中使用监听器统计在线人数

发布时间:2021-04-17 16:42:32 来源:亿速云 阅读:165 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关怎么在java中使用监听器统计在线人数,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

1. 项目结构

怎么在java中使用监听器统计在线人数

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中使用监听器统计在线人数,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI