Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

sessions  RSS feed

 
trinadh reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iwant to store some value in sessions.
can you send me sample code for that
 
Debashree Halder
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
session.setAttribute("SomeValue",10);

you can get afterwards by calling...

session.getAttribute("SomeValue");
 
Scheepers de Bruin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately session.setAttribute("something", 10) won't work. setAttribute takes arguments (String, Object) and int is not an object.

Say you want to store a user object in the session:

User user = new User("myusername", myPassword);

request.getSession().setAttribute("whateverIwant", user);

And to retrieve it from the session:

User user = (User)request.getSession().getAttribute("whateverIwant");
[ September 28, 2005: Message edited by: Scheepers de Bruin ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scheepers de Bruin:
Unfortunately session.setAttribute("something", 10) won't work. setAttribute takes arguments (String, Object) and int is not an object.


Cent percent agreement.

But anyone reading this should not take the impression that we can't store numbers into session. We can use wrapper instead like...

 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't it work in Java 5 ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!