5

I don't want the user's HTTP session to be shared between browser tabs.

For example, if a user logged into the system in tab one, he can see all of his profile details. However, when the same URL is hit from another new tab (tab two), it also displays the same user profile details.

I want to restrict the user session to only the first tab opened. If another tab is opened then the session of the first tab should not be used. Is there any way that this can be accomplished?

yaryan997
  • 483
  • 3
  • 10
  • 18
  • 2
    possible duplicate of [How to differ sessions in browser-tabs?](http://stackoverflow.com/questions/368653/how-to-differ-sessions-in-browser-tabs) – reto Mar 12 '14 at 07:22
  • @reto I had already seen the link you provided. But none of the solution there is accepted by user. I just want to know is there any way I can make the differentiation, that the browser new tab is opened. Any other way ? – yaryan997 Mar 12 '14 at 07:32
  • What browser is this ? Is this IE ? – Human Being Mar 12 '14 at 07:35
  • @HumanBeing I want the generic solution not dependent on any browsers. for testing purpose I am using the firefox browser. – yaryan997 Mar 12 '14 at 07:41
  • If you read the many related questions and the answers you'll see that there isn't a generic way that works in all possible situations. – reto Mar 12 '14 at 07:44
  • @reto ok fine, there isn't any generic way but I am just asking that How can I differentiate the browser tab session in java. Is there any parameter or variable I can check whenever the browser new tab is opened and URL is hit. – yaryan997 Mar 12 '14 at 07:48
  • Please check my answer and let me know for help. – Human Being Mar 12 '14 at 07:53

3 Answers3

0

You can't create the different session for every tabs in your browser.

The below way is used to restrict the user to again login the same application in the another tab of the browser.

This worked for me.

In IE your have to create the New Session in the browser to get login as different different user.

You can restrict the user to not open the URL in another tab by the following steps ,

  1. When the user click the login button, Please check the session in your server code as,

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

Your redirect.jsp will be,

<!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>

Hope this trick will work.

Human Being
  • 8,269
  • 28
  • 93
  • 136
0

you can send each request with a session ID! then get each session by sessionID on server.

0

I have done that before, although without Java, only JavaScript. This should apply as is to your case, though.

Standard Method

The problem you are hitting is that... you are using a cookie, which is the standard method of allowing someone to log in once, and then go in any one tab and still be logged in. It is the best method in most situations. However, remember that the cookie is sent along each and every hit to the server and that may be very important if you have CSS, JS, IMG, etc. data that is not public.

Separate Sessions

In order to distinguish each tab (page) as a separate session, what you do is use a session identifier in the page, not using cookies. (you may need both to allow private data as mentioned earlier.)

This works well in forms where you can easily create a hidden <input> tag:

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

This supposes that you are generating your pages dynamically so each time someone hits that page, you get them a new session (actually you should have a special log in page to get the session, then navigate to the important page that shows you top-secret data...)

However, what you will find out is... YOU are responsible to carry that session identifier manually every where. That can be very tedious (i.e. NO direct link anywhere works because someone clicking a simple anchor link would not send that special session along, unless you add it as a parameter in the query string... or something of the sort. But then the session is visible in the URL. So using something like jQuery() you would have to capture each link, and if clicked you actually "POST a redirect". As I said, it's quite a bit of work!)

Note also that as soon as the user closes such a page, as far as he's concerned, he's logged out. Yet, the session is still active on the server. To really log the user out, you have to use the onclose event and make sure to send the server a quick notification to ask for the cancellation of the session.

Mixed Method

Using both methods: a hidden input (or whatever tag you want, very easy to retrieve with jQuery code) and the cookie, you may have better luck. That is, you may know that the user is "semi-logged in" with the cookie. Yet, without the session identifier, you do not show certain other top-secret data.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156