知识大全 关于JSP中基于Session的在线用户统计分析[2]
Posted 统计分析
篇首语:苟利国家生死以,岂因祸福避趋之。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 关于JSP中基于Session的在线用户统计分析[2]相关的知识,希望对你有一定的参考价值。
关于JSP中基于Session的在线用户统计分析[2] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
假设其中的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) logout(); // 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) return; Vector activeSessions = (Vector) this application getAttribute( activeSessions ); if (activeSessions != null) activeSessions remove(this session); application setAttribute( activeSessions activeSessions); 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域 节选部分片段
<% HttpSessionManager hsm = new HttpSessionManager(application request response); try hsm login(); catch ( UserNotFoundException e) response sendRedirect( InsufficientPrivilege jsp?detail=User% does% not% exist ); return; catch ( InvalidPasswordException e ) response sendRedirect( InsufficientPrivilege jsp?detail=Invalid% Password ); return; catch ( Exception e ) %> Error:<%=e toString() %><br> Press <a href= login jsp >Here</a> to relogin <% return; response sendRedirect( index jsp ); %>
cha138/Article/program/Java/JSP/201311/20685相关参考