0

I just wanted to find out how the Java Session Id gets generated. Reason is I want to use this Id as a unique id for session tracking. It would be very easy when I would be able to do so.

Does anybody now a nice like with a description of it?

thx

Dale Athanasias
  • 471
  • 3
  • 16
katamshut
  • 21
  • 1
  • 1
  • 'Reason is I want to use this Id as a unique id for session tracking'. It already does that. 'It would be very easy when I would be able to do so.' Do what? – user207421 Dec 10 '10 at 01:44

3 Answers3

3

Reason is I want to use this Id as a unique id for session tracking. It would be very easy when I would be able to do so.

Bozho already answered the technical part. The functional part as you state, is however not a good approach. If you want to do "session tracking", you basically don't need to do anything special. The servletcontainer alread does the job of session tracking in flavor of HttpSession. You just have to store the object of interest in the session by

session.setAttribute("somename", someObject);

It will be available in the subsequent requests in the same session by

SomeObject someObject = (SomeObject) session.getAttribute("somename");

You don't need to take over the session tracking job from the servletcontainer.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks for this! I now about session attributes. Have you ever used a unique session generator? Do you mind if you provide me a link? – katamshut Dec 09 '10 at 16:40
  • No, I haven't. It simply makes no sense. If you actually want some unique ID generator (not session!), have a look at `java.util.UUID`. – BalusC Dec 09 '10 at 16:44
  • What exactly do you need it for? – BalusC Dec 10 '10 at 02:04
1

If really interested, you can look it up in the Servlet specification. The important point is that it is guarnateed to be unique per a servlet container instance. But if you use two instances, or you restart your container, a session id that have been used before may again be used.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Ok. That means it does not take the exact time of generation and use this value to genearate the id? This would make the id unique. – katamshut Dec 09 '10 at 16:38
  • That wouldn't make it unique across multiple users or multiple cluster elements. There is nothing unique about a session identifier except among the currently active sessions. Your question remains unclear. – user207421 Dec 10 '10 at 01:45
0

I guess @katamshut is looking for random string generator. Commons Lang library can help to generate random string using RandomStringUtils class. Once a string is generated, you can add the same into session object

Ratna Dinakar
  • 1,573
  • 13
  • 16