aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt on session 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 "Doubt on session" Watch "Doubt on session" New topic
Author

Doubt on session

Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
Can anybody tell as to how a single session spans across multiple requests? How is it done practically?
With regards,
Padma priya N.G.


Padma priya N.G.
Be the change you want to be - Mahatma Gandhi
Sahid Khan
Ranch Hand

Joined: Jun 27, 2007
Posts: 41
Basically, when you call request.getSession(), container creates a session and attaches with that request, that is if the request is not already associated with any session. While sending the response, container adds a cookie which contains the sessionid. Now when the user sends next request, container gets the sessionid from cookie, and retrieves the session it created in first request.
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi Sahid,
What you told works for a single request between two machines. How does it work when different requests come from diferent machines? How is a single session maintained across different requests?
With regards,
Padma priya N.G.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Session :

Means, conversational state between client and server.

Client means browser.

browser1 makes a request to server.So server will create a session for this client and attaches to the response.

browser2 makes a request to server.So server will create a session for this client and attaches to the response.

Here there is no connection between browser1 and browser 2.

If angain browser1 makes a request to the server,this time this request contains some id to identify at the server side who is this client ?

Because this time two session id's are there at server side.


Thanks

Anil Kumar
Sahid Khan
Ranch Hand

Joined: Jun 27, 2007
Posts: 41
Hi Padma priya,

Session is totally a server side object. When a client sends a request. Server checks if there is any sessionid attached with the request.
For server every request is different, it doesn't matter if it is coming from same browser, different browsers or different machines.
Server only looks for a sessionid in the request. If it finds one, it knows this request is part of which session, if not it creates a new session and attaches with the request.

HTH,
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
For a second request from a different browser, will a unique session id be created?
With regards,
Padma priya N.G.
Sahid Khan
Ranch Hand

Joined: Jun 27, 2007
Posts: 41
For a second request from a different browser, will a unique session id be created?

I think more appropriate question to be asked, is "will server create a new session for second request"?
First of all server will not know whether it's second or first request. For server, every request is afresh. But to make the request-response conversational, server creates a http session and attaches a unique id for that session with the particular response. And server does it implicitly by setting a cookie. So when a request comes in, server will check if there is any session id with the request, if not server will create one.

Does that help?
maybe I am saying same thing again and again, but I find it difficult to put it in any other way .
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
so what will happen if the client has cookies turned off? This scenario will be on the exam...
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Padma priya Gururajan:
For a second request from a different browser, will a unique session id be created?

It depends on what you mean by "a different browser". If the second browser window was spawned by the first and the first was already participating in a session, then the second browser will also participate in the same session.

By "spawned" I mean that JavaScript from the previous window opened the second, an anchor link posted to a target outside of the first window, or a user uses a hotkey (like Ctrl+N) to create the second window apart from the first.

The exam typically won't get into anything this specific though I imagine it could ask something that would imply you knowing if it is possible for two browser windows to participate in the same session. Oh, and since we can have multiple browsers participating in the same session, this makes it extra clear that session attributes are not naturally thread safe (you could actually prove this with only a single window if you're clever, but the picturing a scenario of two browsers makes it easier to remember).
[ August 25, 2007: Message edited by: Marc Peabody ]

A good workman is known by his tools.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on session