如何关闭 Java EE 中的特定会话

发布于 2024-10-24 01:16:21 字数 378 浏览 2 评论 0原文

如果我打开了多个会话,如何关闭特定会话,如下所示:

String userName = (String) session.getAttribute("userName");
HashMap cartList = (HashMap) session.getAttribute("cartList");

如果我想关闭 cartList 的会话,我应该使用什么代码?

我尝试使用以下内容:

  1. session.invalidate() 但它关闭了所有内容。
  2. session.removeAttribute("cartList"); 它没有关闭我的会话。

How can I close a specific session if i have several session open as follows:

String userName = (String) session.getAttribute("userName");
HashMap cartList = (HashMap) session.getAttribute("cartList");

If i want to close the session of cartList, what code should i use?

I tried using the following:

  1. session.invalidate() but it closes everything.
  2. session.removeAttribute("cartList"); it didn't close my session.

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

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

发布评论

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

评论(4

短叹 2024-10-31 01:16:21

您没有为每个访问者打开多个会话。每位访客只能进行一次会话。您只是将属性存储在其中。 “关闭”会话是通过 invalidate() 方法进行的。它会破坏整个会话并解除所有属性的绑定。任何下一个 HTTP 请求都将产生一个全新的会话。

您似乎只是想解除购物车的绑定。 removeAttribute("name") 方法是正确的做法。它将从会话中删除该属性,以便在当前响应和所有后续请求中不再可以通过 getAttribute("name")${name} 访问该属性/回应。它显然不起作用可能只是您的误解。

另请参阅:

You don't have several sessions open per visitor. You have only one session per visitor. You are just storing attributes in it. "Closing" a session happens by invalidate() method. It destroys the entire session and unbinds all of the attributes. Any next HTTP request will result in a fresh new session.

You seem to just want to unbind the shopping cart. The removeAttribute("name") method is the right thing to do so. It will remove the attribute from the session, so that it's not accessible by getAttribute("name") or ${name} anymore in the current response and all subsequent requests/responses. That it apparently didn't work is likely just misconception from your side.

See also:

つ低調成傷 2024-10-31 01:16:21

您并不是通过

String userName = (String) session.getAttribute("userName");
HashMap cartList = (HashMap) session.getAttribute("cartList");

此创建会话,您只是从 session 引用的会话中读取属性。


参见

By

String userName = (String) session.getAttribute("userName");
HashMap cartList = (HashMap) session.getAttribute("cartList");

You aren't creating session by this, you are just reading attributes from the session referred by session.


See

许你一世情深 2024-10-31 01:16:21

session.invalidate() 将解除绑定到它的所有对象,而 session.removeAttribute("cartList") 将从会话中解除 cartList 对象的绑定。

session.invalidate() will unbind all the objects which were bound to it while session.removeAttribute("cartList") will unbind the cartList object from the session.

み零 2024-10-31 01:16:21

访问

HashMap cartList = (HashMap) session.getAttribute("cartList");

尝试在 session.removeAttribute("cartList"); 之后

,它将在“cartList”中为您提供 null

Try accessing

HashMap cartList = (HashMap) session.getAttribute("cartList");

after session.removeAttribute("cartList");

It will give you null in "cartList"

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