在 Java 中每当会话即将超时时要求容器通知您的应用程序

发布于 2024-08-07 06:59:40 字数 754 浏览 3 评论 0 原文

每当会话即将超时时,可以使用哪种方法要求容器通知您的应用程序?(选择所有适用的方法)

A. HttpSessionListener.sessionDestroyed — 正确

B. HttpSessionBindingListener.valueBound

C. HttpSessionBindingListener.valueUnbound —正确,这是一种迂回,但如果您有一个属性类,这是一种通知超时的方法

D. HttpSessionBindingEvent.sessionDestroyed - 没有这样的方法

E. HttpSessionAttributeListener.attributeRemoved - 删除属性并不紧密关联带有会话超时

F. HttpSessionActivationListener.sessionWillPassivate -- 会话钝化与超时不同

我同意选项 A.

1) 但 C 是值得怀疑的

value unbound 如何与会话超时紧密耦合。它只是属性获取时的回调方法已删除。

2)如果C是正确的,E也应该是正确的。

HttpSessionAttributeListener 只是一个想要知道会话中何时添加、删除或替换任何类型的属性的类。它由任何类实现。

HttpSessionBindingListener 的存在是为了让属性本身能够发现它何时被添加到会话中或从会话中删除,并且属性类必须实现此接口才能实现它。

任何想法...

Which method(s) can be used to ask the container to notify your application whenever a session is about to timeout?(choose all that apply)

A. HttpSessionListener.sessionDestroyed -- correct

B. HttpSessionBindingListener.valueBound

C. HttpSessionBindingListener.valueUnbound -- correct this is kind of round-about but if you have an attribute class this is a way to be informed of a timeout

D. HttpSessionBindingEvent.sessionDestroyed -- no such method

E. HttpSessionAttributeListener.attributeRemoved -- removing an attribute isn’t tightly associated with a session timeout

F. HttpSessionActivationListener.sessionWillPassivate -- session passivation is different than timeout

I agree with option A.

1) But C is doubtful

How can value unbound be tightly coupled with session timeout.It is just the callback method when an attribute gets removed.

2) and if C is correct, E should also be correct.

HttpSessionAttributeListener is just a class that wants to know when any type of attribute has been added, removed, or replaced in a session. It is implemented by any class.

HttpSessionBindingListener exists so that the attribute itself can find out when it has been added to or removed from a session and the attribute class must implement this interface to achieve it.

Any ideas…

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-08-14 06:59:40

我对此问题的解决方案是使用 A。请记住,HttpSessionListener.sessionDestroyed() 在会话失效之前被调用,因此其所有属性都完好无损。

我们的应用程序使用带有键“LOGOUT”的会话属性来表示显式注销。因此,您的代码可能看起来像这样:

public void sessionDestroyed(HttpSessionEvent se) {
    Object logout = se.getSession().getAttribute("LOGOUT");
    String username = (String)se.getSession().getAttribute("USERNAME");
    auditLog.info("User '" + username + "' " +
                 (logout == null ? "timed" : "logged") +
                 " out.");
}

据我所知,没有标准方法可以从 HttpSession 判断它为何即将失效,并且令人惊讶的是,< code>HttpSessionEvent 对象,描述事件是什么(直到 Java EE 6)。

My solution to this problem is to use A. Remember that HttpSessionListener.sessionDestroyed() is called before the session is invalidated so all its attributes are intact.

Our application uses a session attribute with key "LOGOUT" to signify an explicit logout. So your code may look something like this:

public void sessionDestroyed(HttpSessionEvent se) {
    Object logout = se.getSession().getAttribute("LOGOUT");
    String username = (String)se.getSession().getAttribute("USERNAME");
    auditLog.info("User '" + username + "' " +
                 (logout == null ? "timed" : "logged") +
                 " out.");
}

As far as I can tell there is no standard way to tell from the HttpSession why it is about to be invalidated and, amazingly, there is no information in the HttpSessionEvent object that describes what the event was (this is true up to Java EE 6).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文