aspose file tools
The moose likes JSF and the fly likes Accessing JSF sesson bean from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » JSF
Reply Bookmark "Accessing JSF sesson bean from a servlet" Watch "Accessing JSF sesson bean from a servlet" New topic
Author

Accessing JSF sesson bean from a servlet

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 434
Hi,

I have this managed session bean defined in my faces-config.xml file:

<managed-bean>
<managed-bean-name>ConfigLoginBean</managed-bean-name>
<managed-bean-class>com.comcast.npsconfig.login.ConfigLoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

How do I access this bean from my servlet? I've noticed invoking FacesContext.getInstances() returns null. This is what I currently have, which fails because of the NullPointerException.

private void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
...

// Get the config login bean.
FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
ConfigLoginBean configLoginBean = (ConfigLoginBean)app.getVariableResolver().resolveVariable(ctx, "ConfigLoginBean");
configLoginBean.finishLogin(uc);
...
}

Thanks for your help, - Dave
Venkat Sadasivam
Ranch Hand

Joined: May 10, 2008
Posts: 139
You can try below code
HttpSession session = request.getSession(false);
ConfigLoginBean configLoginBean = (ConfigLoginBean) session.getAttribute("ConfigLoginBean");


“Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”<br>
-Martin Fowler
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 434
Thanks. I tried this and even on JSF pages, this returns null. - Dave
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
I think that this can solve your problem.

And the answer to your last post is that the Servlet is out of the FacesContext so it will be always null and the JSF Beans will not be out of the FacesContext.
[ July 11, 2008: Message edited by: Andres Quinones ]
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 434
That was it! Thanks for the help, - Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing JSF sesson bean from a servlet
 
Similar Threads
Weired Problem with JSF TLD on Tomcat
"#{...} is not allowed in template text" error
is there a mayor change on how to use faces-config.xml on JSF 2.0
Not creating managed bean
Obtaining an instance of a Managed Bean declared with Application Level Scope