HttpSessionBindingListener

Java 中對於Http協定的管理接口,如果一個類(對象)實現了該接口,那么當這個對象在被綁定,或者從Session中刪除時,Servlet會通知這個對象,當接受到通知之後,則可以進行一些初始化或者清除。 如果一個對象實現了HttpSessionBindingListener接口,當這個對象被綁定到Session中或者從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 implementsHttpSessionBindingListener
{
//用戶登錄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深入開發剖析與實例套用

相關詞條

熱門詞條

聯絡我們