If my user is redirected to a login page, how to make it so that, after they log in, they get redirected to the -original- destination page (the one they first clicked on)?<
Pass the originally requested URL as request parameter on the redirect to the login page.
String from = URLEncoder.encode(request.getRequestURI(), "UTF-8");
if (request.getQueryString() != null) {
from += "?" + request.getQueryString();
}
response.sendRedirect("login.jsp?from=" + from);
In login.jsp
, pass it to the login form submit target as a hidden input field.
(note: fn:escapeXml()
prevents you from XSS when redisplaying user-controlled data in HTML)
In the login action, check if it is there and then handle accordingly.
String from = request.getParameter("from");
if (from != null && !from.isEmpty()) {
response.sendRedirect(from);
} else {
response.sendRedirect("home.jsp");
}