HttpSessionBindingListener

Java 中对于Http协议的管理接口,如果一个类(对象)实现了该接口,那么当这个对象在被绑定,或者从Session中删除时,Servlet会通知这个对象,当接受到通知之后,则可以进行一些初始化或者清除。
 
1、HttpSessionBindingListener接口介绍
  如果一个对象实现了HttpSessionBindingListener接口,当这个对象被绑定到Session中或者从session中被删除时,Servlet容器会通知这个对象,而这个对象在接收到通知后,可以做一些初始化或清除状态的操作。
  javax.servlet.http.HttpSessionBindingListener接口提供了以下方法:
  public void valueBound(HttpSessionBindingEvent event)
  当对象正在被绑定到Session中,Servlet容器调用这个方法来通知该对象。
  public void valueUnbound(HttpSessionBindingEvent event)
  当从Session中删除对象时,Servlet容器调用这个方法来实现了HttpSessionBindingListener接口的对象,而这个对象 可以利用HttpSessionBindingEvent对象来访问与它相联系的HttpSession对象。Javax.Servlet.http.HttpSessionBindingEvent类提供了以下两种方法。
  public HttpSessionBindingEvent(HttpSession session,java.lang.String name)
  public HttpSessionBindingEvent(HttpSession session,java.lang.string name,java.lang.Object value)
  上面两个构造一个事件对象,当一个对象被绑定到Session中或者从Session中被删除时,用这个事件对象来通知它。
  public java.lang.String getName()
  返回绑定到Session中或者从session中删除的属性的名字。
  public java.lang.Object getValue()
  返回添加、删除或替换的属性的值。如果属性被添加或者被删除,这个方法返回属性的值。如果属性被替换,这个方法返回属性先前的值。
  public HttpSession getSession()
  返回HttpSession对象。
  2、在线人数统计程序:
  利用HttpSessionBindingListener接口,编写一个在线人数统计的等程序。当一个用户登陆时,添加Session到在线人名单中,当一个用户退出时或者Session超时时,从在线人名单中删除该用户。
  在UserList这个类中,应用单件模式,向程序提供一个全局访问点。
import java.util.Vector;
  import java.util.Enumeration;
public class UserList
  {
  private static final UserList userList = new UserList();
  private Vector v = new Vector();
  
  private UserList()
  {
  //v = new Vector();
  }
  public static UserList getInstance()
  {
  return userList;
  }
  //将用户登陆ID保存到Vector中
  public void addUser(Object dlid) throws Exception
  {
  try{
  if ( dlid != null)
  {
  if (v.indexOf(dlid) >= 0)//判断是否已经存在
  return ;
  //可能的操作
  Yhjbxx yh = new Yhjbxx();
  yh.SetYhjbxxDqzt(Integer.parseInt(dlid.toString()),"1");//改写数据库供其它应用读取。
  //添加登录ID
  v.addElement(dlid);
  }
  }
  catch(Exception ex)
  {
  Log.writeDebug(ex.toString());
  }
  finally{
  }
  }
  
  public boolean IsExist(Object dlid)throws Exception
  {
  try{
  if (v.indexOf(dlid) >= 0)
  return true;
  return false;
  }
  catch(Exception ex)
  {
  Log.writeDebug(ex.toString());
  return false;
  }
  }
  
  //删除用户登录ID
  public void RemoveUser(Object dlid)throws Exception
  {
  try{
  if ( dlid != null )
  {
  //修改数据库
  Yhjbxx yh = new Yhjbxx();
  yh.SetYhjbxxDqzt(Integer.parseInt(dlid.toString()),"");
  //移除用户登录ID
  v.removeElement(dlid);
  }
  }
  catch(Exception ex)
  {
  Log.writeDebug(ex.toString());//写日志
  }
  finally{
  }
  }
  //返回Vector枚举
  public Enumeration getUserList()
  {
  return v.elements();
  }
  //返回在线人数
  public int getUserCount()
  {
  return v.size();
  }
  }
User 类实现了HttpSessionBindingListener接口,表示登录用户
  import javax.servlet.http.HttpSessionBindingListener;
  import javax.servlet.http.HttpSessionBindingEvent;
  public class User implements HttpSessionBindingListener
  
{
  //用户登录ID
  private int dlid;
  private UserList u1 = UserList.getInstance();
  public User(int dlid)
  {
  this.dlid = dlid;
  }
  public User()
  {
  }
  public void setdlid(int v)
  {
  this.dlid = v;
  }
  public int getdlid()
  {
  return this.dlid;
  }
  //判断用户是否存在
  public boolean IsExist(int dlid)throws Exception
  {
  try
  {
  Object o = Integer.toString(dlid);
  return u1.IsExist(o);
  }
  catch(Exception ex)
  {
  Log.writeDebug(ex.toString());
  return false;
  }
  }
  
  public void valueBound(HttpSessionBindingEvent event)
  {
  try{
  Object o = Integer.toString(dlid);//(Object)dlid;
  u1.addUser(o);
  }
  catch(Exception ex)
  {
  Log.writeDebug(ex.toString());
  }
  }
  public void valueUnbound(HttpSessionBindingEvent event)
  {
  try{
  Object o = Integer.toString(dlid);
  u1.RemoveUser(o);
  }
  catch(Exception ex)
  {
  Log.writeDebug(ex.toString());
  }
  }
  
}
  
  登录时添加会话:
  User user = new User(y.getid());
  session.setAttribute("user",user);
  
  退出时删除会话:
  User us = (User)session1.getAttribute("user");
  if ( us != null )
  {
  if ( us.IsExist(us.getdlid()))
  session1.invalidate();
  }
  
  退出时删除会话并关闭浏览器Servelt
import javax.servlet.*;
  import java.io.*;
  import javax.servlet.http.*;
public class LogoutServlet extends HttpServlet
  {
  public void doGet(HttpServletRequest req,HttpServletResponse resp)
  throws ServletException,IOException
  {
  resp.setContentType("text/html;charset=gb2312");
  
  HttpSession session = req.getSession();
  User user = (User)session.getAttribute("user");
  session.invalidate();
  PrintWriter out = resp.getWriter();
  StringBuffer strbuffer = new StringBuffer();
  strbuffer.append("<body>");
  strbuffer.append("<script loaguage="javascript">");
  strbuffer.append("var ua=navigator.userAgent;");
  strbuffer.append("var ie=navigator.appName=="Microsoft Internet Explorer"?true:false;");
  strbuffer.append("if(ie){");
  strbuffer.append("var Ieversion=parseFloat(ua.substring(ua.indexOf("MSIE")+5,ua.indexOf(";",ua.indexOf("MSIE "))));");
  strbuffer.append("if(Ieversion< 5.5){");
  strbuffer.append(" var str= '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">';");
  strbuffer.append("str += '<param name="Command" value="Close"></object>';");
  strbuffer.append("document.body.insertAdjacentHTML("beforeEnd", str);");
  strbuffer.append("document.all.noTipClose.Click();");
  strbuffer.append(" }");
  strbuffer.append(" else{");
  strbuffer.append("window.opener = null;");
  strbuffer.append("window.close();");
  strbuffer.append("}");
  strbuffer.append("}");
  strbuffer.append("else {");
  strbuffer.append("window.close();");
  strbuffer.append("}");
  strbuffer.append("</script>");
  strbuffer.append("</body>");
  out.print( strbuffer.toString());
  }
  }
参考资料:JAVAWEB开发详解_XML+XSLT+Servlet+JSP深入开发剖析与实例应用
posted @ 2015-05-06 11:02  萧痕♂泪  阅读(181)  评论(0)    收藏  举报