This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

session creation problem

 
Gab Buda
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic