wood burning stoves 2.0*
The moose likes Servlets and the fly likes using HttpSession in Java Class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "using HttpSession in Java Class?" Watch "using HttpSession in Java Class?" New topic
Author

using HttpSession in Java Class?

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

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".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476

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

Joined: Jan 10, 2002
Posts: 60785
    
  65

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

Joined: Jan 24, 2001
Posts: 476

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

Joined: Jan 10, 2002
Posts: 60785
    
  65

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

Joined: Jan 24, 2001
Posts: 476

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
 
jQuery in Action, 2nd edition
 
subject: using HttpSession in Java Class?
 
Similar Threads
Getting "HttpSession" object
Please explain me
session not available in a jsp file?
a statement about Session
Stateful Session bean