如何使用 Java 将会话限制为单个浏览器选项卡

yar*_*997 5 html javascript java jakarta-ee

我不希望在浏览器选项卡之间共享用户的 HTTP 会话。

例如,如果用户在选项卡一中登录系统,他可以看到他的所有个人资料详细信息。但是,当从另一个新选项卡(选项卡二)点击相同的 URL 时,它也会显示相同的用户配置文件详细信息。

我想将用户会话限制为仅打开第一个选项卡。如果打开另一个选项卡,则不应使用第一个选项卡的会话。有什么办法可以做到这一点吗?

Hum*_*ing 0

您无法为浏览器中的每个选项卡创建不同的会话。

以下方法用于限制用户login在浏览器的另一个选项卡中再次使用相同的应用程序。

这对我有用。

您必须在IE浏览器中创建新会话才能以不同的用户身份登录。

URL您可以通过以下步骤限制用户不在其他选项卡中打开,

  1. 当用户单击login按钮时,请检查session您的server codeas,

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        response.setCharacterEncoding("UTF-8");
        response.setContentType("UTF");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
    
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        boolean loginStatus ;
    
        if( (name != null && name.equals("human")) && (password != null && password.equals("human")) ){
            loginStatus = true;
            HttpSession session = request.getSession();
    
            String status = (String)session.getAttribute("status"); 
    
            if(status != null && status.equals("loggedin")){
    
                // if status is not null , you can justify that , the user is already loggedin in the same session
    
                response.sendRedirect("redirect.jsp");
    
            }else{
    
                // If the user is trying to login first time,This will work
    
                // do your login work here
    
                if(loginStatus){
                session.setAttribute("status","loggedin"); 
                RequestDispatcher rd = request.getRequestDispatcher("success.jsp");
                rd.forward(request, response);
                }
            }
        }else{
    
            System.out.println("Authentication failed !");
            response.sendRedirect("fail.jsp");
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

你的redirect.jsp意愿是,

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Calculator</title>
</head>

<script type="text/javascript"></script>

<body>
    <h1 style="color:red;"> You are already in logged in state </h1>

</body>

</html>
Run Code Online (Sandbox Code Playgroud)

希望这个技巧能奏效。