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

📄 usersessionlistener.java

📁 struts+spring+hibernate自创框架
💻 JAVA
字号:
package com.pegasus.framework.acl.pojo;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.*;

public class UserSessionListener implements HttpSessionListener {
    private org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(UserSessionListener.class);
    private static HashSet sessionList = new HashSet();

    /**
     * @param event .
     */
    public void sessionCreated(HttpSessionEvent event) {
        logger.debug("sessionCreated");
        sessionList.add(event.getSession());
    }

    /**
     * @param event .
     */
    public void sessionDestroyed(HttpSessionEvent event) {
        logger.debug("sessionDestroyed");
        sessionList.remove(event.getSession());
    }

    /**
     * @return .
     */
    public static int getActiveSessionCount() {
        return sessionList.size();
    }

    /**
     * 得到在线用户总数
     *
     * @return .
     */
    public static synchronized int getOnlineUserCount() {
        int count = 0;

        for (Iterator it = sessionList.iterator(); it.hasNext();) {
            HttpSession session = (HttpSession) it.next();
            if (session == null) continue;
            UserSession userSession = (UserSession) session.getAttribute(UserSession.USERSESSION_KEY);
            if (userSession != null) {
                if (null != userSession.getLoginDate())
                    count++;
            }
        }

        return count;
    }

    /**
     * @return .
     * @throws Exception .
     */
    public static synchronized List getOnlineUserList() throws Exception {
        List list = new ArrayList();
        for (Iterator it = sessionList.iterator(); it.hasNext();) {
            HttpSession session = (HttpSession) it.next();
            if (session == null) continue;
            UserSession userSession = (UserSession) session.getAttribute(UserSession.USERSESSION_KEY);
            if (userSession != null) {
                if (null != userSession.getLoginDate() && null != userSession.getUserID()) {
                    SessionInfo sessionInfo = new SessionInfo();
                    sessionInfo.setActName(userSession.getLastActName());
                    sessionInfo.setCorpName(userSession.getCorp_name());
                    sessionInfo.setDeptName(userSession.getDept_name());
                    sessionInfo.setLastVisitDate(userSession.getLastVisitDate());
                    sessionInfo.setLoginDate(userSession.getLoginDate());
                    sessionInfo.setSessionid(session.getId());
                    sessionInfo.setUserName(userSession.getUserName());
                    sessionInfo.setLoginName(userSession.getLoginname());
                    sessionInfo.setUserIP(userSession.getUser_ip());
                    list.add(sessionInfo);
                }
            }
        }
        Collections.sort(list);
        return list;
    }


}

⌨️ 快捷键说明

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