As per the problem, you're apparently not using container managed authentication with a realm and <security-constraint>
in web.xml
. It will namely handle this fully transparently for you.
I'll assume that you've homegrown a filter which redirects the user to the login page when there's no logged-in user present in the session. In that case, you need to add the current request URL as a request parameter or session attribute as well.
Here's an example which passes it as a request parameter in the filter:
if (user == null) {
String from = URLEncoder.encode(request.getRequestURI(), "UTF-8");
if (request.getQueryString() != null) from += "?" + request.getQueryString();
response.sendRedirect("login.jsf?from=" + from);
}
Embed it as a hidden field in the login form (yes, using plain HTML/JSTL, JSF 1.x isn't helpful here):
<input type="hidden" name="from" value="${fn:escapeXml(param.from)}" />
In the login method, check if it is there and handle accordingly:
public String login() {
// ...
String from = externalContext.getRequestParameterMap().get("from");
if (from != null && !from.isEmpty()) {
externalContext.redirect(from);
return null;
} else {
return "home"; // Default landing page after login.
}
}
Here's an example which passes it as a session attribute in the filter:
if (user == null) {
String from = request.getRequestURI();
if (request.getQueryString() != null) from += "?" + request.getQueryString();
request.getSession().setAttribute("from", from);
response.sendRedirect("login.jsf");
}
This doesn't need a hidden field. In the login method, check if it is there and handle accordingly:
public String login() {
// ...
String from = externalContext.getSessionMap().get("from");
if (from != null && !from.isEmpty()) {
externalContext.getSessionMap().remove("from");
externalContext.redirect(from);
return null;
} else {
return "home"; // Default landing page after login.
}
}