您的位置:首页 > 理论基础 > 计算机网络

HttpSessionBindingListener接口介绍

2012-09-17 11:50 323 查看

HttpSessionBindingListener接口介绍



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.Stringname)

public HttpSessionBindingEvent(HttpSession session,java.lang.stringname,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

{

privatestatic final UserList userList = new UserList();

privateVector v = new Vector();

privateUserList()

{

//v = new Vector();

}

publicstatic UserList getInstance()

{

return userList;

}

//将用户登陆ID保存到Vector中

public voidaddUser(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{

}

}

publicboolean IsExist(Object dlid)throws Exception

{

try{

if ( v.indexOf(dlid) >= 0)

returntrue;

return false;

}

catch(Exception ex)

{

Log.writeDebug(ex.toString());

return false;

}

}

//删除用户登录ID

public voidRemoveUser(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枚举

publicEnumeration getUserList()

{

return v.elements();

}

//返回在线人数

public intgetUserCount()

{

return v.size();

}

}

User 类实现了HttpSessionBindingListener接口,表示登录用户

importjavax.servlet.http.HttpSessionBindingListener;

import javax.servlet.http.HttpSessionBindingEvent;

public class User implements HttpSessionBindingListener

{

//用户登录ID

private intdlid;

privateUserList u1 =UserList.getInstance();

publicUser(int dlid)

{

this.dlid = dlid;

}

publicUser()

{

}

public voidsetdlid(int v)

{

this.dlid = v;

}

public intgetdlid()

{

return this.dlid;

}

//判断用户是否存在

publicboolean 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(HttpSessionBindingEventevent)

[b] {

try{

Object o =Integer.toString(dlid);//(Object)dlid;

u1.addUser(o);

}

catch(Exception ex)

{

Log.writeDebug(ex.toString());

}

}

public voidvalueUnbound(HttpSessionBindingEvent event)

{

try{

Object o = Integer.toString(dlid);

u1.RemoveUser(o);

}

catch(Exception ex)

{

Log.writeDebug(ex.toString());

}

}


[/b]}

登录时添加会话:

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 extendsHttpServlet

{

public voiddoGet(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("<scriptloaguage="javascript">");

strbuffer.append("var ua=navigator.userAgent;");

strbuffer.append("var ie=navigator.appName=="Microsoft InternetExplorer"?true:false;");

strbuffer.append("if(ie){");

strbuffer.append("varIeversion=parseFloat(ua.substring(ua.indexOf("MSIE")+5,ua.indexOf(";",ua.indexOf("MSIE"))));");

strbuffer.append("if(Ieversion< 5.5){");

strbuffer.append(" var str ='<object id=noTipCloseclassid="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());

}

}

参考:JAVA WEB 开发详解_XML+XSLT+Servlet+JSP深入开发剖析与实例应用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: