File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes session creation problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "session creation problem" Watch "session creation problem" New topic

session creation problem

Gab Buda

Joined: Mar 25, 2006
Posts: 24
Given the folowing code (, Test 3 (SCWCD1.4), Question 8):

<% Integer count = (Integer) request.getSession(false).getAttribute("count");
if(count != null )
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

SCJP 1.4 (85%)<br />SCWCD 1.4 (94%)
Marc Peabody
pie sneak

Joined: Feb 05, 2003
Posts: 4727

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.

A good workman is known by his tools.
Gab Buda

Joined: Mar 25, 2006
Posts: 24
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 !
I agree. Here's the link:
subject: session creation problem
It's not a secret anymore!