让对象的类实现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.
}
}
Run Code Online (Sandbox Code Playgroud)
如果您无法控制对象的类代码,因此无法更改其代码,则可以选择实现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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:当您仍处于Servlet 2.5或更早版本时,请替换@WebListener为中的<listener>配置条目web.xml.
| 归档时间: |
|
| 查看次数: |
3332 次 |
| 最近记录: |