将控制权从被调用者 servlet 发送回调用者 jsp

sending back the control from callee servlet to caller jsp

我有一个要求,就像用户在不登录应用程序的情况下尝试访问应用程序中的任何 url 一样

loggedin=false;
if(!loggedin)
{
forward request to login jsp page
}
else
{
execute the jsp
}

现在登录 jsp 将获取登录凭据并将其发送到控制器 servlet,如果登录凭据正确,控制器 servlet 必须将请求发送回用户访问的 url . 我尝试过使用重定向/转发,但这些都不能完全满足需要。

我有什么建议可以实现吗

Spring 安全性甚至 Java EE 声明性安全性中都有开箱即用的解决方案。

无论如何,如果您想实现该行为,只需将原始值 URL 存储在 Cookie 中即可。然后,当用户正确登录时,您可以重定向到保存的 URL

    boolean loggedin=false;
    if(!loggedin){
        Cookie c = new Cookie("URC", request.getPathInfo());
        c.setHttpOnly(true);
        c.setPath(request.getContextPath());
        c.setMaxAge(-1);
        response.addCookie(c);
        //forward request to login jsp page
    }else{
        Cookie cookie = getUrlRedirectCookie(request);
        if(cookie!=null){
            //redirect to cookie.getValue()
        }else{
            //execute the jsp
        }
    }
}

private Cookie getUrlRedirectCookie(HttpServletRequest request){
    Cookie[] cookies = request.getCookies();
    if(cookies!=null && cookies.length>0){
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if(cookie.getName().equals("URC")){
                return cookie;
            }
        }
    }
    return null;
}