public void valuebound(httpsessionbindingevent event) public void valueunbound(httpsessionbindingevent event) |
public class httpsessionbinding implements javax.servlet.http.httpsessionbindinglistener { servletcontext application = null; public httpsessionbinding(servletcontext application) { super(); if (application ==null) throw new illegalargumentexception("null application is not accept."); this.application = application; } public void valuebound(javax.servlet.http.httpsessionbindingevent e) { vector activesessions = (vector) application.getattribute("activesessions"); if (activesessions == null) { activesessions = new vector(); } jdbcuser sessionuser = (jdbcuser)e.getsession().getattribute("user"); if (sessionuser != null) { activesessions.add(e.getsession()); } application.setattribute("activesessions",activesessions); } public void valueunbound(javax.servlet.http.httpsessionbindingevent e) { jdbcuser sessionuser = (jdbcuser)e.getsession().getattribute("user"); if (sessionuser == null) { vector activesessions = (vector) application.getattribute("activesessions"); if (activesessions != null) { activesessions.remove(e.getsession().getid()); application.setattribute("activesessions",activesessions); } } } } |
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)); } } |
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%20does%20not%20exist."); return; } catch ( invalidpasswordexception e2) { response.sendredirect("insufficientprivilege.jsp?detail=invalid%20password"); return; } catch ( exception e3) { %> error:<%=e3.tostring() %><br> press <a href="login.jsp">here</a> to relogin. <% return; } response.sendredirect("index.jsp"); %> |
<body bgcolor="#ffffff"> <table cellspacing="0" cellpadding="0" width="100%"> <tr > <td >sessionid </td> <td >user </td> <td >login time </td> <td >last access time </td> </tr> <% vector activesessions = (vector) application.getattribute("activesessions"); if (activesessions == null) { activesessions = new vector(); application.setattribute("activesessions",activesessions); } iterator it = activesessions.iterator(); while (it.hasnext()) { httpsession sess = (httpsession)it.next(); jdbcuser sessionuser = (jdbcuser)sess.getattribute("user"); string userid = (sessionuser!=null)?sessionuser.getuserid():"none"; %> <tr> <td nowrap=''><%= sess.getid() %></td> <td nowrap=''><%= userid %></td> <td nowrap=''> <%= beacondate.getinstance( new java.util.date(sess.getcreationtime())).getdatetimestring()%></td> <td class="<%= stl %>3" nowrap=''> <%= beacondate.getinstance( new java.util.date(sess.getlastaccessedtime())).getdatetimestring()%></td> </tr> <% } %> </table> </body> |
新闻热点
疑难解答