如何关闭 Java EE 中的特定会话
如果我打开了多个会话,如何关闭特定会话,如下所示:
String userName = (String) session.getAttribute("userName");
HashMap cartList = (HashMap) session.getAttribute("cartList");
如果我想关闭 cartList 的会话,我应该使用什么代码?
我尝试使用以下内容:
session.invalidate()
但它关闭了所有内容。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:
session.invalidate()
but it closes everything.session.removeAttribute("cartList");
it didn't close my session.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有为每个访问者打开多个会话。每位访客只能进行一次会话。您只是将属性存储在其中。 “关闭”会话是通过
invalidate()
方法进行的。它会破坏整个会话并解除所有属性的绑定。任何下一个 HTTP 请求都将产生一个全新的会话。您似乎只是想解除购物车的绑定。
removeAttribute("name")
方法是正确的做法。它将从会话中删除该属性,以便在当前响应和所有后续请求中不再可以通过getAttribute("name")
或${name}
访问该属性/回应。它显然不起作用可能只是您的误解。另请参阅:
ServletContext
/HttpSession
/HttpServletRequest
/HttpServletResponse
工作吗?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 bygetAttribute("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:
ServletContext
/HttpSession
/HttpServletRequest
/HttpServletResponse
work?您并不是通过
此创建会话,您只是从
session
引用的会话中读取属性。参见
By
You aren't creating session by this, you are just reading attributes from the session referred by
session
.See
session.invalidate()
将解除绑定到它的所有对象,而session.removeAttribute("cartList")
将从会话中解除 cartList 对象的绑定。session.invalidate()
will unbind all the objects which were bound to it whilesession.removeAttribute("cartList")
will unbind the cartList object from the session.访问
尝试在
session.removeAttribute("cartList");
之后,它将在“cartList”中为您提供 null
Try accessing
after
session.removeAttribute("cartList");
It will give you null in "cartList"