File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes HttpSession concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "HttpSession concept" Watch "HttpSession concept" New topic

HttpSession concept

Jen Yang

Joined: Jul 07, 2003
Posts: 11
A newbie question: If a user opens 2 browsers on the same machine, are these browsers share the same session? More specifically, if this user changes a session attribute in one browser, when he clicks "refresh" in the other browser, will he see the change? Thanks.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 4442

Do mean windows of the same browser program or windows of two different browser programs?
If you open more than one window in IE they all share the same session. However if you start Netscape and hit the same URL, you'll get another session. I'm not sure if Netscape shares the same session or not between its own multiple windows.
I could be wrong but I think that it all depends on the browser that you're using. Not sure if there's any type of protocol that specifies one way or another.

Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
Each time you open your browser and go to a URL, that URL assigns a session ID, which is stored on your machine in some manner (cookie, URL rewriting). So, when you open two different browsers, and say, access, the ranch web server assigns each browser a session id, so there are TWO seperate sessions. These sessions are not aware of each other and therefore, cannot share variables.
[ August 29, 2003: Message edited by: Winston Smith ]
[ August 29, 2003: Message edited by: Winston Smith ]

for (int i = today; i < endOfTime; i++) { code(); }
Junilu Lacar

Joined: Feb 26, 2001
Posts: 4442

Originally posted by Winston Smith:
When you open a new browser, a new session is created, so no, you can't share session attributes between browsers.

Well, you can if the session is shared by windows of the same browser program, which IE does. We had to address this problem in our application because the user could open multiple windows and data would "cross over" when he switched back and forth between windows while editing two different records.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60741

It all has to do with cookies. If the browser windows will share cookies, they will share the session. If not, they won't.
Each browser handles this with their own rules, so you need to check what the situation is.
With regards to IE, back when I last checked on this, if another browser window was opened with 'New' or ctrl+N, the new window whould share cookies with the original window. But if a new instance of the pregram was started, they would not share cookies. This behaviour may have changed since last I checked, so try things out on your own to know how things will act for you.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
Indeed, if one instance of IE spawns another window, session variables can be shared since they will share the cookies.
However, if you open a new instance of IE, they will each have their own session and you won't be able to share session attributes.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: HttpSession concept
Similar Threads
session problem
session collusion problem
Session id identical - how to handle?
Differenciating between Http Request