• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

session creation problem

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the folowing code (jdiscuss.com, Test 3 (SCWCD1.4), Question 8):

<% Integer count = (Integer) request.getSession(false).getAttribute("count");
if(count != null )
{
out.println(count);
}
else request.getSession(false).setAttribute("count", new Integer(1));
%>


WHY a new session is created (and the count attribute set) since the method getSession(false) is called? I thought that only getSession() and getSession(true) create a new session !!!

I did the test by putting this code in a very new jsp and indeed after its execution there is a session (isNew returns true on it) with the attribute inside !

Thanks in advance
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getSession(false) will not create a session, but there are lots of other ways a session can be created without explicity creating one.

For instance, if a JSP is entered that does not explicitly claim not to have a session (for example, <%@ page session="false" %> , one will be created automatically. Try adding this directive to your JSP and you should receive a NullPointerException, as getSession(false) should return null.
 
Gab Buda
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc,

you're right, the session is automatically created for the jsp before the first explicit use of the "session" object or a getSession() call if you dont't use the session="false" in the page directive.

The same code, this time executed in a servlet, throws a NullPointerException !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic