File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes which one is better to use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "which one is better to use?" Watch "which one is better to use?" New topic
Author

which one is better to use?

Amudha Loganathan
Greenhorn

Joined: May 20, 2002
Posts: 6
Hi,
To keep the current session alive which one of the below is the best practise?
1. using request.getsession()
2. using request.getsession(false)
Can any one help me to solve this
TAI,
AM


TAI,<br />Amudha L
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Hi,
The methods mentioned below are used for getting session.
use can set the timeout period either using web app deployment descriptor or setting them programmatically.

Originally posted by Amudha Loganathan:
Hi,
To keep the current session alive which one of the below is the best practise?
1. using request.getsession()
2. using request.getsession(false)
Can any one help me to solve this
TAI,
AM


Groovy
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
if u r going to use the same session object in subsequent servlets, u can use either method!!
getSession() or getSession(true) gets the current session object or creates a new one if there is none!
getSession(false) gets the current session but, if one doen't exist already, it doesn't create a new one.
if u r talking about creating a session object for the first time i.e. in the first servlet, then using getSession() or getSession(true) is a better option as u'll get a new session object even if there wasn't any. whereas, getSession(false) won't creat any, and u won't have any session object to pass session values to the remaining servlets!!
hope that cleared ur doubt!!
rgds,
Shashi
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
It's always a good idea to explicitly use the one with the parameter getSession(true), because some books do not even mention it at all that getSession(true) and getSession() are the same thing.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Paulo Lima
Greenhorn

Joined: Apr 04, 2002
Posts: 10
Hi, Amudha !
Try on request.getsession(true);
It's will create a session object on the first time and return the session object if it already exists.
Regards,
Paulo Lima.
Originally posted by Amudha Loganathan:
Hi,
To keep the current session alive which one of the below is the best practise?
1. using request.getsession()
2. using request.getsession(false)
Can any one help me to solve this
TAI,
AM
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
paulocdl
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at JavaRanch's naming policy. . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please change to a new name which meets the requirements.
Thanks.
timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
I think most everyone has covered the fact that the various getSession() methods simply return a session if it exists OR create a session if there isn't one ( getSession(true) ) do not create a new session if one doesn't already exist ( getSession(false) )
If you are trying to keep the session alive between delayed requests you should be able to set the session timeout period somwhere in the servers configuration.
Tomcat allows you to never timeout by setting the timeout period to -1.
Sanjay P Kumar
Ranch Hand

Joined: Jul 17, 2001
Posts: 40
Call
setMaxInactiveInterval(int interval)on HttpSession.
"Specifies the time, in seconds, between client requests before the servlet container will invalidate this session. A negative time indicates the session should never timeout".
You can also specify time out in the Descriptor.
Thanks
Sanjay
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Many servlet programming problems can be due to not realizing the getSession( true ) has created a new session instead of recovering one. You can use the HttpSession method:
boolean isNew()
to detect when this happens.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which one is better to use?