您的位置:首页 > 移动开发

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

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.部署,显示效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息