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

📄 usercounterlistener.java

📁 JiveJdon 3.0 is the application of Jdon Framework 1.4 final.
💻 JAVA
字号:
/*
 * Copyright 2003-2006 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.jdon.jivejdon.presentation.listener;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;


/**
 * UserCounterListener class used to count the current number
 * of active users for the applications.  Does this by counting
 * how many user objects are stuffed into the session.  It Also grabs
 * these users and exposes them in the servlet context.
 *
 * @web.listener
 */
public class UserCounterListener implements ServletContextListener,
                                            HttpSessionAttributeListener {
    public static final String COUNT_KEY = "userCounter";
    public static final String USERS_KEY = "userNames";
    private transient ServletContext servletContext;
    private Set sessionIds;

    public synchronized void contextInitialized(ServletContextEvent sce) {
        servletContext = sce.getServletContext();
        servletContext.setAttribute((COUNT_KEY), Integer.toString(0));
    }

    public synchronized void contextDestroyed(ServletContextEvent event) {
        servletContext = null;
        sessionIds = null;
    }

   

    synchronized void addSession(Object sessionId) {
        sessionIds = (Set) servletContext.getAttribute(USERS_KEY);

        if (sessionIds == null) {
            sessionIds = new HashSet();
        }
        sessionIds.add(sessionId);
        servletContext.setAttribute(USERS_KEY, sessionIds);
        servletContext.setAttribute(COUNT_KEY, Integer.toString(sessionIds.size()));
    }

    synchronized void removeSession(Object sessionId) {
        sessionIds = (Set) servletContext.getAttribute(USERS_KEY);

        if (sessionIds != null) {
            sessionIds.remove(sessionId);
        }

        servletContext.setAttribute(USERS_KEY, sessionIds);
        servletContext.setAttribute(COUNT_KEY, Integer.toString(sessionIds.size()));
    }


    /**
    * This method is designed to catch when user's login and record their name
     * @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent event) {
        addSession(event.getSession().getId());
    }

    /**
    * When user's logout, remove their name from the hashMap
     * @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent event) {
        removeSession(event.getSession().getId());
    }

    /**
     * @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent event) {
        // I don't really care if the user changes their information
    }
}

⌨️ 快捷键说明

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