• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Needed -- how do I store and retrieve a session variable?

 
Mike London
Ranch Hand
Posts: 1286
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to keep a call in a JSP page to a bean from only happening once in a user's session.
Trying to do things in the JSP page like session.setProperty("someproperty", "value"); and then later session.getProperty("someproperty", "value");
However, everytime I refresh the page (or go to another page in the site and come back to the JSP page with the above session logic), the value of "someproperty" is always Null. It's not saved in the session.
Am I confused about what a session is or am I using these methods incorrectly?
I'm trying to set/retrieve a session variable as a way to only run code in a JSP page once each session.
Any advice on a better (or correct <s> way to do what I am trying to do would be greatly appreciated.
Thanks.
-- Mike
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike
You should be able to use the session.getAttribute() and session.setAttribute() methods to do what you are atttempting to acheive.
If these methods continue to return null it might be worth checking that you are actually looking at the same session between requests by checking the id of the session using session.getId(). It could be that the application server is not able to find the session id on the request (either in a cookie or in an attribute on the request) and so is creating a new session for each incoming request.
Hope this helps
Andy
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1st thing -- hopefully you're using session.getAttribute( "someName" ); and session.setAttribute( "someName", someObject); rather than get/setProperty.... but anyway --
My guess is that your session isn't being persisted across requests -- maybe your browser isn't using cookies?? on the top of your JSP page print out the value of session.getId() -- are the Id's different? if so -- that's why you're null for your attribute -- the session isn't being carried from one page to the next.

[ darn... beat by 2 minutes... I should not multi-task so much.... ]
[ April 30, 2003: Message edited by: Jessica Sant ]
 
Mike London
Ranch Hand
Posts: 1286
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the great replies!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!