Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to create unique ID in JSP for concurrent users

 
Sue Stephen
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am thinking of Timestamp as a unique identifier.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several appropriate taglibs in Coldtags suite:

a) create unique id
b) create a temp directory

See http://www.servletsuite.com/jsp.htm
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic