• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access a session scope bean in another bean

 
Samantha Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a session scope java bean, and another java bean. Each one is in a separate package. I would like to use the property of the session scope bean in the other bean. Is this possible? How do I do this?

I see that in other functions in this non-session-scope bean, (this bean works fine, so far - I don't want to mess it up!) other bean's methods are called. But I don't see any reference to variables in different beans.

Hope someone can help...

Samantha
 
Jinal P Shah
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Samantha,
Can you elaborate on what exactly you want to do?


Regards
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be best not to directly address variables of another class from a certain class. If you want to change state, use setter and getter methods.
If I understand you correctly, it would be a good idea to inject the value of the property of the session-scope bean, into the non-session-scope bean in the servlet, and then the two beans would require no further interactions.
 
Samantha Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the responses!

I'm working on a pretty old website. It's built with JSP pages and .java classes. No JSTL/EL involved here... old stuff. The Java code and the HTML code happen on the same JSP pages...

If the session object is called myObject, and the other, non-session-scope object is called myOtherObject, what I need to do is:
Get the attribute myObject.numProds in myOtherObject.compareNumProds() method, so as to do a comparison in that method like this: if(num < myObject.numProds) {...}.

That's the idea.

I understand from what Dieter said, that it would be best not to try to directly access the variable myObject.numProds in myOtherObject.compareNumProds(). But you also said that using getter and setter methods would be OK.

Can I say, in myOtherObject.compareNumProds() and assuming "num" has a value:



Would that work? Remember - the two objects are in different packages...

Thanks so much!

Samantha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic