The moment you are redirected to the login page, you need to save the current request URI. You are probably using Filter to do login validation and redirection. In this case, you can use HttpServletRequest#getRequestURI() to get the URI of the current request:
String requestURI = request.getRequestURI();
You can either pass it as a request parameter to the redirect URL, or save it in the session. Passing as a request parameter is most secure:
response.sendRedirect(request.getContextPath() + "/login.jsf?from=" + URLEncoder.encode(requestURI, "UTF-8"));
In the bean associated with the login page, you can set it as a managed property or view parameter. Suppose a bean is a viewport that allows you to perform nice ajax actions / validations, etc. In this 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 this URI to ExternalContext#redirect() :
public void login() throws IOException {
(if necessary, specify the default target for the case from null )
source share