How to close a specific session in Java EE

How can I close a specific session if I have multiple sessions open:

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

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

I tried using the following:

  • session.invalidate()but he closes everything.
  • session.removeAttribute("cartList"); he did not close my session.
+3
source share
4 answers

You do not have multiple sessions for each visitor. You have only one session per visitor. You just store attributes in it. "Closing" a session occurs using the method invalidate(). It destroys the entire session and discards all attributes. Any subsequent HTTP request will result in a new session.

, . removeAttribute("name") - . , getAttribute("name") ${name} /. , , -, , , .

. :

+5

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

, , session.


+2

session.invalidate() , , session.removeAttribute("cartList") cartList .

+2

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

session.removeAttribute("cartList");

null "cartList"

+2

All Articles