6
オブジェクトがHTTPのセッションオブジェクトにバインド/アンバインドされたときに通知を受け取る方法を教えてください。HTTPセッションにバインド/アンバウンドされたときの通知の取得
オブジェクトがHTTPのセッションオブジェクトにバインド/アンバインドされたときに通知を受け取る方法を教えてください。HTTPセッションにバインド/アンバウンドされたときの通知の取得
オブジェクトのクラスに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
を交換してください。
助けてくれてありがとう。これは私が探していたものです:) – ramoh