8

Before user login, if type xxx.detail.jsf, it will redirect to the login page let user login. This task has been done already. How can I make it to redirect back to the xxx.detail.jsf after the user successfull login?

I'm using Eclipse Indigo, Tomcat 7 and Mojarra 2.0.3.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
heng heng
  • 693
  • 3
  • 13
  • 25
  • What you mean is certainly unclear. What do you mean "*if type `xxx.detail.jsf`, it will redirect to the login page*"? – Lion Jan 06 '12 at 01:58
  • @Lion if the user trying to access some page(eg: detail.jsf) before login, the system will redirect the user to the login page, and redirect back to the page(eg: detail.jsf) that the user want to access after the user successful login to the system. – heng heng Jan 09 '12 at 01:29

1 Answers1

11

At the point when you're redirecting to the login page, you need to save the current request URI. You're likely using a Filter to perform the login check and the redirect. In that case, you can use HttpServletRequest#getRequestURI() to obtain the current request URI:

String requestURI = request.getRequestURI();

You could either pass it as a request parameter in the redirect URL or store it in the session. Passing as a request parameter is the safest:

response.sendRedirect(request.getContextPath() + "/login.jsf?from=" + URLEncoder.encode(requestURI, "UTF-8"));

In the bean associated with the login page, you could set it as a managed property or a view parameter. Let's assume that the bean is view scoped so that you can perform nice ajax actions/validations and like. In that case, the view parameter is the only neat way:

<f:metadata>
    <f:viewParam name="from" value="#{login.from}" />
</f:metadata>

Then, when the real login succeeds, you can redirect to that URI by ExternalContext#redirect():

public void login() throws IOException {
    // ...

    FacesContext.getCurrentInstance().getExternalContext().redirect(from);
}

(if necessary supply a default target for the case that from is null)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • can you please guide me how to add in your answer in my code. `externalContext.redirect(from);` I get error on this line – heng heng Jan 09 '12 at 01:38
  • It's available by the `FacesContext` the usual way. I've edited my answer. – BalusC Jan 09 '12 at 11:43
  • thanks for your answer, it helps me a lot. Can you show me if I want to count down 3sec then only start to redirect back to the page, how to do this? – heng heng Jan 31 '12 at 09:16
  • Redirect to an intermediate page which has a `` in the ``. – BalusC Jan 31 '12 at 11:51
  • Thanks again for your quick reply. – heng heng Feb 01 '12 at 08:49
  • BalusC, I refer to here [http://stackoverflow.com/questions/4992526/how-to-handle-session-expiration-in-jsf-2-0?answertab=active#tab-top](http://stackoverflow.com/questions/4992526/how-to-handle-session-expiration-in-jsf-2-0?answertab=active#tab-top) The below code doesn't work for me, after my session was expired, It won't redirect to the home page, may assist? ` javax.faces.application.ViewExpiredException /home.xhtml ` – heng heng Feb 02 '12 at 10:18
  • @BalusC The solution above seems to lose the request parameters from the original request. To include the http request parameters from the original page would it just be a matter of iterating through `request.getParameterMap()` and appending the key/value pairs to the `requestURI` string before encoding? – Ryan Bennetts Mar 10 '16 at 08:32
  • 1
    @Ryan: `request.getQueryString()` is easier. – BalusC Mar 10 '16 at 08:41