| 组件 | 功能 |
| Com.guipei.listener. SessionListener | 监听组件,完成HttpSessionContext的功能 |
| index.jsp | 实现用户登陆,创建新的session |
| logout.jsp | 实现用户退出,用户自动删除session |
| display.jsp | 显示用户登陆信息,在用户登陆后自动转入 |
| session.jsp | 列出当前所有的session |
| kill.jsp | 杀掉指定的会话,使这个用户连接无效 |
监听类com.guipei.listener.SessionListener 实现web application的监听功能,它实现了HttpSessionListener接口,可以监听sessionCreated(HttpSessionEvent se)和sessionDestroyed(HttpSessionEvent se) 方法,因此我们可以很容易的在session的创建和销毁事件过程中处理session的控制。
在此类中,我们创建一个静态实例变量Hashtable ht,采用Hashtable的一个好处是它是线程安全的集合类,无须我们再多做线程处理。采用这个collection类保存我们所要控制的session对象。在监听事件中容易的处理相关任务。
参看全部代码:
|
package com.guipei.listener;
import java.util.Hashtable; import java.util.Iterator;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener;
public class SessionListener implements HttpSessionListener {
// 集合对象,保存session 对象的引用 static Hashtable ht = new Hashtable();
// 实现HttpSessionListener接口,完成session创建事件控制 public void sessionCreated(HttpSessionEvent arg0) { HttpSession session = arg0.getSession(); ht.put(session.getId(), session ); System.out.println("create session :" + session.getId() ); }
// 实现HttpSessionListener接口,完成session销毁事件控制 public void sessionDestroyed(HttpSessionEvent arg0) { HttpSession session = arg0.getSession(); System.out.println("destory session :" + session.getId() ); ht.remove(session.getId()); }
// 返回全部session对象集合 static public Iterator getSet( ){ return ht.values().iterator(); }
// 依据session id返回指定的session对象 static public HttpSession getSession(String sessionId){ return (HttpSession)ht.get(sessionId); } }
|
页面index.jsp 处理用户登陆,创建新的会话的功能。在完成验证后,跳转到display.jsp页面上。
| <%@ page contentType="text/html; charset=gb2312" %> <!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. --> <% String strName = null; String strThing = null; try { strName = request.getParameter("name"); strThing = request.getParameter("thing"); if ((strName == null) || (strName.length()==0)){ throw new Exception("null strName"); } if ((strThing == null) || (strThing.length()==0)) throw new Exception("null strThing");
// add session session.setAttribute("name",strName); session.setAttribute("thing",strThing); response.sendRedirect("display.jsp");
} catch (Exception e) { } %> <html> <head> <title>Welcome</title> </head> <body> <center>Welcome</center>
<form method='post' > <table align='center'> <tr> <td>name:</td> <td> <input name='name' type='input'/> </td> </tr> <tr> <td>thing:</td> <td> <input name='thing' type='input'/> </td> </tr> <tr> <td align='right'> </td> <td align='right'> <button type='submit'>submit</button> <button type='reset'>reset</button> </td> </tr> </table> </form> </body> </html> |
页面display.jsp用于用户登陆后的显示功能,如果用户没有进行过登陆请求,会自动转发到index.jsp页面,保证用户登陆。
| <%@ page language="java" pageEncoding="GB2312" %> <!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>Lomboz JSP</title> </head> <body bgcolor="#FFFFFF">
<%
if (session.isNew()==true){ response.sendRedirect("index.jsp"); } out.println("name: "+ session.getAttribute("name") + "<br>"); out.println("thing: "+ session.getAttribute("thing") + "<br>"); out.println("session id: " + session.getId() + "<br>"); out.println("create time: " + session.getCreationTime() );
%>
</body> </html> |
页面logout.jsp用于用户退出登陆,采用主动方式销毁session。
| <%@ page language="java" pageEncoding="GB2312" %> <!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>Lomboz JSP</title> </head> <body bgcolor="#FFFFFF"> <% if(session.isNew()!=true){ session.invalidate(); } response.sendRedirect("index.jsp"); %> </body> </html> |
页面session.jsp列出当前会话用户,并提供一个连接到kill.jsp,可以用作销毁指定的会话操作。
| <%@ page language="java" pageEncoding="GB2312" %> <%@ page import= 'com.guipei.listener.*,java.util.*'%> <!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>Lomboz JSP</title> </head> <body bgcolor="#FFFFFF">
List session object <br> <table border='1'> <tr bgcolor='yellow'> <td>session id</td> <td>user name </td> <td>what thing </td> <td>create time </td> <td>operate</td> </tr>
<% Iterator iterator = SessionListener.getSet(); while(iterator.hasNext()){ try{ HttpSession session1 = (HttpSession)iterator.next(); out.println("<tr>"); out.println("<td>" + session1.getId() + "</td>" ); out.println("<td>" + session1.getAttribute("name") + "</td>" ); out.println("<td>" + session1.getAttribute("thing") + "</td>" ); out.println("<td>" + session1.getCreationTime() + "</td>" ); out.println("<td> <a href='kill.jsp?sessionid=" + session1.getId() + "'>kill </a> </td>" );
out.println("</tr>"); System.out.println("list " + session1.getId()); }catch(Exception ex){ ex.printStackTrace(); return; } } %> </table> </body> </html> |
页面kill.jsp实现销毁指定会话的功能,接收一个session id参数,从我们保存的session对象集合中取得对应的session对象,调用invalidate方法,销毁对象。
| <%@ page language="java" pageEncoding="GB2312" %> <%@ page import="com.guipei.listener.*"%> <!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>Lomboz JSP</title> </head> <body bgcolor="#FFFFFF"> <% // kill the session try { String strSid = request.getParameter("sessionid"); HttpSession session1 = SessionListener.getSession(strSid); if (session1!=null){ session1.invalidate(); } } catch (Exception e) { e.printStackTrace(); } response.sendRedirect("session.jsp"); %>
</body> </html> |
完成以上代码后,还需要在web.xml描述中添加以下元素。使得SessionListener类可以发挥监听功能。
<listener>
<listener-class>
com.guipei.listener.SessionListener
</listener-class>
</listener>

被折叠的 条评论
为什么被折叠?



