21

How do I retrieve the session ID value inside a JSF managed bean?

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
Veera
  • 32,532
  • 36
  • 98
  • 137

3 Answers3

44
FacesContext fCtx = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
String sessionId = session.getId();
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
Damo
  • 11,410
  • 5
  • 57
  • 74
1

You try

{
String uuidFc = FacesContext.getCurrentInstance().getExternalContext().getSessionId(true);
}

This line return the facesContext sessionId value

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
  • This returns the servletcontext sessionid. And how does it differ from the other accepted answer? The true? Then why should be added to the answer – Kukeltje Feb 13 '19 at 15:31
0

You can also use the OmniFaces JSF utility library.

String sessionId = Faces.getSessionId();

Adding this as an answer, since it's highly likely that you will use this library, if you use JSF.

kozeljko
  • 160
  • 2
  • 13