aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Thread-safe on HttpSession Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Thread-safe on HttpSession Object" Watch "Thread-safe on HttpSession Object" New topic
Author

Thread-safe on HttpSession Object

Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
Today is my fourth day on HF book,

I come through pg200 "Protect session attributes by synchronizing on the HttpSession" and need some clarification.

When we protect context attribute by synchronizes on ServletContext (i.e. lock the context) , for that particular block code, it only work and safe from any other code that ALSO synchronizes on the ServletContext.

So ... how about the HttpSession ? doesn't it also only "safe from any other code that ALSO synchronizes on the HttpSession". Since the book didn't mentioned anything in details, I would like to clarify this.

THANKS !!!


SCJP 6, SCWCD 5, SCBCD 5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

doesn't it also only "safe from any other code that ALSO synchronizes on the HttpSession

Yes, that's your responsibility to synchronize it consistently.


[My Blog]
All roads lead to JavaRanch
krishna na
Greenhorn

Joined: Mar 17, 2009
Posts: 14
If we want to protect session attributes we synchronize on HttpSession object. I agree with it.

I have one question - In the book its mentioned "The client could open a new browser window" page 199.

There are 2 ways to open new browser window which is not mentioned in the book

1A) open new browser window. Hit the servlet url, which you have coded for testing the session. now from the same browser window File->new window which again hits the same url (IE6)
2A) open 2 seperate browser windows & hit the same urls.

changes observered
For the first scenerio 1A - same session is present
For the second scenerio 2A - different session is present

Please let me know if I am wrong.
If I use a tab & hit the same url then I will be in same session right ?


Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
krishna navalgund wrote:If we want to protect session attributes we synchronize on HttpSession object. I agree with it.

I have one question - In the book its mentioned "The client could open a new browser window" page 199.

There are 2 ways to open new browser window which is not mentioned in the book

1A) open new browser window. Hit the servlet url, which you have coded for testing the session. now from the same browser window File->new window which again hits the same url (IE6)
2A) open 2 seperate browser windows & hit the same urls.

changes observered
For the first scenerio 1A - same session is present
For the second scenerio 2A - different session is present

Please let me know if I am wrong.
If I use a tab & hit the same url then I will be in same session right ?




Yes, you are correct !

(a) open a new browser window from File-New menu will use the same session
(b) open a new tab of the same window also using the same session
(c) only open from the program shortcut will have different session (regardless of same or different type web browser)

may be the meaning or "open new browser" means from the web browser application executable file (e.g. firefox.exe file in Windows , in other word , same as open from the program shortcut), but not from File-New menu.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42627
    
  65
Whether a new browser tab or browser window or browser instance will share a new session or not is up to the browser. Whatever web app you're working on had better not rely on any particular browser's behavior for thread-safe semantics; that would be a recipe for disaster.


Ping & DNS - my free Android networking tools app
 
wood burning stoves
 
subject: Thread-safe on HttpSession Object