Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get same bean from Servlet When every request is new one...  RSS feed

 
azhar jodatti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii Guys...


i have little problem with ejb.. i am not getting how to get same bean if i used servlet as my client...

i need to lookup for the bean in every servlet..... if i do then that bean will be new one and dont want that....


if i wont look for the bean in every servlet then from where i will get the existting bean.....

[color=red]


thanks in advance.....[/color]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using a stateful bean?
 
Manuel Alberto Quero
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the point! Even if you perform the lookup only once, every time you use the bean (every method call) may be calling a different bean instance.

So, in order to get the same bean every time you call it from the server you need to use Stateful session beans and also you need to cache the reference after you perform the lookup. Your code should look something like:

if (ejb == null) {
//perform lookup the first execution
Context ctx = new InitialContext();
ejb2 = (ExampleBean)ctx.lookup("EnterpriseApp/ExampleBean");
}
else {
//already obtained
}

Another possibility is (if your servlet is running on the same JVM as your bean) to use Dependency Injection:

public class ExampleServlet extends HttpServlet {
@EJB
private ExampleBean ejb;
...

Hope this helps,

Manuel
 
azhar jodatti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks manual but i am still wonder.....
you used ejb==null but ejb will be always null because it is diffrent servlet.....and it means it will lookup for the bean every time....

consider the scenario.....

1. i am using a session bean(statefull) / entity bean...
2. i have to servlets one reads the data and one updates the data...
as per my knowledge on EJB(Entity bean)..... i will use getter and setter methods for the database fields.. right? ......
from my two serlvets... in one servelts i used getter methods of the bean.. now i have all the
database field as a getter properties of bean.... i will look up for than bean using lookup and i will call getter methods on that bean so, it wil return me the database field....
i displyed those field to the client... now if user want to change some values of the property then he will edit it with changed valued , and say submit....

now it wil go to the diffrent servlet.... surely it is for update.....
for bean if we call setter mathods then the bean field will automatically get updated with database... right.... but to call those setter methods of the bean from which i got the data by calling getter methods i need that same bean...

now my qst is if i lookup for the bean in update servlet then it will give me that type of bean...
can i call setter methods on this bean.. is it same as previous bean? ...


will you help me with some code....



 
Manuel Alberto Quero
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello azhar,

I think you are a litte bit wrong about EJB concepts. You are trying to use a Stateful Session Beans as a singleton (you want it to keep the state between calls from different clients). Stateful Session Beans are intended to keep the state between calls from the same client.

Given your scenario, I think you should use Stateless session beans. You should read about Entity Manager. It is the responsible for keeping synchronized your beans with database so you don't have to worry about that at all...

Hope this helps,

Manuel
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the original question - You dont have to worry if you are using stateless session beans.
If you are using stateful beans, just to add to what Manuel had already posted, one way to cache the reference is to get a Handle (EJB Handle)and store it in the session. You can retrieve the ejb (the stub actually) from the Handle in later invocations.

ram.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!