JSP中基於Session的線上使用者統計分析

來源:互聯網
上載者:User
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)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) } }}

  假設其中的JDBCUser類是一個任意User類。在執行使用者登入時,把User類和HttpSessionBinding類都加入到Session中去。

  這樣,每次使用者登入後,在application中的attribute "activeSessions"這個vector中都會增加一條記錄。每當session逾時,valueUnbound被觸發,在這個vector中刪去將要被逾時的session。

  public void login()throws ACLException,SQLException,IOException{ /* get JDBC User Class */ if (user != null)  {// 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的時候,把User和這個BindingNotofy目的的類都加入到session中去。logout的時候,就要主動在activeSessions這個vector中刪去這個session。

  public void logout()throws SQLException,ACLException{ if (this.user == null && this.session.getAttribute("user")==null)  Vector activeSessions = (Vector) this.application.getAttribute("activeSessions"); if (activeSessions != null)  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類中.這個類引用了jsp裡面的application全域對象。這個類的其他代碼和本文無關且相當長,我就不貼出來了。

  下面來看看jsp裡面怎麼用。

  假設一個登入用的表單被提交到doLogin.jsp, 表單中包含UserName和password域。節選部分區段:

  Error:

  Press Here to relogin.

  再來看看現在我們怎麼得到一個當前線上的使用者列表。

  SessionIdUserLogin TimeLast access Time

  以上的代碼從application中取出activeSessions,並且顯示出具體的時間。其中BeaconDate類假設為格式化時間的類。

  這樣,我們得到了一個察看線上使用者的列表的架構。至於線上使用者列表分頁等功能,與本文無關,不予討論。

  這是一個非重新整理模型的例子,依賴於session的逾時機制。我的同事sonymusic指出很多時候由於各個廠商思想的不同,這有可能是不可信賴的。考慮到這種需求,需要在每個頁面重新整理的時候都判斷目前使用者距離上次使用的時間是否超過某一個預定時間值。這實質上就是自己實現session逾時。如果需要實現重新整理模型,就必須使用這種每個頁面進行重新整理判斷的方法。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.