session绑定的事件监听器的应用(统计在线用户)

统计在线用户列表:

1,写一个单例类,增加一个类型为ArrayList的属性,用来存储所有在线的用户(也可以用数据库进行操作)

2,利用session绑定的事件监听器对正常退出系统的用户进行监听

3,考虑用户以浏览器的关闭按钮关闭或以Alt+F4进行关闭(这可以用javascript对用户的操作进行捕捉,网上这方面够多)

4,当非正常关闭,如电脑突然断电,用户直接关闭电脑等,这需要等到session失效后,用户列表中的用户才能清除

共用到了7个文件:


UserList.java

/**
 * 用于存储用户列表的容器
 * 这是一个单例类
 */
package com.session;

import java.util.ArrayList;
import java.util.List;

public class UserList
{
	private List list;

	private static UserList instance = null;

	// 以private的方式来声明构造方法,使得其他的类对象无法调用此类的构造函数
	private UserList()
	{
		list = new ArrayList();
	}

	public static synchronized UserList getInstance()
	{
		if (instance == null)
		{
			instance = new UserList();
		}
		return instance;
	}

	public void addUser(String userName)
	{
		if (userName != null)
		{
			list.add(userName);
		}
	}

	public void removeUser(String userName)
	{
		if (userName != null)
			list.remove(userName);
	}

	public List getList()
	{
		return list;
	}
}

UserBinding.java/**
 * 实现了HttpSessionBindingListener接口,
 * 以对用户监听,我们必须实现valueBound和
 * UnvalueBound方法
 */

package com.session;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class UserBinding implements HttpSessionBindingListener
{
	private String userName;

	private UserList userList = UserList.getInstance();

	public String getUserName()
	{
		return userName;
	}

	public void setUserName(String userName)
	{
		this.userName = userName;
	}

	public void valueBound(HttpSessionBindingEvent e)
	{
		System.out.println(userName);
		userList.addUser(userName);
		System.out.println(userList.getList());
	}

	public void valueUnbound(HttpSessionBindingEvent e)
	{
		userList.removeUser(userName);

	}

}
 
 
UserListServlet.java
/**
 * 对登录用户进行逻辑处理的Servlet程序
 */

package com.session;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserListServlet extends HttpServlet
{

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session=request.getSession();
		
		System.out.println(session.getId());
		UserBinding user=new UserBinding();
		
		String userName=request.getParameter("userName");
		
		if(null==userName||"".equals(userName))
		{
			return;
		}
		
		//用户名不为空
		user.setUserName(userName);
		session.setAttribute("user", user);
		
//		UserList userList=UserList.getInstance();
//		//System.out.println(userList.getList());
//		request.setAttribute("userList",userList);
//		
//		RequestDispatcher rd=request.getRequestDispatcher("../userlist.jsp");
//		rd.forward(request,response);
		response.sendRedirect("../userlist.jsp");
		
	}
}
 
 
LogoutServlet.java
/**
 * 用于注销
 */
package com.session;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LogoutServlet extends HttpServlet
{

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		if (session != null)
		{
			session.invalidate();
		}
		response.sendRedirect("../userlist.jsp");
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		doGet(request, response);
	}
}
 
简单的登录界面:
login.jsp
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
 <link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="servlet/UserListServlet" method="POST">
 <input type="text" name="userName" >
 <input type="submit" name="login" value=" 登 录 ">
</form>
</body>
</html>
显示用户列表:
userlist.jsp
<%@ page language="java" import="java.util.*"
 contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.session.UserList"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
 <link rel="stylesheet" type="text/css" href="styles.css">
-->

<script type="text/javascript" src="refresh.js">
</script>

</head>
<body>
<h2>在线用户列表</h2>
<div id="userList">
<%
 UserList userList=UserList.getInstance();
 
 List<String> list = userList.getList();
 for (int i = 0; i < list.size(); i++)
 {
  out.println(list.get(i) + "<br>");
 }
 
%>
 
</div>
<form action="servlet/LogoutServlet" method="GET">
<input type="submit" name="logout" value=" 注 销 ">
</form>
</body>
</html>

最后是实现页面无刷新的javascript代码
refresh.js
/*
页面无刷新的javascript代码
*/
window.οnlοad=initAll;

var httpReq=false;
var url="userlist.jsp";

function initAll()
{
	makeRequest(url);
}

function makeRequest(url)
{
	if(window.XMLHttpRequest)// Mozilla, Safari,... 
	{
		httpReq=new XMLHttpRequest();
	}
	else
	{
		if(window.ActiveXObject)//IE
		{
			try
			{
				httpReq=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){}
		}
	}
	
	if(httpReq)
	{
		httpReq.onreadystatechange=tillRefresh;
		httpReq.open("GET",url,true);//异步
		httpReq.send(null);
	}
	else
	{
		alert("不能创建一个XMLHTTPRequest实例");
	}
	
}
function tillRefresh()
{
	var tempDiv=document.createElement("div");
	var userList=document.getElementById("userList");
	
	if(httpReq.readyState==4)
	{
		if(httpReq.status==200)
		{
			tempDiv.innerHTML=httpReq.responseText;
		
			var required=tempDiv.getElementsByTagName("div");
			userList.innerHTML=required[0].innerHTML;
			
			setTimeout("makeRequest(url)",5000);
		}
		else
		{
			alert("对不起,出现了一些问题 :"+httpReq.status);
		}
	}
}

对用户以浏览器的关闭按钮关闭或以Alt+F4进行关闭的情况没有写出,因为对javascript不太懂,COME ON!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值