成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

JSP中基于Session的在線用戶統計分析

開發 后端
本文描述JSP中基于Session的在線用戶統計分析,以及Session就是它提供的基礎設施之一。

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接口。這個接口有兩個方法:

  1. public void valueBound(HttpSessionBindingEvent event)  
  2. public void valueUnbound(HttpSessionBindingEvent event) 

當你執行Session.addAttribute(String,Object)的時候,如果你已經把一個實現了HttpSessionBindingListener接口的類加入為Attribute,Session會通知你的類,調用你的 valueBound方法。相反,Session.removeAttribute方法對應的是valueUndound方法。

  1. public class HttpSessionBinding implements javax.servlet.
    http.HttpSessionBindingListener     
  2. {     
  3. ServletContext application = null;      
  4. public HttpSessionBinding(ServletContext application)     
  5. {      
  6. super();      
  7. if (application ==null)       
  8. throw new IllegalArgumentException("Null application is not accept.");      
  9. this.application = application;     
  10. }      
  11. public void valueBound(javax.servlet.http.HttpSessionBindingEvent e)    
  12. {       
  13. Vector activeSessions = (Vector) application.getAttribute
    ("activeSessions");      
  14. if (activeSessions == null)      
  15. {       
  16. activeSessions = new Vector();      
  17. }       
  18. JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");   
  19. if (sessionUser != null)      
  20. {       
  21. activeSessions.add(e.getSession());      
  22. }      
  23. application.setAttribute("activeSessions",activeSessions);     
  24. }      
  25. public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent e)      
  26. {      
  27. JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");   
  28. if (sessionUser == null)      
  29. {       
  30. Vector activeSessions = (Vector) application.getAttribute
    ("activeSessions");       
  31. if (activeSessions != null)       
  32. {        
  33. activeSessions.remove(e.getSession().getId());      
  34. application.setAttribute("activeSessions",activeSessions);       
  35. }      
  36. }     
  37. }    
  38. }  

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

    這樣,每次用戶登錄后,在application中的attribute "activeSessions"這個vector中都會增加一條記錄。每當session超時,valueUnbound被觸發,在這個vector中刪去將要被超時的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. JDBCUserFactory uf = new JDBCUserFactory();       
    12. if ( (this.request.getParameter("userID")==null)     
    13. || (this.request.getParameter("password")==null) )      
    14. {       
    15. throw new ACLException("Please input a valid 
      userName andpassword."); }       
    16. JDBCUser user = (JDBCUser) uf.UserLogin(     
    17. this.request.getParameter("userID"),     
    18. this.request.getParameter("password") );       
    19. user.touchLoginTime();       
    20. this.session.setAttribute("user",user);     
    21. this.session.setAttribute("BindingNotify",new 
      HttpSessionBinding(application));      
    22. }     
    23. }  

    Login的時候,把User和這個BindingNotofy目的的類都加入到session中去。logout的時候,就要主動在activeSessions這個vector中刪去這個session。

    1. public void logout()    
    2. throws SQLException,ACLException    
    3. {     
    4. if (this.user == null && this.session.getAttribute("user")==null)     
    5. {      
    6. return;     
    7. }      
    8. Vector activeSessions = (Vector)this.application.
      getAttribute("activeSessions");     
    9. if (activeSessions != null)     
    10. {  
    11. activeSessions.remove(this.session);    
    12. application.setAttribute("activeSessions",activeSessions);     
    13. }      
    14. java.util.Enumeration e = this.session.getAttributeNames();      
    15. while (e.hasMoreElements())     
    16. {      
    17. String s = (String)e.nextElement();    
    18. this.session.removeAttribute(s);     
    19. }     
    20. this.user.touchLogoutTime();     
    21. this.user = null;    
    22. }  
    
    

    【編輯推薦】

    1. 拋開JSP,直接從JSF入手
    2. JSF和JSP是一對新的搭檔
    3. 在JSP程序中Application使用淺析
    4. JavaBean實現JSP頁面和代碼分離
    5. 在JSF/JSP中集成FCKEditor
    責任編輯:佚名 來源: builder
    相關推薦

    2019-09-18 09:05:58

    技術SQLDevOps

    2015-08-19 09:40:51

    統計分析

    2012-09-25 14:18:51

    Linux桌面環境

    2024-11-07 11:10:34

    Python腳本統計分析

    2014-06-04 09:42:41

    工業控制系統APT

    2015-02-27 09:53:06

    Countly

    2010-12-30 09:44:10

    C#Attribute

    2011-03-14 14:41:59

    大數據數據中心

    2015-01-05 17:28:02

    JMP

    2011-05-27 13:56:04

    網站流量

    2023-10-25 09:39:46

    2013-09-10 14:50:12

    數組面試題

    2010-06-01 14:30:06

    Mrtg教程

    2014-10-28 14:59:42

    手游付費行為數據統計分析

    2010-06-07 17:26:41

    Mrtg教程

    2022-02-18 12:58:35

    go-monitor工具服務質量

    2021-06-10 09:53:04

    數據統計統計分析數據

    2016-12-26 09:49:39

    httpstatcurl分析工具

    2011-05-27 14:03:22

    網站流量

    2016-12-01 17:33:52

    微信
    點贊
    收藏

    51CTO技術棧公眾號

    主站蜘蛛池模板: 国久久 | 精品国产18久久久久久二百 | 一个色在线| 嫩草视频网 | 成人综合一区 | 日韩精品在线观看一区二区三区 | 国产精品久久久久久久一区二区 | 成人精品久久 | 国产精品久久久久久久久久久免费看 | 日韩在线播放第一页 | av在线成人 | 国产精品视频网站 | 97久久精品午夜一区二区 | 午夜影院在线观看视频 | 久久亚洲精品久久国产一区二区 | 日本成人三级电影 | 国产精品久久久久久久久动漫 | 亚洲一区二区三区免费在线 | 欧美一区二区三区大片 | 在线91| 欧美色偷拍| 一区二区三区精品视频 | 日韩影音 | 亚洲在线中文字幕 | 国产91视频免费 | av在线一区二区 | 日本一区二区三区在线观看 | 日韩一区二区在线观看视频 | 久久久妇女国产精品影视 | 超碰97干 | 欧美一级大片免费看 | 精品免费国产一区二区三区四区 | 国产一区二区不卡 | 一区二区三区四区在线 | 人人人干 | 国产清纯白嫩初高生在线播放视频 | 国产精品夜夜夜一区二区三区尤 | 久久精品久久久久久 | 日韩有码在线观看 | 日韩精品一区二区三区中文在线 | 国产日韩欧美激情 |