This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes how to create unique ID in JSP for concurrent users Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to create unique ID in JSP for concurrent users" Watch "how to create unique ID in JSP for concurrent users" New topic
Author

how to create unique ID in JSP for concurrent users

Sue Stephen
Greenhorn

Joined: May 04, 2004
Posts: 11
Hi,
I am developing an application using JSP and Tomcat. Each time a user access a jsp, a new directory specific for this user is created. After some operations, the directory is deleted. I need to create a unique directory name for each user. I am thinking of using an array to hold integer. The value of count then append to directory name. Is this a practical way? Can the array be initialized to larger size in a more convenient way? Aslo, can I use timestamp for each user? Any suggestion is greatly appreciated.

<%! static int[] count = {1,1,1};%>


<%!
public synchronized int getIndex()
{
for (int i=0; i<count.length; i++)
{
if (count[i] != -1)
{
count[i]=-1;
return i;
}
}
return -1;
}

public synchronized void setIndex(int i)
{
count[i]=1;
}
Eugene Lucash
Ranch Hand

Joined: Feb 19, 2005
Posts: 77
You can create directories for each session using jsessionid as unique identifier. If you wish to remove those directories use Session Listener
(when session ends (on destroy event) you delete this directory).
[ February 22, 2005: Message edited by: Bear Bibeault ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Eugene Lucash:
You can create directories for each session using jsessionid as unique identifier. If you wish to remove those directories use Session Listener
(when session ends (on destroy event) you delete this directory).


What do you mean be "jsessionid". If it is the same as session.getId() then I must say it is not reliable. You need to generate one unique Id for every session.

Otherwise, please say something on jsessionid. What's that?

thanks.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

If you use a db in your application, the best way would be to have a sequence that generates consequent values.


If it is the same as session.getId() then I must say it is not reliable. You need to generate one unique Id for every session.


Do you mean to say a session id may be duplicated at some point of time ? (i.e after an id has been used and invalidated, it may pop-up after some time ?)

ram.
Eugene Lucash
Ranch Hand

Joined: Feb 19, 2005
Posts: 77
Yes, session.getId(). Not enouph reliable? For what? Atomic Power Station?
Ok, ok, no jsessionid. We can envent the wheel, hoping that
we are writing more reliable code than others.

Simple design must be simple. Resister class that implements HttpSessionListener in web.xml

public class SessionListener implements HttpSessionListener {

public void sessionCreated(HttpSessionEvent sesev) {
//create some unique code name (myid)
//create user directory using unique id
//save reference to directory in session
}

public void sessionDestroyed(HttpSessionEvent sesev) {
//cleanup user directory using reference stored in session
}
}

You can generate id on you own (perhaps using db).
I think this is roughtly the same as using jsessionid.

Opinions are welcome
Regards
[ February 23, 2005: Message edited by: Eugene Lucash ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
I am thinking of Timestamp as a unique identifier.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Eugene Lucash:
You can generate id on you own (perhaps using db).
I think this is roughtly the same as using jsessionid.


William Brogden said in this thread

"You will automatically get the correct session for each user anyway, using getId() is superfluous. A HttpSession acts like a Hashtable that is automatically managed by the servlet engine."
Eugene Lucash
Ranch Hand

Joined: Feb 19, 2005
Posts: 77
Yeah, as I show earlier, we can make unical directory once, and then
store reference to it in session, and ondestroy dession we cleanup it, so
we can ommit direct use of any id, so we can rely on servlet container to properly maintain sessions
Dan Novik
Ranch Hand

Joined: Jan 26, 2005
Posts: 39
There are several appropriate taglibs in Coldtags suite:

a) create unique id
b) create a temp directory

See http://www.servletsuite.com/jsp.htm
 
jQuery in Action, 2nd edition
 
subject: how to create unique ID in JSP for concurrent users