Pushkar
Pushkar

Reputation: 33

Access request Attributes (Set in managed bean before redirect) in Filter by precreating FacesContext

I am setting the request attribute in managed bean before redirect the request through faces-config as follows:

FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("foo","bar");

return "redirect_success";

After this i m trying to access this request attribute in my filter through pre creating FacesContext

FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("foo");

Finally not able to get this attribute in filter itself but i am able to get the same attribute again in second managed bean very easily. Is there any way to get it in filter itself?

Upvotes: 3

Views: 11224

Answers (1)

BalusC
BalusC

Reputation: 1108537

Two ways:

  1. Store in session and let filter remove it from session if necessary.

    externalContext.getSessionMap().put("foo", "bar");
    

    There's by the way no need to create FacesContext yourself in a Filter. Just cast ServletRequest to HttpServletRequest.

    HttpSession session = ((HttpServletRequest) request).getSession();
    String foo = (String) session.getAttribute("foo");
    session.removeAttribute("foo");
    
  2. Use ExternalContext#redirect() to add it as request parameter.

    externalContext.redirect("other.jsf?foo=bar");
    

    And then in Filter:

    String foo = ((HttpServletRequest) request).getParameter("foo");
    

Upvotes: 4

Related Questions