JSP作為后起之秀能夠在服務器編程環境中占據一定地位,是和它良好支持一系列業界標準
密切相關的。Session就是它提供的基礎設施之一。作為一個程序員,你可以不介意具體在
客戶端是如何實現,就方便的實現簡單的基于session的用戶管理。
現在對于處理在線用戶,有幾種不同的處理方法。
一種是葉面刷新由用戶控制,服務器端控制一個超時時間比如30分鐘,到了時間之后用戶
沒有動作就被踢出。這種方法的優點是,如果用戶忘了退出,可以防止別人惡意操作。
缺點是,如果你在做一件很耗時間的事情,超過了這個時間限制,submit的時候可能要
再次面臨登陸。如果原來的葉面又是強制失效的話,就有可能丟失你做的工作。在實現
的角度來看,這是最簡單的,Server端默認實現的就是這樣的模式。
另一種方式是,站點采用框架結構,有一個Frame或者隱藏的iframe在不斷刷新,這樣你
永遠不會被踢出,但是服務器端為了判斷你是否在線,需要定一個發呆時間,如果超過
這個發呆時間你除了這個自動刷新的葉面外沒有刷新其他葉面的話,就認為你已經不在
線了。采取這種方式的典型是xici.net。 他的優點是可以可以利用不斷的刷新實現一些
類似server-push的功能,比如網友之間發送消息。
不管哪一種模式,為了實現瀏覽當前所有的在線用戶,還需要做一些額外的工作。
servlet API中沒有得到Session列表的API。
可以利用的是Listener. Servlet 2.2和2.3規范在這里略微有一些不一樣。
2.2中HttpSessionBindingListener可以實現當一個HTTPSession中的Attribute變化的
時候通知你的類。而2.3中還引入了HttpSessionAttributeListener.鑒于我使用的環境
是Visual age for java 4和JRun server 3.1,他們還不直接支持Servlet 2.3的編程,
這里我用的是HttpSessionBindingListener.
需要做的事情包括做一個新的類來實現HttpSessionBindingListener接口。這個接口有
兩個方法:
public void valueBound(HttpSessionBindingEvent event),和
public void valueUnbound(HttpSessionBindingEvent event)。
當你執行Session.addAttribute(String,Object)的時候,如果你已經把一個實現了
HttpSessionBindingListener接口的類加入為Attribute,Session會通知你的類,調用
你的valueBound方法。相反,Session.removeAttribute方法對應的是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); } } }}
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)); }}
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;}
<% HttpSessionManager hsm = new HttpSessionManager(application,request,response); try { hsm.login(); } catch ( UserNotFoundException e) { response.sendRedirect("Insufficient
再來看看現在我們怎么得到一個當前在線的用戶列表。<body bgcolor="#FFFFFF">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>
<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
(出處:http://www.49028c.com)
新聞熱點
疑難解答