0

JSF – Get HTTP Session Attribute

This post demonstrates how to retrieve a attribute from an HTTP Session object in JSF 2.

Requirements

Stuff used in this post.

  • JSF 2.2
    • javax.faces-2.2.8.jar
  • JDK 8

Using FacesContext

To access the HTTP Session object, we need to use the FacesContext to get an external context. JSF has its own context apart from the Servlet/JSP context which in this case is our “external context”.

The following codes look for an attribute named "loggedIn" in the HttpSession object. If the attribute exists, we set sessionData to that value. Otherwise, we set sessionData to "false".

This outputs:

Testing

For testing purposes, we use another URL to set the attribute "loggedIn" in the HttpSession object to "true".

If we click the same button in the previous page, the value is now "true".

Download the codes

https://github.com/Turreta/JSF-Get-HTTP-Session-Attribute

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert