• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between false and true sessions

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, can any one tell me the exact difference between
HttpSession session = httpservletrequest.getSession(true);
and
HttpSession session = httpservletrequest.getSession(false);

which is preferable?

thanking u in advance

cinux
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API Javadoc explains it all.

Originally posted by saikrishna cinux:
thanking u in advance


Who is "u"?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saikrishna cinux
Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.

In particular please see: UseRealWords

Abbreviations such as "u" or "ur" in place of "you" and "you are"
or "you're" confound language translation software making it hard
for our non-English speaking members to read your posts.

Again, welcome to JavaRanch and good luck with your question.
-Ben
 
Orem Hun
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With 'true' it creates a session if it does not exist, with 'false' it returns active session if exists otherwise null. It would be more efficient to use 'false', so you don't unnecessarily create sessions, and create sessions only when needed.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Orem Hun:
It would be more efficient to use 'false', so you don't unnecessarily create sessions, and create sessions only when needed.


Efficiency is not the issue. In such a case, how would you handle a null that is returned, when presumably you are trying to obtain the session in order to do something with it?
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


Efficiency is not the issue. In such a case, how would you handle a null that is returned, when presumably you are trying to obtain the session in order to do something with it?


test for null; if it is null; then recreate using true; geeezz how more confounding can it get.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anselm Paulinus:


test for null; if it is null; then recreate using true; geeezz how more confounding can it get.


Why bother with the test?
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:

Why bother with the test?



If suppose you are in a Servlet and want to invalidate the session. Then how will do that? Then in that case you have to provide attibute false like this...




Regards

Naseem
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


Why bother with the test?


session = req.getSesion(false)//retrieves already existing session or null
//if there is no current existing session

if(session == null)
session=req.getSession(true)

Now I understand that there are better ways of doing this; I could have done something like session = req.getSession() or req.getSession(true) in which case it would have returned an already existing session or created a new one where none exists; my initial response was in line with the discussion that has to do with creating a session using req.getSession(false); I do not know if I made this clear enough; and please let me know if my thought was wrong.
Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anselm Paulinus:
I do not know if I made this clear enough


Someone said that passing "false" as the argument was better, as it avoided creating extra sessions, and only returned an existing one.

Bear said efficiency isn't the question; if you need a session, you need it.

You said it would be better to pass false, test for null, then pass true. I don't understand why you said this, as this is functionally equivalent to just passing true the first time, except that it does two lookups, so is slower!

Then Bear said "why the test?" meaning "why not just pass true?" since, as I've explained, what you recommended doesn't make any sense.

And then your last response above doesn't make any sense to me at all.
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got it; actually, I was trying to tell the guy that said pass false that it was a bad idea, that was my comment of checking for null then passing true which shoud have been done initially; I guess I convoluted the whole thing.
 
Orem Hun
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One cannot say any one of those is better than the other. It depends on the context. In the context of "invalidate" example above, or simply your code is simply trying to check if an object exists in session (if getSession returns null, you know the answer, I don't think you want to session to be created for this purpose....), it makes sense to use false. There are ohter examples given above in which case passing true makes sense.

In a nut shell, not any one of those be considered as a standard for all contexts (or all applications).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic