How can I use Java to limit the session in one browser tab

I do not want the HTTP user session to be shared between browser tabs.

For example, if a user is logged in in a single tab, he can see all of his profile data. However, when the same URL comes from another new tab (tab 2), it also displays the same user profile information.

I want to limit the user session to only opening the first tab. If another tab is open, the session of the first tab should not be used. Is there a way this can be achieved?

+4
source share
3 answers

.

login .

.

IE , .

URL :

  • login, , session server code ,

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

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>

, .

0

! sessionID .

0

, Java, JavaScript. , .

, , , ... cookie, , - , . . , cookie , , CSS, JS, IMG .., .

() , , , , cookie. ( , .)

, <input>:

<input type="hidden" name="session" value="123"/>

, , , - , ( , , , ...)

, ... YOU . (.. - , - , , ... - - jQuery(), , "POST ". , !)

, , ​​, , . . , onclose , .

: ( , , jQuery) cookie, . , , "" cookie. , , .

0

Source: https://habr.com/ru/post/1531283/


All Articles