現(xiàn)在對(duì)于處理在線用戶,有幾種不同的處理方法。一種是頁面刷新由用戶控制,服務(wù)器端控制一個(gè)超時(shí)時(shí)間比如30分鐘,到了時(shí)間之后用戶沒有動(dòng)作就被踢出。這種方法的優(yōu)點(diǎn)是,如果用戶忘了退出,可以防止別人惡意操作。缺點(diǎn)是,如果你在做一件很耗時(shí)間的事情,超過了這個(gè)時(shí)間限制,submit的時(shí)候可能要再次面臨登陸。如果原來的頁面又是強(qiáng)制失效的話,就有可能丟失你做的工作。在實(shí)現(xiàn)的角度來看,這是最簡(jiǎn)單的,Server端默認(rèn)實(shí)現(xiàn)的就是這樣的模式。
另一種方式是,站點(diǎn)采用框架結(jié)構(gòu),有一個(gè)Frame或者隱藏的iframe在不斷刷新,這樣你永遠(yuǎn)不會(huì)被踢出,但是服務(wù)器端為了判斷你是否在線,需要定一個(gè)發(fā)呆時(shí)間,如果超過這個(gè)發(fā)呆時(shí)間你除了這個(gè)自動(dòng)刷新的頁面外沒有刷新其他頁面的話,就認(rèn)為你已經(jīng)不在線了。采取這種方式的典型是xici.net。 他的優(yōu)點(diǎn)是可以可以利用不斷的刷新實(shí)現(xiàn)一些類似server-push的功能,比如網(wǎng)友之間發(fā)送消息。
不管哪一種模式,為了實(shí)現(xiàn)瀏覽當(dāng)前所有的在線用戶,還需要做一些額外的工作。servlet API中沒有得到Session列表的API。
可以利用的是Listener. Servlet 2.2和2.3規(guī)范在這里略微有一些不一樣。2.2中HttpSessionBindingListener可以實(shí)現(xiàn)當(dāng)一個(gè)HTTPSession中的Attribute變化的時(shí)候通知你的類。而2.3中還引入了HttpSessionAttributeListener.鑒于我使用的環(huán)境是Visual age for java 4和JRun server 3.1,他們還不直接支持Servlet 2.3的編程,這里我用的是HttpSessionBindingListener.
需要做的事情包括做一個(gè)新的類來實(shí)現(xiàn)HttpSessionBindingListener接口。這個(gè)接口有兩個(gè)方法:
public void valueBound(HttpSessionBindingEvent event),和
public void valueUnbound(HttpSessionBindingEvent event)。
當(dāng)你執(zhí)行Session.addAttribute(String,Object)的時(shí)候,如果你已經(jīng)把一個(gè)實(shí)現(xiàn)了HttpSessionBindingListener接口的類加入為Attribute,Session會(huì)通知你的類,調(diào)用你的valueBound方法。相反,Session.removeAttribute方法對(duì)應(yīng)的是valueUndound方法。
1
public class HttpSessionBinding implements javax.servlet.http.HttpSessionBindingListener
2
{
3
ServletContext application = null;
4
5
public HttpSessionBinding(ServletContext application)
6
{
7
super();
8
if (application ==null)
9
throw new IllegalArgumentException("Null application is not accept.");
10
11
this.application = application;
12
13
}
14
15
public void valueBound(javax.servlet.http.HttpSessionBindingEvent e)
16
{
17
Vector activeSessions = (Vector) application.getAttribute("activeSessions");
18
if (activeSessions == null)
19
{
20
activeSessions = new Vector();
21
}
22
23
JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");
24
if (sessionUser != null)
25
{
26
activeSessions.add(e.getSession());
27
}
28
application.setAttribute("activeSessions",activeSessions);
29
}
30
31
public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent e)
32
{
33
JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");
34
if (sessionUser == null)
35
{
36
Vector activeSessions = (Vector) application.getAttribute("activeSessions");
37
if (activeSessions != null)
38
{
39
activeSessions.remove(e.getSession().getId());
40
application.setAttribute("activeSessions",activeSessions);
41
}
42
}
43
}
44
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

假設(shè)其中的JDBCUser類是一個(gè)任意User類。在執(zhí)行用戶登錄時(shí),把User類和HttpSessionBinding類都加入到Session中去。這樣,每次用戶登錄后,在application中的attribute "activeSessions"這個(gè)vector中都會(huì)增加一條記錄。每當(dāng)session超時(shí),valueUnbound被觸發(fā),在這個(gè)vector中刪去將要被超時(shí)的session。
1
public void login()
2
throws ACLException,SQLException,IOException
3
{
4
/* get JDBC User Class */
5
if (user != null)
6
{
7
logout();
8
}
9
{
10
// if session time out, or user didn't login, save the target url temporary.
11
12
JDBCUserFactory uf = new JDBCUserFactory();
13
14
if ( (this.request.getParameter("userID")==null)
15
|| (this.request.getParameter("password")==null) )
16
{
17
throw new ACLException("Please input a valid userName and password.");
18
}
19
20
JDBCUser user =
21
(JDBCUser) uf.UserLogin(
22
this.request.getParameter("userID"),
23
this.request.getParameter("password") );
24
user.touchLoginTime();
25
this.session.setAttribute("user",user);
26
this.session.setAttribute("BindingNotify",new HttpSessionBinding(application));
27
}
28
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

這兩個(gè)函數(shù)位于一個(gè)HttpSessionManager類中.這個(gè)類引用了jsp里面的application全局對(duì)象。這個(gè)類的其他代碼和本文無關(guān)且相當(dāng)長(zhǎng),我就不貼出來了。
下面來看看jsp里面怎么用。
假設(shè)一個(gè)登錄用的表單被提交到doLogin.jsp, 表單中包含UserName和password域。
節(jié)選部分片段:
1
<%
2
HttpSessionManager hsm = new HttpSessionManager(application,request,response);
3
try
4
{
5
hsm.login();
6
}
7
catch ( UserNotFoundException e)
8
{
9
response.sendRedirect("InsufficientPrivilege.jsp?detail=User%20does%20not%20exist.");
10
return;
11
}
12
catch ( InvalidPasswordException e2)
13
{
14
response.sendRedirect("InsufficientPrivilege.jsp?detail=Invalid%20Password");
15
return;
16
}
17
catch ( Exception e3)
18
{
19
%> Error:<%=e3.toString() %><br>
20
Press <a href="login.jsp">Here</a> to relogin.
21
<% return;
22
}
23
response.sendRedirect("index.jsp");
24
%>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

再來看看現(xiàn)在我們?cè)趺吹玫揭粋€(gè)當(dāng)前在線的用戶列表。
1
<body bgcolor="#FFFFFF">
2
<table cellspacing="0" cellpadding="0" width="100%">
3
4
<tr >
5
<td style="width:24px">SessionId
6
</td>
7
<td style="width:80px" >User
8
</td>
9
<td style="width:80px" >Login Time
10
</td>
11
<td style="width:80px" >Last access Time
12
</td>
13
</tr>
14
<%
15
Vector activeSessions = (Vector) application.getAttribute("activeSessions");
16
if (activeSessions == null)
17
{
18
activeSessions = new Vector();
19
application.setAttribute("activeSessions",activeSessions);
20
}
21
22
Iterator it = activeSessions.iterator();
23
while (it.hasNext())
24
{
25
HttpSession sess = (HttpSession)it.next();
26
JDBCUser sessionUser = (JDBCUser)sess.getAttribute("user");
27
String userId = (sessionUser!=null)?sessionUser.getUserID():"None";
28
%>
29
<tr>
30
<td nowrap=''><%= sess.getId() %></td>
31
<td nowrap=''><%= userId %></td>
32
<td nowrap=''>
33
<%= BeaconDate.getInstance( new java.util.Date(sess.getCreationTime())).getDateTimeString()%></td>
34
<td class="<%= stl %>3" nowrap=''>
35
<%= BeaconDate.getInstance( new java.util.Date(sess.getLastAccessedTime())).getDateTimeString()%></td>
36
</tr>
37
<%
38
}
39
%>
40
</table>
41
</body>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

以上的代碼從application中取出activeSessions,并且顯示出具體的時(shí)間。其中BeaconDate類假設(shè)為格式化時(shí)間的類。
這樣,我們得到了一個(gè)察看在線用戶的列表的框架。至于在線用戶列表分頁等功能,與本文無關(guān),不予討論。
這是一個(gè)非刷新模型的例子,依賴于session的超時(shí)機(jī)制。我的同事sonymusic指出很多時(shí)候由于各個(gè)廠商思想的不同,這有可能是不可信賴的。考慮到這種需求,需要在每個(gè)頁面刷新的時(shí)候都判斷當(dāng)前用戶距離上次使用的時(shí)間是否超過某一個(gè)預(yù)定時(shí)間值。這實(shí)質(zhì)上就是自己實(shí)現(xiàn)session超時(shí)。如果需要實(shí)現(xiàn)刷新模型,就必須使用這種每個(gè)頁面進(jìn)行刷新判斷的方法。
附:關(guān)閉瀏覽器窗口,釋放session的問題
在主頁中添加以下代碼:
1
<script type="text/javascript">
2
function MM_callJS(jsStr) {
3
return eval(jsStr);
4
}
5
6
function removeline() {
7
if (event.clientX < 0 && event.clientY < 0) {
8
document.write('<iframe width="100" height="100" src="logout.jsp"></iframe><OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>');
9
document.all.WebBrowser.ExecWB(45, 1);
10
}
11
}
12
</script>
13
14
<frameset id="frame1" onUnload="MM_callJS('removeline()')">
代碼解釋:
2

3

4

5

6

7

8

9

10

11

12

13

14

if(event.clientX<0&&event.clientY<0)判斷瀏覽器是關(guān)閉還是刷新,因?yàn)樗⑿乱矔?huì)調(diào)用onunload。
document.all.WebBrowser.ExecWB(45,1);是無提示的關(guān)閉瀏覽器。
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2
這個(gè)是調(diào)用不彈出對(duì)話框的方法,實(shí)際是調(diào)用系統(tǒng)的方法如下
document.all.WebBrowser.ExecWB(45,1);
因?yàn)樵趈avascript當(dāng)中不能調(diào)用java方法,所以選擇另外寫一個(gè)logout.jsp文件用于調(diào)用invalidate()方法。
注意:session在調(diào)用invalidate()方法前要removeAttribute掉相關(guān)的name。
限制用戶重復(fù)登錄
OnlineUserBindingListener.java
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class OnlineUserBindingListener implements HttpSessionBindingListener {
String username;
public OnlineUserBindingListener(){
}
public OnlineUserBindingListener(String username){
this.username=username;
}
public void valueBound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 把用戶名放入在線列表
List onlineUserList = (List) application.getAttribute("onlineUserList");
// 第一次使用前,需要初始化
if (onlineUserList == null) {
onlineUserList = new ArrayList();
}
onlineUserList.add(this.username);
application.setAttribute("onlineUserList", onlineUserList);
}
public void valueUnbound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 從在線列表中刪除用戶名
List onlineUserList = (List) application.getAttribute("onlineUserList");
onlineUserList.remove(this.username);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(this.username + "退出系統(tǒng)。");
System.out.println("退出時(shí)間"+sdf.format(new Date()));
}
}
登陸驗(yàn)證
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class OnlineUserBindingListener implements HttpSessionBindingListener {
String username;
public OnlineUserBindingListener(){
}
public OnlineUserBindingListener(String username){
this.username=username;
}
public void valueBound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 把用戶名放入在線列表
List onlineUserList = (List) application.getAttribute("onlineUserList");
// 第一次使用前,需要初始化
if (onlineUserList == null) {
onlineUserList = new ArrayList();
}
onlineUserList.add(this.username);
application.setAttribute("onlineUserList", onlineUserList);
}
public void valueUnbound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 從在線列表中刪除用戶名
List onlineUserList = (List) application.getAttribute("onlineUserList");
onlineUserList.remove(this.username);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(this.username + "退出系統(tǒng)。");
System.out.println("退出時(shí)間"+sdf.format(new Date()));
}
}
OnlineUserBindingListener online = new OnlineUserBindingListener();
List list = (List) application.getAttribute("onlineUserList");
boolean flag = false;
if (list != null) {
for (int i = 0; i < list.size(); i++) {
String param = (String) list.get(i);
if (param.equals(username)) {
flag = true;
break;
}
}
}
if (flag) {
out.println("<script language='javascript'>alert('該用戶正在使用中!');"
+ "parent.location.href='login.jsp';</script>");
} else {
session.setAttribute("onlineUserBindingListener",
new OnlineUserBindingListener(username));
}
退出系統(tǒng)List list = (List) application.getAttribute("onlineUserList");
boolean flag = false;
if (list != null) {
for (int i = 0; i < list.size(); i++) {
String param = (String) list.get(i);
if (param.equals(username)) {
flag = true;
break;
}
}
}
if (flag) {
out.println("<script language='javascript'>alert('該用戶正在使用中!');"
+ "parent.location.href='login.jsp';</script>");
} else {
session.setAttribute("onlineUserBindingListener",
new OnlineUserBindingListener(username));
}
// 從在線列表中刪除用戶名
List onlineUserList = (List) application.getAttribute("onlineUserList");
onlineUserList.remove(session.getAttribute("username"));
session.invalidate();
List onlineUserList = (List) application.getAttribute("onlineUserList");
onlineUserList.remove(session.getAttribute("username"));
session.invalidate();