• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get session object from session ID  RSS feed

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ben, your solution has helped me!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!