• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Stateful bean -again

 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
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?
Thanks
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
slobodan , could you post your code??
 
Goran Markovic
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic