Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Access managed bean from a servlet

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I want to invoke a managed bean method in a Servlet.
And so I used the following code:


I am able to get the value when the page from where the action has been initiated is not mapped to the backing bean's navigation rule.

But in some cases where the page from where the action has been initiated is mapped to the backing bean's navigation rule, I am unable to access the backing bean.
I get a NULL value for the object of the backing bean.

The scope of the backing bean is set to SESSION and I do not evict it explicitly.

Please let me know how can I fix this issue.

Thanks in advance!!
 
Greenhorn
Posts: 14
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the following:



In some cases this return null as the JSF managed bean and servlet life cycles are different. In this case it might mean that you've called your servlet too early.
 
Saloon Keeper
Posts: 25466
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSF managed beans are constructed on demand, not in advance. So if you attempt to access a managed bean but that bean hasn't yet been accessed by a JSF request, it won't exist even though it's defined in faces-config.

There are really only 2 ways to handle that.

1. Ensure that a JSF request is ALWAYS made first

2. Manually construct the bean in servlet code if it doesn't exist yet.

Solution #2 does have one downside, though. The JSF bean manager also injects the managed properties when it constructs the bean. If you instantiate the bean in servlet code, you also have to manually inject the managed properties. Which means that you have to keep that code in sync with the management specs in faces-config or unpredictable things may happen.
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic