Storing user specific data in java servlet?
A majority of my work has all been in .net, but this project was required to be done in java. So I have a question on handling session data. In .net, I would just do HttpContext.Session.Current in my classes and I would have access to the session data. But it looks like in java, I need to p开发者_开发知识库ass around the HttpSession object so I can do a request.getSession()? Is there a better way to handle this? Thanks!
You don't need to pass it around. Just get the session by HttpServletRequest#getSession()
and use its setAttribute()
method to store objects in session and getAttribute()
to get them from session.
If you think that you need to pass the whole HttpSession
object around into domain objects, then you're doing things wrong. You should rather get the information of interest out of the session (usually in flavor of a Javabean) and then pass that around instead, or maybe just the HttpServletRequest
instance if the design concerns a front controller.
精彩评论