This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSF and the fly likes session not available in a jsp file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "session not available in a jsp file?" Watch "session not available in a jsp file?" New topic

session not available in a jsp file?

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
I have a JSF project. In the back beaning classes, I use the following to access session:

HttpSession session =(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

But FacesContext.getCurrentInstance() is throwing null pointer exception in a Java class that is not getting used by any jsp, even though I registered this class in face-config.xml as <managed-bean>.

Why would I get this error? How should I write a Java class so that FacesContext is instanticated before I use it?


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962

FacesContext is instantiated and destroyed on each JSF HTTP request. It doesn't linger between requests, and it is only created by the Faces Servlet, so if an Http URL request doesn't get routed through the Faces Servlet, you won't have a FacesContext. That specifically means non-faces servlet requests and page requests made to stand-alone JSPs that aren't JSF JSPs.

Usually, if you're attempting to explicitly access a session inside a JSF managed bean, you're doing something wrong. That's because the JSF bean management framework should probably be injecting what you need instead of coding the bean to be explicitly dependent on running inside JSF.

Customer surveys are for companies who didn't pay proper attention to begin with.
jQuery in Action, 2nd edition
subject: session not available in a jsp file?
Similar Threads
My query is null
Sessions in JSF
How to get session scope backbean
How to destroy the Object of a SessionScoped bean
using HttpSession in Java Class?