JaveWeb统计在线人数
2013-04-23 21:54
148 查看
实现功能:
1.当用户没有登录的时候,自动给他分配name=游客***。
2.当用户登录后,自动把name替换成用户名(username)
3.当点击注销时,把用户名(username)替换成name=游客***
具体实现:
1.用application监听来实现统计在线用户
2.用session监听来实现分配name=游客**和用户注销
现有t_user表有id,username,password字段
1.写一个application监听MyServletContextListener
2.写一个session监听MySessionpublic 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);
//获取application
ServletContext 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) {
//获取session
HttpSession session = event.getSession();
//获取随机分配给浏览器的name
String onlineName = (String) session.getAttribute("onlineName");
//获取application并得到onlinePerson
List<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;
}
}
//替换出username
onlinePerson.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
6.部署,显示效果
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) { //创建onlinePerson List<String> onlinePerson =new ArrayList<String>(); //获取application ServletContext application = event.getServletContext(); //保存onlinePerson application.setAttribute("onlinePerson",onlinePerson ); } }
2.写一个session监听MySessionpublic 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);
//获取application
ServletContext 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) {
//获取session
HttpSession session = event.getSession();
//获取随机分配给浏览器的name
String onlineName = (String) session.getAttribute("onlineName");
//获取application并得到onlinePerson
List<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;
}
}
//替换出username
onlinePerson.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.部署,显示效果
相关文章推荐
- php实现统计网站在线人数的方法
- spring-boot 统计实时在线人数
- asp论坛在线人数统计研究
- HttpSessionListener统计在线人数 [转]
- 如何统计在线人数
- 用Servlet监听器实现对在线人数的统计
- 统计网站在线人数
- javaWeb 在线人数统计
- java 页面在线访问人数统计和在线登录人数统计
- 被动式统计网站在线人数
- java 在线人数的统计
- 用java统计网站在线人数
- 有关在线统计人数
- 简单的java web 统计在线人数实现方法
- 统计用户在线登录人数
- 一个实用的FSO-实时统计在线人数
- Asp.net统计在线人数和总人数
- 统计在线人数是实时的吗?
- 得到准确的在线人数统计-减少搜索引擎的干扰 [摘]
- Servlet监听案例之统计在线人数