聊天室显示在线人数和已上线人数

一开始我是以为这两个很类似

是用同一种方法就能实现的

但是,实际上在线人数可以用session实现,而已上线人数应该用servletcontext实现.

也就是一个监听servletcontext,一个监听session

而且为了记录数据要另写一个类来记录(counter)

public class ContextListener implements ServletContextListener {
    public static final String NAME = "name";
    public static final String Talks = "talks";
    //static final 静态最终啥的玩意 其实就是放在application里的数据
    //Talks是服务器内部调用的 ContextListener.Talks
    //talks是web前端调用的 applicationScope.talks
    private StringBuilder talks = new StringBuilder();
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        sce.getServletContext().removeAttribute(NAME);
        sce.getServletContext().removeAttribute(Talks);
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        Counter c = new Counter();
        sce.getServletContext().setAttribute(NAME, c);
//在服务器创建的时候,建立了一个counter sce.getServletContext().setAttribute(Talks, talks); } }
public class SessionListener implements HttpSessionListener{ public static final String NAME = "name"; private int count = 0; @Override public void sessionCreated(HttpSessionEvent hse) { // TODO Auto-generated method stub HttpSession httpSession = hse.getSession(); ServletContext sc = httpSession.getServletContext(); Counter c = (Counter) sc.getAttribute(ContextListener.NAME); String name = "游客" + count; while (!c.addNowNames(name)){
//加入成功则在counter里改变数据
//加入不成功,就改变count知道加入成功 count
++; name = "游客" + count; } httpSession.setAttribute(NAME,name); StringBuilder sb = (StringBuilder)sc .getAttribute(ContextListener.Talks); sb.append(String.format("%s加入了!%n",name)); } @Override public void sessionDestroyed(HttpSessionEvent hse) { // TODO Auto-generated method stub HttpSession httpSession = hse.getSession(); ServletContext sc = httpSession.getServletContext(); Counter c = (Counter) sc.getAttribute(ContextListener.NAME); String name = httpSession.getAttribute(NAME).toString(); c.removeNowNames(name); StringBuilder chatInfo = (StringBuilder) sc .getAttribute(ContextListener.Talks); chatInfo.append(String.format("%s离开了!%n", name)); } }
Serializable序列化来排序
public class Counter implements Serializable {

    private static final long serialVersionUID = 45354343L;
    private int allNames = 0;
    private Set<String> nowNames = new LinkedHashSet<String>();
    public Counter() {
    }

    public boolean addNowNames(String name) {
        boolean r = nowNames.add(name);
        //如果原来有这个名字 就已上线人数增加
        if (r) {
            allNames++;
        }
        return r;
    }
    public boolean renameNowNames(String oldName, String newName) {
        if (nowNames.contains(oldName)) {
            nowNames.remove(oldName);
        } else {
            return false;
        }
        if (nowNames.add(newName)) {
            return true;
        } else {
            nowNames.add(oldName);
            return false;
        }
    }

    public void removeNowNames(String name) {
        nowNames.remove(name);
    }
//必须要有getxxxxxx的方法才能在jsp里被提取到
    public int getNowNamesSize() {
//这个就是在线人数
return nowNames.size(); } public int getAllNames() { return allNames; } public Set<String> getNowNames() { return Collections.unmodifiableSet(nowNames); } }
    <table align="center">
            <tr>
                <td colspan="2">
                <table align="center" cellspacing="10">
            <tr>
                <td>历史访问人数:${applicationScope.name.allNames }</td>
                <td>在线人数:${applicationScope.name.nowNamesSize }
                 <select>
                    <c:forEach items="${applicationScope.name.nowNames}" var="line">
                    <!-- line是nowNames的其中一个值 -->
                        <option>${line}</option>
                    </c:forEach>
                </select>
                </td>
            <td>欢迎:${sessionScope.name}</td>
        </tr>
        <tr>
            <td colspan="3"><textarea rows="20" cols="80">${applicationScope.talks}</textarea>
            </td>
        </tr>
    </table>
原文地址:https://www.cnblogs.com/ydymz/p/6379535.html