This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
Hi guys, Is it possible to use HttpSession in a Java Class? I tried to import javax.servlet.http but it does not work. I really need to use a session object outside of Servlet/JSP framework. Is it possible? thanks, Alex
All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Ah, in that case you can just pass the session to the class, but that will make it "web-aware" as you must import the relevant javax.servlet packages. To keep the class web-agnostic, you should only pass the data from the session that the class requires and not the session itself.
Joined: Jan 24, 2001
But that requires to do work of retrieving data from session and checking it on sending part (Servlet/JSP) and I wanted to do this in the Java Class itself. What I need it for is this: I go from page to page choosing some of the same information from DB. Since server is stateless I want to keep this DB information in session object rather than re-selecting it each time page loads (remeber earlier discussion in JSP forum? ). But html code that uses this information is build in "web-unaware" class. HTML generated by the Java class is used in calling JSP page later on. That's why I want to send session object to my class and retrieve data from there, format HTML as I need to and send it back to calling JSP. thanks, Alex [ April 29, 2004: Message edited by: Alex Kravets ] [ April 29, 2004: Message edited by: Alex Kravets ] [ April 29, 2004: Message edited by: Alex Kravets ]
Two thoughts: 1) Since this class is building up HTML, it's obviously meant for use only in a web environment. Making it "web aware" by importing javax.servlet classes isn't going to limit its usefullness. However, I still dislike passing things like the session around so: 2) Encapsulate the data you are storing in the session into a single value object that's easy to pass around with just a single reference. It's just as easy to extract and pass a session attribute as it is the session itself.
Joined: Jan 24, 2001
Encapsulate the data you are storing in the session into a single value object that's easy to pass around with just a single reference. It's just as easy to extract and pass a session attribute as it is the session itself.
But does it suggest retreiving data from session before it's send to an HTML building class? The way I build my JSP-Class relationship is that JSP does not know anything about data that it handles, it just displays it. My component class on the other hand only receives sql from JSP page and builds a ready to use component that is to be sent back to JSP page. But I think this setup backfired on me since I gave to much power to my classes without as you said making them "web-aware". May be better solution would be to rewrite some of the classes to servlets, or a servlet that serves as a middle man between JSP and classes to handle sessions? thanks, Alex