开发者

java session id structure from HttpServletRequest

开发者 https://www.devze.com 2023-01-29 12:09 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消