2012-03-22 143 views
0

如何使用httpsessionbindinglistner跟蹤註銷時間?我已經給出了示例代碼並在下面給出,但它不起作用。 RESOURCE IS HERE使用HttpSessionBindingListeners跟蹤註銷時間

package com.tunatore.listeners; 

import javax.servlet.http.HttpSessionBindingEvent; 
import javax.servlet.http.HttpSessionBindingListener; 

/** 
* 
* @author tunatore 
*/ 
public class ObjectWillBeInSession implements HttpSessionBindingListener{ 

private String property1 = null; 
private String property2 =null; 


@Override 
public void valueBound(HttpSessionBindingEvent event) { 
    //code to run when ObjectWillBeInSession object associated with a http session 
} 

@Override 
public void valueUnbound(HttpSessionBindingEvent event) { 
    //code to run when ObjectWillBeInSession object removed from a http session   
    //logging into a database server could be done here 
/** 
    * @return the property1 
    */ 
public String getProperty1() { 
    return property1; 
} 

/** 
    * @param property1 the property1 to set 
    */ 
    public void setProperty1(String property1) { 
    this.property1 = property1; 
} 

/** 
    * @return the property2 
    */ 
public String getProperty2() { 
    return property2; 
    } 

    /** 
    * @param property2 the property2 to set 
    */ 
public void setProperty2(String property2) { 
    this.property2 = property2; 
} 

} 

logout.jsp //這裏我想插入註銷時間到數據庫關閉瀏覽器時或會話超時發生

<% 
     ObjectWillBeInSession owi = new ObjectWillBeInSession(); 
     owi.setProperty1("I am a value for Property1"); 
     owi.setProperty2("I am a value for Property2"); 
     //this will call HttpSessionBindingListener's 
     //valueBound method for the object 
     session.setAttribute("owi", owi); 

     //this will call HttpSessionBindingListener's 
     //valueUnbound method for the object 
     session.removeAttribute("owi"); 
      //INSERT INTO DB.......BUT IT IS NOT WORKING 
    %> 

回答

0

我認爲你必須使用HttpSessionListener,而不是綁定監聽器。以下示例顯示服務器中活動會話的數量。

public class SessionCounter implements javax.servlet.http.HttpSessionListener{ 
    /** 
    * Number of active sessions 
    */ 
    private static int activeSessions = 0; 

    public void sessionCreated(javax.servlet.http.HttpSessionEvent se) { 
     activeSessions++; 
     logSessionCount(); 
    } 

    public void sessionDestroyed(javax.servlet.http.HttpSessionEvent se) { 
     if (activeSessions > 0){ 
      activeSessions--; 
     } 
     logSessionCount(); 
    } 
    private void logSessionCount(){ 
     java.lang.System.out.println("Number of active sessions : " + activeSessions); 
    } 
    public static int getActiveSessions() { 
     return activeSessions; 
    } 
} 
+0

yikes!感謝Ravindra,但是logout.jsp有沒有代碼錯誤? – Tom 2012-03-22 09:44:47

+0

不會。註銷時不會有任何錯誤。 – 2012-03-22 09:46:48

+0

好吧,但[這裏](http://stackoverflow.com/questions/9425476/lock-logged-in-user-in-oracle-by-java)Jigar通過使用Binding Listener解釋 – Tom 2012-03-22 09:54:51