• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

which one is better to use?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic