wood burning stoves*
The moose likes EJB and other Java EE Technologies and the fly likes Getting a session variable in stateless session bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Getting a session variable in stateless session bean" Watch "Getting a session variable in stateless session bean" New topic
Author

Getting a session variable in stateless session bean

vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Is it possible to get a session variable in stateless session bean.

The control flow is from jsp to the ejb, so there is no request going to the ejb.

So how can I get a session value in the ejb, or what are other ways that I can do it.

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Are you talking about the HttpSession?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Yes, I was thinking about that only, but is there any other way of bringing the session variable to the bean.

I can use the parameter passing from the jsp to the bean, but if I change the method declaration, then I need to change in many places.

So if there is any other way of doing this, please let me know.

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

EJBs have no access to the HttpSession. You'll have to pass whatever parameters you need via method calls to your EJBs.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

It is not a good practice to pass httpsession to the EJB tier. The EJB tier code gets coupled to the Web tier making the EJB less resusable in non- web environment. Why not pass a HashMap ?


Groovy
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Its very common for applications to pass something like an ApplicationContext to every EJB method that is exposed to the client. This ApplicationContext interface might have an Map which contains all the necessary information that the client needs to pass to the EJB. The advantage of this approach is that if some day you need to pass an extra information to the EJB method, you dont have to change the method signature, you can just pass it as part of the ApplicationContext. Your method would look like:


[ August 16, 2006: Message edited by: jaikiran pai ]

[My Blog] [JavaRanch Journal]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

...or pass some other serializable object that encapsulates all the values from the session this particular EJB method needs (such as a DTO/VO).
vjy chin
Ranch Hand

Joined: Feb 17, 2005
Posts: 279
Thanks for all the replies. Actually I can use the variable as a parameter in the methods, but then I need to change the code in many places.

Anyways I did not use a session variable now, just worked out another logic.

Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting a session variable in stateless session bean