JaveWeb统计在线人数

8 篇文章 0 订阅

实现功能:

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监听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);
		//获取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.部署,显示效果




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值