本文描述JSP中基于Session的在线用户统计分析,以及Session就是它提供的基础设施之一。

JSP作为后起之秀能够在服务器编程环境中占有必定位置,是和它杰出支撑一系列业界标准密切相关的。Session便是它供给的基础设施之一。作为一个程序员,你能够不介意详细在客户端是怎么完成,就便利的完成简略的根据Session的用户办理。现在关于处理在线用户,有几种不同的处理办法。

JSP中根据Session的在线用户统计分析(session统计在线人数)  Session JSP 第1张

一种是页面改写由用户操控,服务器端操控一个超时时刻比方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. publicvoidvalueBound(HttpSessionBindingEventevent)
  2. publicvoidvalueUnbound(HttpSessionBindingEventevent)

当你履行Session.addAttribute(String,Object)的时分,假如你现已把一个完成了HttpSessionBindingListener接口的类参加为Attribute,Session会告诉你的类,调用你的 valueBound办法。相反,Session.removeAttribute办法对应的是valueUndound办法。

  1. publicclassHttpSessionBindingimplementsjavax.servlet.
    http.HttpSessionBindingListener
  2. { 
  3. ServletContextapplication=null; 
  4. publicHttpSessionBinding(ServletContextapplication) 
  5. {  
  6. super();  
  7. if(application==null)   
  8. thrownewIllegalArgumentException("Nullapplicationisnotaccept.");  
  9. this.application=application; 
  10. } 
  11. publicvoidvalueBound(javax.servlet.http.HttpSessionBindingEvente)
  12. {  
  13. VectoractiveSessions=(Vector)application.getAttribute
    ("activeSessions");  
  14. if(activeSessions==null)  
  15. {   
  16. activeSessions=newVector();  
  17. }  
  18. JDBCUsersessionUser=(JDBCUser)e.getSession().getAttribute("user");
  19. if(sessionUser!=null)  
  20. {   
  21. activeSessions.add(e.getSession());  
  22. }  
  23. application.setAttribute("activeSessions",activeSessions); 
  24. } 
  25. publicvoidvalueUnbound(javax.servlet.http.HttpSessionBindingEvente) 
  26. {  
  27. JDBCUsersessionUser=(JDBCUser)e.getSession().getAttribute("user");
  28. if(sessionUser==null)  
  29. {   
  30. VectoractiveSessions=(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. publicvoidlogin()
    2. throwsACLException,SQLException,IOException
    3. { 
    4. /*getJDBCUserClass*/ 
    5. if(user!=null) 
    6. {  
    7. logout(); 
    8. } 
    9. {  
    10. //ifsessiontimeout,oruserdidn'tlogin,
      savethetargeturltemporary.  
    11. JDBCUserFactoryuf=newJDBCUserFactory();  
    12. if((this.request.getParameter("userID")==null)
    13. ||(this.request.getParameter("password")==null))  
    14. {   
    15. thrownewACLException("Pleaseinputavalid
      userNameandpassword.");}  
    16. JDBCUseruser=(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. publicvoidlogout()
    2. throwsSQLException,ACLException
    3. { 
    4. if(this.user==null&&this.session.getAttribute("user")==null) 
    5. {  
    6. return; 
    7. } 
    8. VectoractiveSessions=(Vector)this.application.
      getAttribute("activeSessions"); 
    9. if(activeSessions!=null) 
    10. {
    11. activeSessions.remove(this.session);  
    12. application.setAttribute("activeSessions",activeSessions); 
    13. } 
    14. java.util.Enumeratione=this.session.getAttributeNames(); 
    15. while(e.hasMoreElements()) 
    16. {  
    17. Strings=(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
    转载请说明出处
    知优网 » JSP中根据Session的在线用户统计分析(session统计在线人数)

    发表评论

    您需要后才能发表评论