Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Doubt on session

 
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 .
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what will happen if the client has cookies turned off? This scenario will be on the exam...
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
I'm still in control here. LOOK at this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic