本文描述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接口。这个接口有两个办法:
- publicvoidvalueBound(HttpSessionBindingEventevent)
- publicvoidvalueUnbound(HttpSessionBindingEventevent)
当你履行Session.addAttribute(String,Object)的时分,假如你现已把一个完成了HttpSessionBindingListener接口的类参加为Attribute,Session会告诉你的类,调用你的 valueBound办法。相反,Session.removeAttribute办法对应的是valueUndound办法。
- publicclassHttpSessionBindingimplementsjavax.servlet.
http.HttpSessionBindingListener- {
- ServletContextapplication=null;
- publicHttpSessionBinding(ServletContextapplication)
- {
- super();
- if(application==null)
- thrownewIllegalArgumentException("Nullapplicationisnotaccept.");
- this.application=application;
- }
- publicvoidvalueBound(javax.servlet.http.HttpSessionBindingEvente)
- {
- VectoractiveSessions=(Vector)application.getAttribute
("activeSessions");- if(activeSessions==null)
- {
- activeSessions=newVector();
- }
- JDBCUsersessionUser=(JDBCUser)e.getSession().getAttribute("user");
- if(sessionUser!=null)
- {
- activeSessions.add(e.getSession());
- }
- application.setAttribute("activeSessions",activeSessions);
- }
- publicvoidvalueUnbound(javax.servlet.http.HttpSessionBindingEvente)
- {
- JDBCUsersessionUser=(JDBCUser)e.getSession().getAttribute("user");
- if(sessionUser==null)
- {
- VectoractiveSessions=(Vector)application.getAttribute
("activeSessions");- if(activeSessions!=null)
- {
- activeSessions.remove(e.getSession().getId());
- application.setAttribute("activeSessions",activeSessions);
- }
- }
- }
- }
假定其间的JDBCUser类是一个恣意User类。在履行用户登录时,把User类和HttpSessionBinding类都参加到Session中去。
这样,每次用户登录后,在application中的attribute "activeSessions"这个vector中都会添加一条记载。每逢session超时,valueUnbound被触发,在这个vector中删去将要被超时的session。
- publicvoidlogin()
- throwsACLException,SQLException,IOException
- {
- /*getJDBCUserClass*/
- if(user!=null)
- {
- logout();
- }
- {
- //ifsessiontimeout,oruserdidn'tlogin,
savethetargeturltemporary.- JDBCUserFactoryuf=newJDBCUserFactory();
- if((this.request.getParameter("userID")==null)
- ||(this.request.getParameter("password")==null))
- {
- thrownewACLException("Pleaseinputavalid
userNameandpassword.");}- JDBCUseruser=(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。
- publicvoidlogout()
- throwsSQLException,ACLException
- {
- if(this.user==null&&this.session.getAttribute("user")==null)
- {
- return;
- }
- VectoractiveSessions=(Vector)this.application.
getAttribute("activeSessions");- if(activeSessions!=null)
- {
- activeSessions.remove(this.session);
- application.setAttribute("activeSessions",activeSessions);
- }
- java.util.Enumeratione=this.session.getAttributeNames();
- while(e.hasMoreElements())
- {
- Strings=(String)e.nextElement();
- this.session.removeAttribute(s);
- }
- this.user.touchLogoutTime();
- this.user=null;
- }
【修改引荐】
- 抛开JSP,直接从JSF下手
- JSF和JSP是一对新的伙伴
- 在JSP程序中Application运用浅析
- JavaBean完成JSP页面和代码别离
- 在JSF/JSP中集成FCKEditor
知优网 » JSP中根据Session的在线用户统计分析(session统计在线人数)