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.