How to differ sessions in browser-tabs?

2018-12-31 09:27发布

In a web-application implemented in java using JSP and Servlets; if I store information in the user session, this information is shared from all the tabs from the same browser. How to differ sessions in the browser-tabs? In this example:

<%@page language="java"%>
<%
String user = request.getParameter("user");
user = (user == null ? (String)session.getAttribute("SESSIONS_USER") : user);
session.setAttribute("SESSIONS_USER",user);
%>
<html><head></head><body>
<%=user %>
<form method="post">
User:<input name="user" value="">
<input type="submit" value="send">
</form>
</body></html>

Copy this code in a jsp page (testpage.jsp), deploy this file in an existing context of a web application on the server (I use Apache Tomcat), then open a browser (FF, IE7 or Opera) using the correct URL (localhost/context1/testpage.jsp), type your name in the input and submit the form. Then open a new tab in the same browser, and then you can see your name (get from the session) on the new tab. Be careful with the browser-cache, sometimes seems that it doesn't happen, but it's in the cache, refresh the second tab.

Thanks.

21条回答
余欢
2楼-- · 2018-12-31 09:39

We had this problem and we solved it very easy. I mean easy because no programming involved. What we wanted to do was to let a user login to multiple account within same browser window without conflicting the sessions.

So the solution was random subdomains.

23423.abc.com
242234.abc.com
235643.abc.com

So we asked our system admin to configure the SSL certificates for *.abc.com rather abc.com Then with little code change, every time a user try to login, he gets logged in a tab with a random subdomain number. so each tab could have its own session independently. Also to avoid any conflict, we developed the random number using a hash or md5 of user id.

查看更多
看淡一切
3楼-- · 2018-12-31 09:39

I'll be honest here. . .everything above may or may not be true, but it all seems WAY too complicated, or doesn't address knowing what tab is being used server side.

Sometimes we need to apply Occam's razor.

Here's the Occam's approach: (no, I'm not Occam, he died in 1347)

1) assign a browser unique id to your page on load. . . if and only if the window doesn't have an id yet (so use a prefix and a detection)

2) on every page you have (use a global file or something) simply put code in place to detect the focus event and/or mouseover event. (I'll use jquery for this part, for ease of code writing)

3) in your focus (and/or mouseover) function, set a cookie with the window.name in it

4) read that cookie value from your server side when you need to read/write tab specific data.

Client side:

//Events 
$(window).ready(function() {generateWindowID()});
$(window).focus(function() {setAppId()});
$(window).mouseover(function() {setAppId()});


function generateWindowID()
{
    //first see if the name is already set, if not, set it.
    if (se_appframe().name.indexOf("SEAppId") == -1){
            "window.name = 'SEAppId' + (new Date()).getTime()
    }
    setAppId()
}

function setAppId()
{
    //generate the cookie
    strCookie = 'seAppId=' + se_appframe().name + ';';
    strCookie += ' path=/';

    if (window.location.protocol.toLowerCase() == 'https:'){
        strCookie += ' secure;';
    }

    document.cookie = strCookie;
}

server side (C# - for example purposes)

//variable name 
string varname = "";
HttpCookie aCookie = Request.Cookies["seAppId"];
if(aCookie != null) {
     varname  = Request.Cookies["seAppId"].Value + "_";
}
varname += "_mySessionVariable";

//write session data 
Session[varname] = "ABC123";

//readsession data 
String myVariable = Session[varname];

Done.

查看更多
宁负流年不负卿
4楼-- · 2018-12-31 09:39

In javascript, how can I uniquely identify one browser window from another which are under the same cookiedbased sessionId

Essentially use window.name. If its not set, set it to a unique value and use it. It will be different across tabs that belong to same session.

查看更多
有味是清欢
5楼-- · 2018-12-31 09:40

The window.name Javascript property, is the only thing that will persist across tab activity, but can remain independent (instead of URL guff).

查看更多
后来的你喜欢了谁
6楼-- · 2018-12-31 09:42

You can use link-rewriting to append a unique identifier to all your URLs when starting at a single page (e.g. index.html/jsp/whatever). The browser will use the same cookies for all your tabs so everything you put in cookies will not be unique.

查看更多
人气声优
7楼-- · 2018-12-31 09:42

I developed a solution to this problem recently using Cookies. Here is a link to my solution. I have also included sample code of the solution using ASP.NET, you should be able to adapt this to JSP or Servelets if you need.

https://sites.google.com/site/sarittechworld/track-client-windows

查看更多
登录 后发表回答