1、HttpSessionBindingListener接口介紹
如果一個(gè)對(duì)象實(shí)現(xiàn)了HttpSessionBindingListener接口,當(dāng)這個(gè)對(duì)象被綁定到Session中或者從session中被刪除時(shí),Servlet容器會(huì)通知這個(gè)對(duì)象,而這個(gè)對(duì)象在接收到通知后,可以做一些初始化或清除狀態(tài)的操作。
javax.servlet.http.HttpSessionBindingListener接口提供了以下方法:
public void valueBound(HttpSessionBindingEvent event)
當(dāng)對(duì)象正在被綁定到Session中,Servlet容器調(diào)用這個(gè)方法來通知該對(duì)象。
public void valueUnbound(HttpSessionBindingEvent event)
當(dāng)從Session中刪除對(duì)象時(shí),Servlet容器調(diào)用這個(gè)方法來實(shí)現(xiàn)了HttpSessionBindingListener接口的對(duì)象,而這個(gè)對(duì)象可以利用HttpSessionBindingEvent對(duì)象來訪問與它相聯(lián)系的HttpSession對(duì)象。 Javax.Servlet.http.HttpSessionBindingEvent類提供了以下兩種方法。
public HttpSessionBindingEvent(HttpSession session,java.lang.String name)
public HttpSessionBindingEvent(HttpSession session,java.lang.string name,java.lang.Object value)
上面兩個(gè)構(gòu)造一個(gè)事件對(duì)象,當(dāng)一個(gè)對(duì)象被綁定到Session中或者從Session中被刪除時(shí),用這個(gè)事件對(duì)象來通知它。
public java.lang.String getName()
返回綁定到Session中或者從session中刪除的屬性的名字。
public java.lang.Object getValue()
返回添加、刪除或替換的屬性的值。如果屬性被添加或者被刪除,這個(gè)方法返回屬性的值。如果屬性被替換,這個(gè)方法返回屬性先前的值。
public HttpSession getSession()
返回HttpSession對(duì)象。
2、在線人數(shù)統(tǒng)計(jì)程序:
利用HttpSessionBindingListener接口,編寫一個(gè)在線人數(shù)統(tǒng)計(jì)的等程序。當(dāng)一個(gè)用戶登陸時(shí),添加Session到在線人名單中,當(dāng)一個(gè)用戶退出時(shí)或者Session超時(shí)時(shí),從在線人名單中刪除該用戶。
在UserList這個(gè)類中,應(yīng)用單件模式,向程序提供一個(gè)全局訪問點(diǎn)。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)//判斷是否已經(jīng)存在
return ;
//可能的操作
Yhjbxx yh = new Yhjbxx();
yh.SetYhjbxxDqzt(Integer.parseInt(dlid.toString()),"1");//改寫數(shù)據(jù)庫供其它應(yīng)用讀取。
//添加登錄ID
v.addElement(dlid);
}
}
catch(Exception ex)
{
Log.writeDebug(ex.toString());
}
finally{
}
}
/**
* 判斷是否存在會(huì)話
*/
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 )
{
//修改數(shù)據(jù)庫
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();
}
//返回在線人數(shù)
public int getUserCount()
{
return v.size();
}
}User 類實(shí)現(xiàn)了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());
}
}
}
登錄時(shí)添加會(huì)話:
User user = new User(y.getid());
session.setAttribute("user",user);
退出時(shí)刪除會(huì)話:
User us = (User)session1.getAttribute("user");
if ( us != null )
{
if ( us.IsExist(us.getdlid()))
session1.invalidate();
}
退出時(shí)刪除會(huì)話并關(guān)閉瀏覽器Serveltimport 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());
}
}
參考:JAVA WEB 開發(fā)詳解_XML+XSLT+Servlet+JSP深入開發(fā)剖析與實(shí)例應(yīng)用
聯(lián)系客服