loggedusercounter.java

来自「尚学堂科技JAVA系列教程之JAVA网上商城项目讲解的源代码」· Java 代码 · 共 35 行

JAVA
35
字号
package com.bjsxt.shopping.util.listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class LoggedUserCounter implements HttpSessionListener, HttpSessionAttributeListener {
	
	private static int count = 0;
	
	public void sessionCreated(HttpSessionEvent event) {
System.out.println("------------session created!");
	}

	public void sessionDestroyed(HttpSessionEvent event) {
	}

	public void attributeAdded(HttpSessionBindingEvent event) {
		if(event.getName().equals("admin") && ((String)event.getValue()).equals("admin")) {
			count ++;
			System.out.println("a user logged in ! now total :" + count);
		}
	}

	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?