*
The moose likes Servlets and the fly likes difference between false and true sessions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "difference between false and true sessions" Watch "difference between false and true sessions" New topic
Author

difference between false and true sessions

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
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


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

The API Javadoc explains it all.

Originally posted by saikrishna cinux:
thanking u in advance


Who is "u"?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Orem Hun
Greenhorn

Joined: May 24, 2005
Posts: 28
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

Joined: Jan 10, 2002
Posts: 60822
    
  65

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

Joined: Sep 05, 2003
Posts: 389
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

Joined: Jan 10, 2002
Posts: 60822
    
  65

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

Joined: Apr 25, 2005
Posts: 809

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


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
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

Joined: May 24, 2005
Posts: 28
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between false and true sessions
 
Similar Threads
doubt in session
Sessions in JSF
Help on HttpSessionListener
Servlet concepts doubts
Invalidating Session Only in IE5.5