File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How to store info in  a session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to store info in  a session" Watch "How to store info in  a session" New topic

How to store info in a session

anil gowda

Joined: Jun 12, 2007
Posts: 12
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,
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

HttpSession has two methods

Refer API documentation for more details

Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />
rambabu maripi

Joined: Jun 30, 2007
Posts: 1
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 is an object of type HttpServletRequest
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
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

Joined: Nov 29, 2005
Posts: 2308
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 ?

Rahul Bhattacharjee
LinkedIn - Blog
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

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.

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
anil gowda

Joined: Jun 12, 2007
Posts: 12
Hi all,

Thanks for your replies, Actually i just gave a scenario i,e storing user id and password in session 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.

I agree. Here's the link:
subject: How to store info in a session
It's not a secret anymore!