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:
- Best option for session management in Java
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
精彩评论