aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Stateful Session Bean - Conversational State Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Stateful Session Bean - Conversational State" Watch "Stateful Session Bean - Conversational State" New topic
Author

Stateful Session Bean - Conversational State

Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 457

Dear All,

I implmeneted a stateful session bean to represent a shopping cart and invoking this bean from servlet. Every time I add an item to the cart I am showing the number of items in the Servlet. However, the bean is returning the recently added items always and not preserving previously added items in the list. The following is the code I am using.

Stateful Session Bean Code:



Servlet Code:



Item Code:



My understanding is, the bean's @PostConstruct call back is being called every time I add an item to the cart. I do not know why a new instance is being returned for each servlet call eventhough I am using the same bean reference to add item to cart and to get items from cart. Please help!

I deployed this bean to Glassfish V3 along with web application as an EAR using Eclipse.


Sai Surya, SCJP 5.0, SCWCD 5.0, IBM 833 834
http://sai-surya-talk.blogspot.com, I believe in Murphy's law.
Christian Nicoll
Ranch Hand

Joined: Mar 09, 2008
Posts: 106
Hi Sai,

i'm bit confused about your servlet-code. In which method (doGet, doPost, ...) do you 've put this servlet-code? Please show the whole servlet-code.

My understanding is, the bean's @PostConstruct call back is being called every time I add an item to the cart.


That is not my understanding.

I believe that this makes not really sense. It is more logical if you call an method (addItem) of a stateful session bean, that add's the item to customers shopping cart. Otherwise if the @PostConstruct call back would be called then there would exists one stateful session bean for each call, so each Bean would 've only one item.


SCJP 5, SCJD 5, SCWCD 5, SCBCD 5, SCJDWS 5
My SCBCD-Notes - My Hello World Webservice
Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 457

Hi Nicoll,

Thanks for the reply I managed to make it work with DI using @EJB annotation.

Still not sure why this code in servlet not working as desired. The servlet code I posted is exactly from doPost(). Just calling the addItem() may not work since the shopping cart array list is not initialized hence I did initialization in @PostConstruct.
Christian Nicoll
Ranch Hand

Joined: Mar 09, 2008
Posts: 106
Hi Sai,

I would try the following code in the doPost()-method:

Hoping it helps.
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
I think, that every time look up Item via JNDI you end up with new instance.
Consider this text from ejb core spec, page. 74-75

A session bean instance’s life starts when a client obtains a reference to a stateful session bean
instance through dependency injection or JNDI lookup, or when the client invokes a create<METHOD> method
on the session bean’s home interface. This causes the container to
invoke newInstance on the session bean class to create a new session bean instance. Next,
the container injects the bean’s SessionContext, if applicable, and performs any other
dependency injection as specified by metadata annotations on the bean class or by the deployment
descriptor. The container then calls the PostConstruct lifecycle callback interceptor
method(s) for the bean


So what I think you should do is to store the reference to HttpSession as attribute and use it in subsequent requests to your servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateful Session Bean - Conversational State
 
Similar Threads
printing message
printing cart contents
problems when using a datable within a form: currently my code is clunky
Unable to redirect the page through dispatcher
Storing of Data