首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >聊天室显示在线人数和已上线人数

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

作者头像
ydymz
发布2018-09-10 14:55:43
发布2018-09-10 14:55:43
1.3K00
代码可运行
举报
文章被收录于专栏:lgp20151222lgp20151222
运行总次数:0
代码可运行

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

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

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

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

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

代码语言:javascript
代码运行次数:0
运行
复制
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));
    }

}
代码语言:javascript
代码运行次数:0
运行
复制
用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);
    }
}
代码语言:javascript
代码运行次数:0
运行
复制
    <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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-02-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档