下午比较闲(其实今天都很闲),想了一下在线人数统计方面的实现,上网找了下这方面的知识,最初我的想法是,管理session,如果session销毁了就减少,如果登陆用户了就新增一个,但是如果是用户非法退出,如:未注销,关闭浏览器等,这个用户的session是管理不到的,最后决定用HttpSessionListener接口或HttpSessionBindingListener接口来实现,通过监听session的新建和销毁来控制,详细如下。 先添加登陆的页面index.jsp 点击登陆后跳转的login.jsp(为了方便,用jsp做servlet,同学们用的时候记得改过来) 登陆成功跳转到显示页面result.jsp 点击注销页面logout.jsp页面 OK,登陆、查看、注销页面都有了,下面开始新建监听器 1、HttpSessionListener 添加类OnlineUserListener,继承HttpSessionListener,HttpSessionListener中有两个方法sessionCreated(HttpSessionEvent event)与sessionDestroyed(HttpSessionEvent event),前者是监听session的新建,后者是监听session的销毁。 ?OnlineUserListener代码如下: ? web.xml配置: 一旦监听器发现调用了sessionDestoryed方法就会把其用户从在线人数中delete,在下面两种情况下会发生sessionDestoryed事件 a.执行session.invalidate()方法时 logout.jsp中调用了 session.invalidate()方法 b.session会话超时 session的默认超时事件是30分钟,30分钟后自动销毁session ? 2、HttpSessionBindingListener HttpSessionBindingListener虽然叫做监听器,但使用方法与HttpSessionListener完全不同。我们实际看一下它是如何使用的。 新建类OnlineUserBindingListener,实现HttpSessionBindingListener接口,构造方法传入username参数,HttpSessionBindingListener内有两个方法valueBound(HttpSessionBindingEvent event)和valueUnbound(HttpSessionBindingEvent event),前者为数据绑定,后者为取消绑定 所谓对session进行数据绑定,就是调用session.setAttribute()把HttpSessionBindingListener保存进session中。 在login.jsp中做这一步: 这就是HttpSessionBindingListener和HttpSessionListener之间的最大区别:HttpSessionListener只需要设置到web.xml中就可以监听整个应用中的所有session。HttpSessionBindingListener必须实例化后放入某一个session中,才可以进行监听。 从监听范围上比较,HttpSessionListener设置一次就可以监听所有session,HttpSessionBindingListener通常都是一对一的。 正是这种区别成就了HttpSessionBindingListener的优势,我们可以让每个listener对应一个username,这样就不需要每次再去session中读取username,进一步可以将所有操作在线列表的代码都移入listener,更容易维护。 HttpSessionBindingListener代码如下: 这里可以直接使用listener的username操作在线列表,不必再去担心session中是否存在username。 valueUnbound的触发条件是以下三种情况: a.执行session.invalidate()时。 b.session超时,自动销毁时。 c.执行session.setAttribute("onlineUserListener", "其他对象");或session.removeAttribute("onlineUserListener");将listener从session中删除时。 因此,只要不将listener从session中删除,就可以监听到session的销毁。monospace !important; font-style: normal !important; font-size: 1em !important; display: block !important; margin: 0px !important;">01
<%@ page contentType="text/html;charset=utf-8"%>
02
<html>
03
<head>
04
<title>test</title>?????????????
05
</head>
06
<body>
07
<form?action="login.jsp"?method="post">
08
????用户名:<input?type="text"?name="username"?/>
09
????<br?/>
10
????<input?type="submit"?value="登录"?/>
11
</form>
12
</body>
13
</html>
01
<%@ page contentType="text/html;charset=utf-8"%>
02
<%@ page import="java.util.*"%>
03
<%
04
????request.setCharacterEncoding("UTF-8");
05
????// 取得登录的用户名
06
????String username = request.getParameter("username");
07
????// 把用户名保存进session
08
????session.setAttribute("username", username);
09
????// 把用户名放入在线列表
10
????List onlineUserList = (List) application.getAttribute("onlineUserList");
11
????// 第一次使用前,需要初始化
12
????if (onlineUserList == null) {
13
????????onlineUserList = new ArrayList();
14
????????application.setAttribute("onlineUserList", onlineUserList);
15
????}
16
????onlineUserList.add(username);
17
????// 成功
18
????response.sendRedirect("result.jsp");
19
%>
1
<%@ page contentType="text/html;charset=utf-8"%>
2
<%@ page isELIgnored="false"%>
3
<%@page import="java.util.List"%>
01
<h3>您好:${username} [<a?href="logout.jsp">注销</a>]</h3>
02
当前在线用户:
03
<table>
04
<%
05
????List onlineUserList = (List) application.getAttribute("onlineUserList");
06
????for (int i = 0; i <?onlineUserList.size(); i++) {
07
????String onlineUsername = (String) onlineUserList.get(i);
08
%>
09
????<tr>
10
????????<td><%=onlineUsername%></td>
11
????</tr>
12
<%
13
}
14
%>
15
</table>
01
<%@ page contentType="text/html;charset=utf-8"%>
02
<%@ page import="java.util.*"%>
03
<%
04
????// 取得登录的用户名
05
????String username = (String) session.getAttribute("username");
06
????// 销毁session
07
????session.invalidate();
08
????// 从在线列表中删除用户名
09
????List onlineUserList = (List) application.getAttribute("onlineUserList");
10
????onlineUserList.remove(username);
11
????// 成功
12
????response.sendRedirect("index.jsp");
13
%>
01
package?com.test;
02
?
03
import?java.util.List;
04
import?javax.servlet.ServletContext;
05
import?javax.servlet.http.HttpSession;
06
import?javax.servlet.http.HttpSessionEvent;
07
import?javax.servlet.http.HttpSessionListener;
08
/**
09
?* @author 版本
10
?*/
11
public?class?OnlineUserListener?implements?HttpSessionListener {
12
?
13
????public?void?sessionCreated(HttpSessionEvent event) {
14
????????System.out.println("新建session:"+event.getSession().getId());
15
????}
16
????public?void?sessionDestroyed(HttpSessionEvent event) {
17
????????HttpSession session = event.getSession();
18
????????ServletContext application = session.getServletContext();
19
????????// 取得登录的用户名
20
????????String username = (String) session.getAttribute("username");
21
????????// 从在线列表中删除用户名
22
????????List onlineUserList = (List) application.getAttribute("onlineUserList");
23
????????onlineUserList.remove(username);
24
????????System.out.println(username+"已经退出!");
25
????}
26
}
1
<listener>
2
??<listener-class>com.test.OnlineUserListener</listener-class>
3
</listener>
01
<%@page import="com.test.OnlineUserBindingListener"%>
02
<%@ page contentType="text/html;charset=utf-8"%>
03
<%@ page import="java.util.*"%>
04
<%
05
????request.setCharacterEncoding("UTF-8");
06
????// 取得登录的用户名
07
????String username = request.getParameter("username");
08
???????// 把用户名放入在线列表
09
????session.setAttribute("onlineUserBindingListener", new OnlineUserBindingListener(username));
10
????// 成功
11
????response.sendRedirect("result.jsp");
12
%>
01
package?com.test;
02
?
03
import?java.util.ArrayList;
04
import?java.util.List;
05
import?javax.servlet.ServletContext;
06
import?javax.servlet.http.HttpSession;
07
import?javax.servlet.http.HttpSessionBindingEvent;
08
import?javax.servlet.http.HttpSessionBindingListener;
09
?
10
public?class?OnlineUserBindingListener?implements?HttpSessionBindingListener {
11
????String username;
12
?????
13
????public?OnlineUserBindingListener(String username){
14
????????this.username=username;
15
????}
16
????public?void?valueBound(HttpSessionBindingEvent event) {
17
????????HttpSession session = event.getSession();
18
????????ServletContext application = session.getServletContext();
19
????????// 把用户名放入在线列表
20
????????List onlineUserList = (List) application.getAttribute("onlineUserList");
21
????????// 第一次使用前,需要初始化
22
????????if?(onlineUserList ==?null) {
23
????????????onlineUserList =?new?ArrayList();
24
????????????application.setAttribute("onlineUserList", onlineUserList);
25
????????}
26
????????onlineUserList.add(this.username);
27
????}
28
?
29
????public?void?valueUnbound(HttpSessionBindingEvent event) {
30
????????HttpSession session = event.getSession();
31
????????ServletContext application = session.getServletContext();
32
?
33
????????// 从在线列表中删除用户名
34
????????List onlineUserList = (List) application.getAttribute("onlineUserList");
35
????????onlineUserList.remove(this.username);
36
????????System.out.println(this.username +?"退出。");
37
?
38
????}
39
?
40
}