Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can solve this the following way:</p> <ul> <li><p>Tell the browser to not cache the login page, so that it will always send a fullworthy GET request on it instead of loading from the browser cache.</p></li> <li><p>When the login page is requested while the user is already logged-in, then send a redirect to the desired page (which you've remembered in the session when the user logs in).</p></li> </ul> <p>This could be done in one and same <a href="https://stackoverflow.com/tags/servlet-filters/info">filter</a> (although I'd personally prefer to disable browser caching on all change-sensitive dynamic pages, so that would then be done in another filter on a more generic URL pattern covering those pages).</p> <p>Here's a kickoff example assuming that you're using container managed authentication (for homegrown authentication wherein you manually put the user in the session, just check the presence of the session attribute instead):</p> <pre><code>@WebFilter("/login.xhtml") public class LoginPageFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; HttpSession session = request.getSession(); // Tell browser to not cache this page. response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. response.setHeader("Pragma", "no-cache"); // HTTP 1.0. response.setDateHeader("Expires", 0); // Proxies. // Check if user is logged in. if (request.getRemoteUser() == null) { // Not logged in, so remember previous URL and continue request. session.setAttribute("referrer", request.getHeader("referer")); // Yes, the misspelling is "correct"! chain.doFilter(request, response); } else { // Logged in, so redirect to initial referring URL, if any. String referrer = (String) session.getAttribute("referrer"); if (referrer != null) { response.sendRedirect(referrer); } else { // There was no referring page. Just continue request? chain.doFilter(request, response); } } } // ... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload