Redirecting back to actual page what they were viewing before login

后端 未结 1 1267
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 09:13

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)?<

1条回答
  •  失恋的感觉
    2020-11-29 09:27

    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");
    }
    

    0 讨论(0)
提交回复
热议问题