Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Get session object from session ID Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Get session object from session ID" Watch "Get session object from session ID" New topic
Author

Get session object from session ID

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

Hi all,

I need to extract the session object from a session id , i googled out this and find this link , but its not useful ..

Is anybody have solution for it ??

Thanks in advance !!


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There are methods for doing this but they're deprecated for security reasons.

Now that we have session listeners, it's easy to create a context scoped map that holds references to sessions (using the session id as the key).
Once this is set up, it's easy to do what you want.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

Hi Ben

Do you want me to do something like this code :


but, this code is not working , i mean the session is not get stored in map, when i create the session like this


Whats the cause ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How do you know it's not being stored in your map?

Here is the code I use.
You can download the whole project at:
http://simple.souther.us/not-so-simple.html
Look for SessionMonitor.




As you can see, I stored the map in context scope.
I created it in a contextListener to insure that it was in place before any requests could be handled.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

Ben, I declared map (with static type) in SessionListener class, with 2 static method like getHttpSession() and setHttpSession().

and this two methods working fine for me !!
The only problem is they are not context scoped !, then whats scope they(SessionListener class) have ?
[ June 21, 2008: Message edited by: Sagar Rohankar ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

First, I assume you renamed sessionObjectMap to map or vice versa but didn't update the code posted to the ranch. If not, are you working with two different variables? If so, correct this and see if it works.

In webapps, I generally try to avoid using static variables for anything other than constants. Servlet containers rely heavily on custom class loaders so I'm never comfortable that I'm always dealing with the same class though out the application.

Binding your object (session map, in this case) to context scope is a nice, servlet spec compliant way to make that resource available to all the other objects in the application.

Is that something you're willing to try?
Let us know if this work for you.

-Ben
[ June 21, 2008: Message edited by: Ben Souther ]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

First, I assume you renamed sessionObjectMap to map or vice versa but didn't update the code posted to the ranch.


yes Ben ,you are right ! The both objects are same, sorry for not updating the code. .

So as per your great explanation , i remove the static code from my webapps, and put that code in ContextListener class. And thats too working fine , with greater reliability in future !

Thanks for your valuable help !
A Singhal
Greenhorn

Joined: May 30, 2007
Posts: 3
Ben,

This solution worked for me But our porblem is we have clustered environment. Does this takes care of clustered environment?

Thanks.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Singhal",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted.
Vinod Borole
Greenhorn

Joined: Feb 25, 2009
Posts: 26
Thank you Ben, your solution has helped me!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Get session object from session ID
 
Similar Threads
HFSJ Mock Exam
Session ID changing within session
Session handling during logoff
session ID changes
what is url re writing