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

网友投稿 227 2022-12-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)http://

*/

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"%>

<%session.setMaxInactiveInterval(3); %>

当前在线人数:${count}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Javaweb监听器实例之统计在线人数
下一篇:IDEA+JRebel实现全自动热部署的方法步骤
相关文章

 发表评论

暂时没有评论,来抢沙发吧~