Listener 快速开始
2024-10-18 04:39:43
【SessionListener】
@WebListener
public class SessionListener implements HttpSessionListener,HttpSessionIdListener{
@Override
public void sessionIdChanged(HttpSessionEvent e, String oldSessionId) {
SessionRegistry.updateSessionId(e.getSession(),oldSessionId);
System.out.println(this.date()+":Session ID:"+oldSessionId+" changed to "+e.getSession().getId());
} private String date() {
SimpleDateFormat f = new SimpleDateFormat("EEE dd,mm,yyyy HH:mm:ss");
return f.format(System.currentTimeMillis());
} @Override
public void sessionCreated(HttpSessionEvent e) {
SessionRegistry.addSession(e.getSession());
System.out.println(this.date() + ":Session "+e.getSession().getId()+"created.");
} @Override
public void sessionDestroyed(HttpSessionEvent e) {
SessionRegistry.removeSession(e.getSession());
System.out.println(this.date() + ":Session "+e.getSession().getId()+"destroyed.");
}
}
【SessionRegistry】比较轻量,私有构造,一些静态方法
public final class SessionRegistry {
private static final Map<String,HttpSession> SESSIONS = new Hashtable<>();
public static void addSession(HttpSession session){
SESSIONS.put(session.getId(),session);
}
public static void updateSessionId(HttpSession session,String oldSessionId){
synchronized (SESSIONS){
SESSIONS.remove(oldSessionId);
addSession(session);
}
} public static void removeSession(HttpSession session){
SESSIONS.remove(session.getId());
} public static List<HttpSession> getAllSessions(){
return new ArrayList<>(SESSIONS.values());
} public static int getNumberOfSessions(){
return SESSIONS.size();
}
private SessionRegistry(){}
} 【web.xml】
<listener>
<listener-class>net.mypla.SessionListener</listener-class>
</listener>
最新文章
- LinuxThreads 和 NPTL
- linux -- nano
- php_html转译符号
- Java_Eclipse_Maven插件部署
- javascript通过时区获取时间
- jQuery自学笔记(一):初识jQuery
- 1.elk 入门示例
- 快速构建Windows 8风格应用14-ShareContract概述及原理
- 洛谷-均分纸牌-NOIP2002提高组复赛
- java中File类的常用所有方法及其应用
- Tomcat 连接池详解
- java.lang.IllegalArgumentException: No enum constant org.apache.ws.commons.schema.XmlSchemaForm.
- docker swarm 部署服务时,限制服务启动后所在的机器
- loadrunner飞机订票系统从登陆订票退票登出的脚本实现代码调试通过
- Excel VBA 连接各种数据库(一) VBA连接MySQL数据库
- python3爬虫抓取智联招聘职位信息代码
- 修改Linux用户配置之后先验证再退出
- Deep learning with Python 学习笔记(8)
- ccf-20171203 Crontab问题
- sklearn dataset 模块学习