1
votes

I have Two different JSF page Let Us suppose A.jsf and B.jsf But Both calling same managed bean different methods ManagedBean.java

A.jsf is calling a SessionScoped Managed bean method where i set some attribute in Request class object

HttpServletRequest request = (HttpServletRequest) FacesContext
                .getCurrentInstance().getExternalContext().getRequest();
request.setAttribute("token", requestToken.getToken()); 
        request.setAttribute("tokenSecret", requestToken.getTokenSecret());

Then redirecting some other side like this

response.sendRedirect(requestToken.getAuthorizationURL());

Now after successful login i am opening another JSF page of my website suppose b.jsf and from this page i am calling method like this

<f:event listener="#{ManagedBean.redirectLogin2}" type="preRenderView" />

and calling same Managedbean but another method

public String redirectLogin2() throws TwitterException {

        HttpServletRequest request = (HttpServletRequest) FacesContext
                .getCurrentInstance().getExternalContext().getRequest();
}

But when i am doing in above method redirectLogin2()

request.getAttribute("token")
request.getAttribute("tokenSecret")

Both giving Null. What cab be the issue here?

1

1 Answers

1
votes

Request scoped attribute life span will be lost on sendRedirect. You should set value on session scope.

 HttpSession session=request.getSession();
 session.setAttribute("token", requestToken.getToken()); 
 session.setAttribute("tokenSecret", requestToken.getTokenSecret());

After setting value to session. You can access that from request like

 HttpServletRequest request = (HttpServletRequest) FacesContext
            .getCurrentInstance().getExternalContext().getRequest();

request.getSession().getAttribute("token");
request.getSession().getAttribute("tokenSecret");

Although, above code will work but that is not good practice. JSF has @SessionScoped annotation which will make available of your variable access with login session.