How to Get Session ID In JSF?

There are two ways to get the session id in JSF. One way through HttpSession and other way through cookies. Both these options are using FacesContext.

  Option 1:

FacesContext fCtx = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
String sessionId = session.getId();

Option 2:

Map<String, Object> cookies = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap();
Cookie cookie = (Cookie) cookies.get("JSESSIONID");
String sessionId = cookie.getValue();


Post a Comment

Powered by Blogger.