• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Stateful Session Bean - Conversational State

 
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sai,

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

Hoping it helps.
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic