users@glassfish.java.net

Re: Identifying unique user

From: <glassfish_at_javadesktop.org>
Date: Wed, 23 Apr 2008 05:25:55 PDT

Your posting leaves some questions open. Do you need help in generating a unique identifier or how to pass this value around between subsequent requests and so on.

Now I assume your app is using a database. If so you should let the database generate the unique identifier (e.g. a sequence value) for the users of your app. Now whenever one user registers, you create a user and use the generated value for the primary key as your unique identifier. Using JPA might ease this for you.

When a user logs in later on you could put the object representing this user into the session and use this object for the duration of the session. You do not even have a need to transfer user id back and forth between subsequent requests.

The way to store anything in the session is to use

session.setAttribute("user", userObject);

Here "user" is the key you have to use whenever you want to retrieve this object. And the second parameter can take any Object. In your case this would be the user object.

Retrieval is done like this:

User currentUser = (User)session.getAttribute("user);

Since you can store and retrieve arbitrary objects you have to cast the retrieved object to the actual type you need.

I hope this helps. For any follow up posts consider to state your problem in more detail.

--
Wolfram Rittmeyer
[Message sent by forum member 'writtmeyer' (writtmeyer)]
http://forums.java.net/jive/thread.jspa?messageID=270692