2012-06-29 14 views

答えて

7

オブジェクトのクラスにHttpSessionBindingListenerを実装させます。

public class YourObject implements HttpSessionBindingListener { 

    @Override 
    public void valueBound(HttpSessionBindingEvent event) { 
     // The current instance has been bound to the HttpSession. 
    } 

    @Override 
    public void valueUnbound(HttpSessionBindingEvent event) { 
     // The current instance has been unbound from the HttpSession. 
    } 

} 

あなたは、オブジェクトのクラスコードを制御することはできませんので、あなたがそのコードを変更することができない場合は、代替HttpSessionAttributeListenerを実装することです。

@WebListener 
public class YourObjectSessionAttributeListener implements HttpSessionAttributeListener { 

    @Override 
    public void attributeAdded(HttpSessionBindingEvent event) { 
     if (event.getValue() instanceof YourObject) { 
      // An instance of YourObject has been bound to the session. 
     } 
    } 

    @Override 
    public void attributeRemoved(HttpSessionBindingEvent event) { 
     if (event.getValue() instanceof YourObject) { 
      // An instance of YourObject has been unbound from the session. 
     } 
    } 

    @Override 
    public void attributeReplaced(HttpSessionBindingEvent event) { 
     if (event.getValue() instanceof YourObject) { 
      // An instance of YourObject has been replaced in the session. 
     } 
    } 

} 

注意:サーブレット2.5またはそれ以前に残っているときに、web.xml<listener>構成エントリで@WebListenerを交換してください。

+0

助けてくれてありがとう。これは私が探していたものです:) – ramoh

関連する問題