Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which one is better to use?

 
Amudha Loganathan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shashi Kanta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paulo Lima
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic