aspose file tools*
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
Author

session creation problem

Gab Buda
Greenhorn

Joined: Mar 25, 2006
Posts: 24
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


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

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
Greenhorn

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: http://aspose.com/file-tools
 
subject: session creation problem