⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usersessionlistener.java

📁 java阿里巴巴代码
💻 JAVA
字号:
package com.saas.biz.commen;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
public class UserSessionListener implements HttpSessionBindingListener {
 private String userName;
 public UserSessionListener(String userName) {
  super();
  this.userName = userName;
 }
 
 private String getNow(){
  Date date = new Date();
  SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
  return df.format(date);  
 }
 @SuppressWarnings("unchecked")
 public void valueBound(HttpSessionBindingEvent arg0) {
  // TODO Auto-generated method stub
  arg0.getSession().setMaxInactiveInterval(-1);
  ServletContext application = arg0.getSession().getServletContext();
  synchronized (application) {
   LinkedList<String> userList = (LinkedList<String>) application
     .getAttribute("UserList");
   if (userList == null) {
    userList = new LinkedList<String>();
   }
   userList.add(userName);
   application.setAttribute("UserList", userList);
   LinkedList<String> messages = (LinkedList<String>) application
     .getAttribute("ChatMessage");
   if (messages == null) {
    messages = new LinkedList<String>();
   }
   //messages.add(getNow() + " " +  userName + " Login");
   application.setAttribute("ChatMessage", messages);
  }
 }
 @SuppressWarnings("unchecked")
 public void valueUnbound(HttpSessionBindingEvent arg0) {
  // TODO Auto-generated method stub
  ServletContext application = arg0.getSession().getServletContext();
  synchronized (application) {
   LinkedList<String> userList = (LinkedList<String>) application
     .getAttribute("UserList");
   userList.remove(userName);
   application.setAttribute("UserList", userList);
   LinkedList<String> messages = (LinkedList<String>) application
     .getAttribute("ChatMessage");
   if (messages == null) {
    messages = new LinkedList<String>();
   }
   //messages.add(getNow() + " " + userName + " Logout");
   application.setAttribute("ChatMessage", messages);
  }
 }
 /** *//**
  * @return the userName
  */
 public String getUserName() {
  return userName;
 }
 /** *//**
  * @param userName
  *            the userName to set
  */
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String toString() {
  return this.userName;
 }
}
    

⌨️ 快捷键说明

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