2014-11-24 18 views
0

我需要在用戶會話過期時更改ServletContext中的屬性。從valueUnbound(HttpSessionBindingEvent事件)方法修改ServletContext的屬性

這裏的建議how-to-call-a-method-before-the-session-object-is-destroyed,我在我的java類中實現了方法valueUnbound(HttpSessionBindingEvent事件),它允許在使用事件引用銷燬它之前訪問Session對象。

在這個方法裏面,我需要改變一個屬性在ServletContext中的值。我能怎麼做?

public class myClass implements HttpSessionBindingListener { 

@Override 
public void valueUnbound(HttpSessionBindingEvent event) { 
    int userid = Integer.valueOf((Integer) event.getSession().getAttribute("IDplayer")); 
    boolean[] id_used = (boolean[]) getServletContext().getAttribute("id_used"); 
} 

的問題是,getServletContext()方法。的getAttribute()不成立,另外,如果我列入 「進口的javax.servlet。*」。

如何從會話關閉前調用的方法訪問ServletContext屬性?

回答

1

通過HttpSessionBindingEvent訪問它。 使用此:

event.getSession().getServletContext().getAttribute("id_used"); 
1

試試這個代碼

session = event.getSession(); 
contextObj = session.getServletContext();