This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store info in a session

 
anil gowda
Greenhorn
Posts: 12
  • 0
  • 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
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • 0
  • 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/
 
rambabu maripi
Greenhorn
Posts: 1
  • 0
  • 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
 
Jason Kwok
Ranch Hand
Posts: 126
  • 0
  • 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?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • 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 ?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • 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
  • 0
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic