Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on pre-existing session

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 233 of HFSJ,they explained about preexisting session.getSession(false) returns session if there is one,else returns null.And getSession() method returns new Session everytime? I am very confused here.As per my understanding getSession() always returns already existing session if request is from same client

FirstRequest request.getSession()
session diesn't exist.GenerateSessionId,Create Cookie object,stuffSessionId into cooke,return sessionId to client
SubsequentRequests request.getSession()
Container gets sessionId from the cookie in the request,matches with an existing session....

As per above explanation container never creates new session for request from same client.

Then what is meant by pre-existing session?What is significance of request.getSession(false).? Can anybody explain?

Thanks
Veena
 
Radhika Gokhale
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veena,

It is clearly explained in HFSJ.
Request.getSession() with return you either the existing session if a match is found with the passed sessionid or will create a new session and set the JSESSIONID cookie.

Request.getSession(false) will only return a session if it finds a match for the passed session id. If it does not find any match then it will return a null as in it will never create a new session for you.

Request.getSession(true) will behave exactly the same as Request.getSession().

Regards,
Radhika
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Veena Point:
Then what is meant by pre-existing session?What is significance of request.getSession(false).? Can anybody explain?


One great use for request.getSession(false) is in a filter. Can you think of why this would be useful, Veena? (Remember that a filter can forward or redirect a request!)
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Radhika.Yeah it is clear now . Marc,I didn't read filters yet.Will keep this in mind when I read.

Thanks again

Veena
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radhika,

In HFSJ purpose of having getSeesion(false) is given as <b>"You might have a scenario in which a servlet wants to use only a previously created session.It might not make sense for the checkout servlet,for example,to start a new session."</b> I was confused here because ,getSession() already takes care of creating new session only if it doesn't exist.!!! We don't have to check fr it by using getSessin(false) method.

I didn't read filters yet.May be I will understan then??!!.

Thanks
Veena
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic