简体   繁体   中英

java session id structure from HttpServletRequest

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

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM