HttpSessionBindingListener和HttpSessionAttributeListener的实际用法 [英] Practical Usage of HttpSessionBindingListener And HttpSessionAttributeListener
问题描述
我正在通读头JSP和servlet.通过不同类型的侦听器,我遇到了HttpSessionBindingListener
和HttpSessionAttributeListener
.
I am reading through head first JSP and servlets. Going through different type of listeners, I came across HttpSessionBindingListener
and HttpSessionAttributeListener
.
我正在考虑两者之间的区别-我想看看这两个听众在现实世界中的实际用法示例.我通过实现valueBound()
和valueUnBound()
测试了HttpSessionBindingListener
-为什么对象需要知道是否已添加它?
I was thinking about the difference between the two - I want to see the practical usages in real world examples of those two listeners. I tested HttpSessionBindingListener
by implementing valueBound()
and valueUnBound()
- why would an object need to know whether it has been added or not?
我对实际用法很困惑.请帮助澄清这一点.
I am pretty confused about the practical usages. Please help in clarifying this.
推荐答案
HttpSessionBindingListener
将在其实例可能存储在会话中的类(例如,登录用户)上实现.
The HttpSessionBindingListener
is to be implemented on the class whose instances may be stored in the session, such as the logged-in user.
例如
public class ActiveUser implements HttpSessionBindingListener {
@Override
public void valueBound(HttpSessionBindingEvent event) {
logins.add(this);
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
logins.remove(this);
}
}
当HttpSession#setAttribute()
将此ActiveUser
的实例设置为会话属性时,将调用valueBound()
.当它被HttpSession#removeAttribute()
删除或使会话无效或被另一个HttpSession#setAttribute()
替换时,将调用valueUnbound()
.
When an instance of this ActiveUser
get set as a session attribute by HttpSession#setAttribute()
, then the valueBound()
will be invoked. When it get removed by either HttpSession#removeAttribute()
, or an invalidate of the session, or get replaced by another HttpSession#setAttribute()
, then the valueUnbound()
will be invoked.
以下是一些实际的用例:
Here are some real world use cases:
- 从HttpSessionListener获取SessionScoped bean?
- 如何在之前调用方法会话对象被销毁了?
- 关闭JSP时调用操作
- 实施HttpSessionListener
- 如何使用Java访问HTTP会话
- Getting SessionScoped bean from HttpSessionListener?
- How to call a method before the session object is destroyed?
- Call an action when closing a JSP
- implementing HttpSessionListener
- How to access HTTP sessions in Java
HttpSessionAttributeListener
用于可以实现为整个应用程序@WebListener
的实现,当在HttpSession
中添加,删除或替换 any 任何属性时,就会调用该应用程序.继续上面的ActiveUser
示例,如果您不能修改ActiveUser
类以实现HttpSessionBindingListener
(因为它是第3方),或者当您想使用标记接口"时,这特别有用"在任意数量的班级上进行学习,这样您就可以在一个中央位置进行听力工作.
The HttpSessionAttributeListener
is to be implemented as an application wide @WebListener
which get invoked when any attribute is added, removed or replaced in the HttpSession
. Continuing with the above ActiveUser
example, this is particularly useful if you can't modify the ActiveUser
class to implement HttpSessionBindingListener
(because it's 3rd party or so), or when you want to make use of a "marker interface" on an arbitrary amount of classes so that you can do the listening job in a single central place.
@WebListener
public class ActiveUserListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
if (event.getValue() instanceof ActiveUser) {
logins.add(event.getValue());
}
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
if (event.getValue() instanceof ActiveUser) {
logins.remove(event.getValue());
}
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
if (event.getValue() instanceof ActiveUser) {
logins.add(event.getValue());
}
}
}
这是一个真实的用例:
这篇关于HttpSessionBindingListener和HttpSessionAttributeListener的实际用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!