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 Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to store info in a session

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Can anyone tell me how can i store information in a session.?

suppose if i want to store Login detail that is USERID and PASSWORD in
a session how can i do it?


Thanks in advance,
Anil
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpSession has two methods



Refer API documentation for more details

http://java.sun.com/products/servlet/2.3/javadoc/
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai
you can put any number of objects in the session.

The setAttribute("object_name",object) and getAttribute(object) are the two methods to put and retrieve the values from the session respectively.


example request.getSession.setAttribute("userid",userid)
request.getSession.getAttribute("userid")

request is an object of type HttpServletRequest
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a little off topic, but just in response to your original scenario "suppose if i want to store Login detail that is USERID and PASSWORD"...

I just wanted to say that, at least in my opinion, saving passwords to a session isn't a good idea because having the password in the session would serve no purpose after the user has successfully logged in. Just something to consider, perhaps there are scenarios were storing passwords might be useful?
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have a bean that contains information related to session that you might want to use later alone with the username.
Once login is successful you can create a instance of this bean and store it in session.

How will storing password in session help ?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anil,

As Rahul said, its better and efficient to store an Object (container) which is typically a bean that has the required information as its properties. In this case, the number of objects to be dealt with is #1 and not many.

I think you must have simply thought of asking how to store the login related information into a session. While asking the same, you would have mentioned the USERID and PASSWORD! Aint I? If not, then i am also in the queue waiting for the reasons to know the demand of storing the password in session.
 
anil gowda
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanks for your replies, Actually i just gave a scenario i,e storing user id and password in session ...im not going to store the password in session by any means, i'm storing the userid which will be helpful in identifying the particular user among many.

regards,
anil
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!