Session variables from JSF

Posted Thursday, September 08, 2005 1:32 AM by Shunjie
I guess most people already know this but since I am pretty new to J2EE, I decide to post it here for personal references:

import javax.faces.application.*;
import javax.faces.context.*;
import javax.servlet.http.*;
public static HttpSession currentContext()
{
ExternalContext G = FacesContext.getCurrentInstance().getExternalContext();
HttpSession session =
(HttpSession) G.getSession(true);
        return session;
}

another way is the way recommended by Jsfcentral

FacesContext facesContext = FacesContext.getCurrentInstance();
Application app = facesContext.getApplication();
MyObject myObject = (MyObject)app.createValueBinding("#{myObject}").
getValue(facesContext);

I prefer the first way, gives me more control.

The best is still HttpContext.current.Session["mySessionObject"]..short and sweet..but thats .NET
=.=


Filed under: , ,