Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain an HttpSession Object from SessionID?

I want to invalidate sessions of users based on some Event. I store their sessionID, how to get their HttpSession from this ID? The HttpSessionContext class is deprecated with no replacement.

like image 883
Bahaa Zaid Avatar asked Aug 09 '09 13:08

Bahaa Zaid


People also ask

How do I get the HttpSession object?

How to get the HttpSession object ? The HttpServletRequest interface provides two methods to get the object of HttpSession: public HttpSession getSession():Returns the current session associated with this request, or if the request does not have a session, creates one.

Where is HttpSession stored?

httpSession contains the properties, and is stored on the Web client as a cookie.

Which object of HttpSession can be used?

Object of HttpSession used to view & manipulate information about session - Servlets.


2 Answers

The methods that are already doing this are deprecated. But you can implement the your idea useing HttpSessionListener, when a new session is created store it in a HashMap with session id as a key and object is actual HttpSessionObject. You can get the reference by this.

like image 171
Niger Avatar answered Sep 28 '22 14:09

Niger


Servlet 2.2 specifically deprecated this for security reasons so there shouldn't be any official way to do this. Not recommended but you can can try to use Manager.findSession() if you use Tomcat.

I just removed HttpSession from my application. It's really hard to keep sessions in sync when many servers are running. We tried to tweak it by writing our own manager but can never get it work right. Finally, we wrote our own session implementation with about 500-lines of code and it works much better.

like image 23
ZZ Coder Avatar answered Sep 28 '22 14:09

ZZ Coder