【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>

最新文章

  1. LinuxThreads 和 NPTL
  2. linux -- nano
  3. php_html转译符号
  4. Java_Eclipse_Maven插件部署
  5. javascript通过时区获取时间
  6. jQuery自学笔记(一):初识jQuery
  7. 1.elk 入门示例
  8. 快速构建Windows 8风格应用14-ShareContract概述及原理
  9. 洛谷-均分纸牌-NOIP2002提高组复赛
  10. java中File类的常用所有方法及其应用
  11. Tomcat 连接池详解
  12. java.lang.IllegalArgumentException: No enum constant org.apache.ws.commons.schema.XmlSchemaForm.
  13. docker swarm 部署服务时,限制服务启动后所在的机器
  14. loadrunner飞机订票系统从登陆订票退票登出的脚本实现代码调试通过
  15. Excel VBA 连接各种数据库(一) VBA连接MySQL数据库
  16. python3爬虫抓取智联招聘职位信息代码
  17. 修改Linux用户配置之后先验证再退出
  18. Deep learning with Python 学习笔记(8)
  19. ccf-20171203 Crontab问题
  20. sklearn dataset 模块学习

热门文章

  1. 【php】php位运算及其高级应用
  2. os模块总结
  3. webpack入门(七) API in LOADERS
  4. Session&amp;&amp;cookie
  5. C# Winfrom 发送邮件验证码&amp;Timer控件
  6. django2+uwsgi+nginx上线部署到服务器Ubuntu16.04(最新最详细版)
  7. SNP在世界地图上的频率分布
  8. get请求中params参数的使用
  9. 新建体(1):新建type
  10. 一个关于finally和return的面试题