• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using HttpSession in Java Class?

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really need to use a session object outside of Servlet/JSP framework. Is it possible?

No. When there is no servlet container, who is going to create the session?
Or are you asking if you can pass the session from a servlet or JSP to a clas that is "web-unaware"?
And lastly,
but it does not work

is a completely useless statement without an explanation of what's "not working".
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Or are you asking if you can pass the session from a servlet or JSP to a clas that is "web-unaware"?

Yes.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic