This is not true. FacesContext not available in ServletRequestListener as such. getCurrentInstance() can return null , which will result in NPE.
If you use webapp on an HTTP web server (and thus are not a Portlet web server, for example), you can simply drop ServletRequest to HttpServletRequest .
public void requestInitialized(ServletRequestEvent event) { HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
Note that a more common practice is to use Filter for this, since you can map it to a fixed URL pattern, such as *.jsf , or even to specific servlets so that it runs only when FacesServlet starts. For example, you can skip cookie checks on static resources like CSS / JS / images.
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) { HttpServletRequest request = (HttpServletRequest) req;
If you are already inside the JSF context (in a managed bean, phase designer, or something else), you can simply use ExternalContext#getRequestCookieMap() to get cookies.
Map<String, Object> cookies = externalContext.getRequestCookieMap();
When running JSF on top of the servlet API, the map value is of type javax.servlet.http.Cookie .
Cookie cookie = (Cookie) cookies.get("name");
source share