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桌面环境

    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

    2010-06-01 14:30:06

    Mrtg教程

    2013-09-10 14:50:12

    数组面试题

    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

    微信

    2020-05-15 15:21:58

    SPSS数据分析
    点赞
    收藏

    51CTO技术栈公众号