my dog learned polymorphism
The moose likes Servlets and the fly likes Problem on session managment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem on session managment" Watch "Problem on session managment" New topic

Problem on session managment

preet girn

Joined: Aug 28, 2012
Posts: 5
Hello to all!

I want to access same session Id from the instance of two different browsers. Can any body help me out?

First time i send the request from google crome session is created wrt this request.
Now i open new window of Mozilla.

I want to access same session in mozilla.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32815

Why do you want to do this? Two browsers is two different sessions.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 297


The behavior is not identical.Session id will be different. but there are other things for session management like hidden fields through which you can get the same session id across different browser. but on every page you need to have that hidden field.


preet girn

Joined: Aug 28, 2012
Posts: 5
Actually i want to develop a small online test application. In this application i have a requirement that any user which is already log in, should not log in again in any other browser. also if he/she tried to do so then they get same as if anybody tried to open two instances of same browser. i.e., user should be already logged in.
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 297

Hi Preet,

For this you can take one extra field in database as islogedin .. if user is logged in set it to true .Once user loges out set it to false.Also need to set it to false on session timeout .

If you are not using database then you should maintain map with all the users who are logged in and can check whether the user is logged in or not.

Jatan bhavsar
preet girn

Joined: Aug 28, 2012
Posts: 5
jatan bhavsar,

Many thanks to you. This will help me a lot.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20183

jatan bhavsar wrote:Also need to set it to false on session timeout .

That's not going to work that easily. If I have two open browser windows and I ignore one, then its session will timeout while I'm still active in the second browser window. You can only log someone out in the database if a) he logs out manually, or b) all of his sessions timeout. That means you have to keep track of all these sessions per user. You could use the database, or a servlet context attribute. The latter could be a Map<String, Set<String>> with the keys being the user names and the values being sets of session IDs. If a session is created for the user, add an element to this Set (creating it if necessary), and if a session is destroyed you remove an element. If that was the last element you can remove the entire map entry and log the user out of the database. Make sure you add proper synchronization to the servlet context attribute if you choose this solution.

How To Ask Questions How To Answer Questions
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 128

yet another way could be something like this -

maintain the state of user (eg. which page is the user viewing right now with what all params in the server session itself)
have a mechanism to retrieve the session Object using the user Id
every time when the application is opened in a browser, render the login page, (maintain the session using URL rewriting or hidden field instead of cookies to avoid any previously left cookie)
whenever there is a login request, check whether that user is already logged in, if yes, then render the existing state (with new session & invalidate the older session), otherwise show the normal home page.
I agree. Here's the link:
subject: Problem on session managment
It's not a secret anymore!