gpt4 book ai didi

session - 当绑定(bind)/未绑定(bind)到 HTTP session 时获取通知

转载 作者:行者123 更新时间:2023-12-02 14:08:13 25 4
gpt4 key购买 nike

当我的对象绑定(bind)/未绑定(bind)到 HTTP session 对象时,我如何收到通知。

最佳答案

让对象的类实现 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.
}
}

}

注意:当您仍在使用 Servlet 2.5 或更早版本时,请替换 @WebListener通过<listener> web.xml中的配置条目.

关于session - 当绑定(bind)/未绑定(bind)到 HTTP session 时获取通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11258724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com