As much as I understand Stateful session beans are used to maintain conversation state. Does anyone can give me an example?
I try something like this.
I add two variable to my Stateful bean :String name, and int age.
First servlet set name variable. Second servlet set age.
Third servlet invoke getters of session bean to retrieve a value of variables, which were previously set.
I got null,null.. It's probably because of my bad understanding of Stateful session beans. I thought that it maintains a state for particular user like this way, but I was wrong, obviously. Does anyone can example to me 'how it maintain the state' of user?
You should be careful when dealing with stateful EJBs from within a stateless context like servlets.
I am guessing that you are doing a DI in your servlet, that makes things wores in this case.
As I recall from my reading in EJB in action, one way to do it would be by storing a reference to the stateful EJB as an session attribute when you first create a session then in other servlets use the stored reference to access the correct stateful bean (the one that's associated with that client)
Thank you Musab Al-Rawi , you have right. I completely forgot 'that issue'. I figure out that each time I had created a new Bean in different servlets, but brain just stop working
Of course, i should store the statefull session bean into HttpSession, so I could monitoring that state of the same bean bound to particular user.
It's ok now, it really remember in its instance variable stuff I had consequently added.
First servlet create bean, retrieve name parameter, and store value into stateful bean instance.
Stateful bean I then store into HttpSession and redirect to JSP Than I populate JSp field and invoke second servlet inside what I have retrieved parameter value and stateful bean from HttpSession, and store second value into second instance variable of the same stateful bean.
I have repeat these steps, just to fulfill each instance of bean at various calls to ensure that they will be remembered after several JSP/Servlet invokation.
And the test has passed successful.
After I had store the values from many requests into the same stateful (ejb) bean which I have retrieved all of them with a values I put into variables. So, thank you guys once again on memory refreshing
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop