JaveWeb统计在线人数
来源:互联网 发布:macbook办公软件 编辑:程序博客网 时间:2024/05/16 19:11
实现功能:
1.当用户没有登录的时候,自动给他分配name=游客***。
2.当用户登录后,自动把name替换成用户名(username)
3.当点击注销时,把用户名(username)替换成name=游客***
具体实现:
1.用application监听来实现统计在线用户
2.用session监听来实现分配name=游客**和用户注销
现有t_user表有id,username,password字段
1.写一个application监听MyServletContextListener
public class MyServletContextListener implements ServletContextListener {public void contextDestroyed(ServletContextEvent event) {}public void contextInitialized(ServletContextEvent event) {//创建onlinePersonList<String> onlinePerson =new ArrayList<String>();//获取applicationServletContext application = event.getServletContext();//保存onlinePersonapplication.setAttribute("onlinePerson",onlinePerson );}}
2.写一个session监听MySession
public class MySessionListener implements HttpSessionListener {public void sessionCreated(HttpSessionEvent event) {HttpSession session = event.getSession();//随机给浏览器创建一个用户名String onlineName = "游客"+new SimpleDateFormat("HHmmss").format(new Date());session.setAttribute("onlineName", onlineName);//获取applicationServletContext application = event.getSession().getServletContext();List<String> onlinePerson = (List<String>) application.getAttribute("onlinePerson");//把onlineName添加到onlinePerson中去onlinePerson.add(onlineName);//添加到application中去application.setAttribute("onlinePerson", onlinePerson);}public void sessionDestroyed(HttpSessionEvent event) {//获取sessionHttpSession session = event.getSession();//获取随机分配给浏览器的nameString onlineName = (String) session.getAttribute("onlineName");//获取application并得到onlinePersonList<String> onlinePerson = (List<String>) session.getServletContext().getAttribute("onlinePerson");//索引int index=-1;//迭代出onlinePerson,判断onlineName是否一样for(Iterator<String> i = onlinePerson.iterator();i.hasNext();){index++;//判断name名是否一样if(i.next().equals(onlineName)){break;}}//把username移除掉onlinePerson.remove(index);}}3.在登录servlet(login.do)中
User user = UserServiceImpl.getInstance().login(username, password);request.getSession().setAttribute("user", user);HttpSession session = request.getSession();String onlineName = (String) session.getAttribute("onlineName");List<String> onlinePerson = (List<String>) request.getSession().getServletContext().getAttribute("onlinePerson");int index=-1;//迭代出onlinePerson,判断onlineName是否一样for(Iterator<String> i = onlinePerson.iterator();i.hasNext();){index++;if(i.next().equals(onlineName)){break; } }//替换出usernameonlinePerson.set(index, username);request.getSession().getServletContext().setAttribute("onlinePerson", onlinePerson);4.在注销servlet(logout.do)中
HttpSession session = request.getSession();//调用destroy方法session.invalidate();//重定向到首页response.sendRedirect("./index.jsp");
5.在index.jsp中登录超链接到login.do,注销超链接到logout.do
<% //获取onlinePerson
List<String> onlinePerson = (List<String>)application.getAttribute("onlinePerson"); %> 在线人数:<%=onlinePerson.size() %> <%
//迭代出onlinePerson,并输出 for(Iterator<String> i = onlinePerson.iterator();i.hasNext();){ %> <%=i.next() %> <% }%>
6.部署,显示效果
- JaveWeb统计在线人数
- 在线人数统计
- HttpSessionListener统计在线人数
- 在线人数统计
- 如何统计在线人数
- HttpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- HttpSessionListener统计在线人数
- 统计在线人数
- HttpSessionListener统计在线人数
- 在线人数统计方法
- 统计在线人数
- HttpSessionListener统计在线人数
- jsp在线人数统计
- 统计在线人数Web
- httpSessionListener统计在线人数
- 在线统计人数
- 在线人数统计所在位置
- Android Fragments 详细使用
- php复习
- 解决vi/vim中粘贴会在行首多很多缩进和空格的问题
- linux下报错bash: service: command not found
- ERP项目中的后期运维研究
- JaveWeb统计在线人数
- JAVA静态代理为什么用聚合用接口
- VMware下安装的Mac OS X如何修改显示分辨率
- 浅谈数据库索引
- first single char
- 重学C++ 友元函数
- Android NDK 编译过程中遇到错误 exception handling disabled, use
- 互联网信息可信性现状
- mysql数据库索引优缺点及注意事项