JSP
作為后起之秀能夠在
服務(wù)器編程環(huán)境中占據(jù)一定地位,是和它良好支持一系列
業(yè)界標(biāo)準(zhǔn)密切相關(guān)的。Session就是它提供的
基礎(chǔ)設(shè)施之一。作為一個(gè)程序員,你可以不介意具體在客戶端是如何實(shí)現(xiàn),就方便的實(shí)現(xiàn)簡(jiǎn)單的基于session的用戶管理。現(xiàn)在對(duì)于處理在線用戶,有幾種不同的處理方法。
一種是頁(yè)面刷新由用戶控制,服務(wù)器端控制一個(gè)超時(shí)時(shí)間比如30分鐘,到了時(shí)間之后用戶沒有動(dòng)作就被踢出。這種方法的優(yōu)點(diǎn)是,如果用戶忘了退出,可以防止別人惡意操作。缺點(diǎn)是,如果你在做一件很耗時(shí)間的事情,超過(guò)了這個(gè)時(shí)間限制,submit的時(shí)候可能要再次面臨登陸。如果原來(lái)的葉面又是強(qiáng)制失效的話,就有可能丟失你做的工作。在實(shí)現(xiàn)的角度來(lái)看,這是最簡(jiǎn)單的,Server端默認(rèn)實(shí)現(xiàn)的就是這樣的模式。
另一種方式是,站點(diǎn)采用框架結(jié)構(gòu),有一個(gè)Frame或者隱藏的iframe在不斷刷新,這樣你永遠(yuǎn)不會(huì)被踢出,但是服務(wù)器端為了判斷你是否在線,需要定一個(gè)發(fā)呆時(shí)間,如果超過(guò)這個(gè)發(fā)呆時(shí)間你除了這個(gè)自動(dòng)刷新的頁(yè)面外沒有刷新其他頁(yè)面的話,就認(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è)新的類來(lái)實(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方法。
public class HttpSessionBinding implements javax.servlet.http.HttpSessionBindingListener { ServletContext application = null;
public HttpSessionBinding(ServletContext application) { super(); if (application ==null) throw new IllegalArgumentException("Null application is not accept."); this.application = application; }
public void valueBound(javax.servlet.http.HttpSessionBindingEvent e) { Vector activeSessions = (Vector) application.getAttribute("activeSessions"); if (activeSessions == null) { activeSessions = new Vector(); }
JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user"); if (sessionUser != null) { activeSessions.add(e.getSession()); } application.setAttribute("activeSessions",activeSessions); }
public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent e) { JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user"); if (sessionUser == null) { Vector activeSessions = (Vector) application.getAttribute("activeSessions"); if (activeSessions != null) { activeSessions.remove(e.getSession().getId()); application.setAttribute("activeSessions",activeSessions); } } } } |
假設(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.
public void login() throws ACLException,SQLException,IOException { /* get JDBC User Class */ if (user != null) { logout(); } { // if session time out, or user didn't login, save the target url temporary.
JDBCUserFactory uf = new JDBCUserFactory();
if ( (this.request.getParameter("userID")==null) || (this.request.getParameter("password")==null) ) { throw new ACLException("Please input a valid userName and password."); }
JDBCUser user = (JDBCUser) uf.UserLogin( this.request.getParameter("userID"), this.request.getParameter("password") ); user.touchLoginTime(); this.session.setAttribute("user",user); this.session.setAttribute("BindingNotify",new HttpSessionBinding(application)); } } |
Login的時(shí)候,把User和這個(gè)BindingNotofy目的的類都加入到session中去。logout的時(shí)候,就要主動(dòng)在activeSessions這個(gè)vector中刪去這個(gè)session.
public void logout() throws SQLException,ACLException { if (this.user == null && this.session.getAttribute("user")==null) { return; }
Vector activeSessions = (Vector) this.application.getAttribute("activeSessions"); if (activeSessions != null) { activeSessions.remove(this.session); application.setAttribute("activeSessions",activeSessions); }
java.util.Enumeration e = this.session.getAttributeNames();
while (e.hasMoreElements()) { String s = (String)e.nextElement(); this.session.removeAttribute(s); } this.user.touchLogoutTime(); this.user = null; } |
這兩個(gè)函數(shù)位于一個(gè)HttpSessionManager類中.這個(gè)類引用了jsp里面的application全局對(duì)象。這個(gè)類的其他代碼和本文無(wú)關(guān)且相當(dāng)長(zhǎng),我就不貼出來(lái)了。
下面來(lái)看看
JSP里面怎么用。
假設(shè)一個(gè)登錄用的表單被提交到doLogin.jsp, 表單中包含UserName和password域。節(jié)選部分片段:
<% HttpSessionManager hsm = new HttpSessionManager(application,request,response); try { hsm.login(); } catch ( UserNotFoundException e) { response.sendRedirect("InsufficientPrivilege.jsp?detail=User%20does%20not%20exist."); return; } catch ( InvalidPasswordException e2) { response.sendRedirect("InsufficientPrivilege.jsp?detail=Invalid%20Password"); return; } catch ( Exception e3) { %> Error:<%=e3.toString() %><br> Press <a href="login.jsp">Here</a> to relogin. <% return; } response.sendRedirect("index.jsp"); %> |
再來(lái)看看現(xiàn)在我們?cè)趺吹玫揭粋€(gè)當(dāng)前在線的用戶列表。
<body bgcolor="#FFFFFF"> <table cellspacing="0" cellpadding="0" width="100%">
<tr > <td style="width:24px">SessionId </td> <td style="width:80px" >User </td> <td style="width:80px" >Login Time </td> <td style="width:80px" >Last Access Time </td> </tr> <% Vector activeSessions = (Vector) application.getAttribute("activeSessions"); if (activeSessions == null) { activeSessions = new Vector(); application.setAttribute("activeSessions",activeSessions); }
Iterator it = activeSessions.iterator(); while (it.hasNext()) { HttpSession sess = (HttpSession)it.next(); JDBCUser sessionUser = (JDBCUser)sess.getAttribute("user"); String userId = (sessionUser!=null)?sessionUser.getUserID():"None"; %> <tr> <td nowrap=''><%= sess.getId() %></td> <td nowrap=''><%= userId %></td> <td nowrap=''> <%= BeaconDate.getInstance( new Java.util.Date(sess.getCreationTime())).getDateTimeString()%></td> <td class="<%= stl %>3" nowrap=''> <%= BeaconDate.getInstance( new java.util.Date(sess.getLastAccessedTime())).getDateTimeString()%></td> </tr> <% } %> </table> </body> |
以上的代碼從application中取出activeSessions,并且顯示出具體的時(shí)間。其中BeaconDate類假設(shè)為格式化時(shí)間的類。
這樣,我們得到了一個(gè)察看在線用戶的列表的框架。至于在線用戶列表分頁(yè)等功能,與本文無(wú)關(guān),不予討論。
這是一個(gè)非刷新模型的例子,依賴于session的超時(shí)機(jī)制。我的同事sonymusic指出很多時(shí)候由于各個(gè)廠商思想的不同,這有可能是不可信賴的。考慮到這種需求,需要在每個(gè)葉面刷新的時(shí)候都判斷當(dāng)前用戶距離上次使用的時(shí)間是否超過(guò)某一個(gè)預(yù)定時(shí)間值。這實(shí)質(zhì)上就是自己實(shí)現(xiàn)session超時(shí)。如果需要實(shí)現(xiàn)刷新模型,就必須使用這種每個(gè)葉面進(jìn)行刷新判斷的方法。