Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a session variable in stateless session bean

 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about the HttpSession?
 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic