views:

38

answers:

1

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 pass around the HttpSession object so I can do a request.getSession()? Is there a better way to handle this? Thanks!

+1  A: 

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.

BalusC
Also, note that all the `HttpServlet` `doGet`, `doPost`, etc... methods pass in `HttpServletRequest` and `HttpServletResponse` objects.
R. Bemrose
Ok, so for my next question, what jar is HttpServletRequest a part of? I can't seem to get it to resolve...
Arthurdent510
It's in the library of the servletcontainer you'd like to use. E.g. Apache Tomcat, Sun Glassfish, JBoss AS, etc..etc.. This is however a story apart .. Starters often make the major mistake to copy a different vendor's one into the webapp's `/WEB-INF/lib`. If you're using a simple notepad and javac, then you just need to add the path to the server library to classpath. If you're using an IDE like Eclipse, then you need to have the Eclipse for Java EE version and integrate the servletcontainer in Eclipse and finally associate it with the dynamic web project. Eclipse will take care automagically
BalusC
Ok, today is just one of those days where I can't wrap my head around this... Does anyone have any good code examples?
Arthurdent510
http://courses.coreservlets.com/Course-Materials/csajsp2.html It also covers Eclipse and Tomcat. Don't give up, nobody has learnt Java EE web profile in 1 day. It'll take minimal one month or two of intensive training.
BalusC