jQuery in Action, 2nd edition*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Confused about getSession 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 "Confused about getSession" Watch "Confused about getSession" New topic
Author

Confused about getSession

Reidar Gjerstad
Greenhorn

Joined: Dec 02, 2008
Posts: 19
This is from Ethuware/Standard Tests/Test 2/com.enthuware.ets.scwcd.v5.2.374

Consider the following JSP code:



What will it print for the very first request to this page as well as the web application that contains this page?

The correct answer is

"It will print Hello and will set the count attribute in the session."

I tried it and the answer is correct, but I don't understand why the code behaves that way. On the first request it prints "Hello!". Subsequent requests prints "1 Hello!". Obviously the else line sets the count attribute to "1".

When the first request comes in there is no session object available, so returns null because there is no session and false explicitly instructs the container NOT to create a new session. So the variable count gets set to null.

The line should in my opinion not succeed in setting the count attribute in any session object because there is no session and getSession(false) explicitly instructs the container NOT to create a new session.

Could anyone tell me where I misunderstand?

Cheers
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Reidar,

should in my opinion not succeed in setting the count attribute in any session object because there is no session and getSession(false) explicitly instructs the container NOT to create a new session.


request.getSession(false) is equivalent to request.getSession(). Which means get a session if exists , otherwise create a new one.
Here a new session is created for the first it executes.
Reidar Gjerstad
Greenhorn

Joined: Dec 02, 2008
Posts: 19
Balu Sadhasivam wrote:Reidar,

request.getSession(false) is equivalent to request.getSession(). Which means get a session if exists , otherwise create a new one.
Here a new session is created for the first it executes.


Hi Balu

Thanks for reply. I believe that what you write contradicts the API documentation. As far as I can understand, request.getSession(false) is not equivalent to request.getSession():

getSession(boolean create)

Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.

If create is false and the request has no valid HttpSession, this method returns null.

getSession()

Returns the current session associated with this request, or if the request does not have a session, creates one.

In this case, the argument is always false so no new session should be created. Or is the session object created anyhow? Or is the API doc incorrect?

Cheers
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Oops. yeah i meant otherwise.

Reidar,
I mean request.getSession(false) is not equivalent to request.getSession(). Ignore my previous reply.

This is JSP , so by default you have session created when request reaches this page , unless you set page session attribute as false.

So for first request , request.getSession(false).getAttribute("count"); , this would return session , but there is no attribute "count". (Imagine if session is null , this would throw NPE ?)


Since you set "count" session attribute here , From subsequent requests would return value.

Hope i m clear this time.


Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
I think request.getSession(true) is equivalent to request.getSession().
correct me if I am wrong.


Thanks,
Shailesh
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


I think request.getSession(true) is equivalent to request.getSession().


Yes . you are right.
Reidar Gjerstad
Greenhorn

Joined: Dec 02, 2008
Posts: 19
Balu Sadhasivam wrote:

This is JSP , so by default you have session created when request reaches this page , unless you set page session attribute as false.



Hi Balu

I did not know that you always have session created when the request reaches the page unless you set session attribute as false. This was new to me.

Thanks a lot for helping me out on this. I really appreciate your help.

Cheers
Dumitru Postoronca
Ranch Hand

Joined: May 06, 2008
Posts: 46
My first idea was that request.getSession(false).getAttribute("count"); should throw a NPE since getSession(false) will return null.
The only way this will work is if something else calls getSession() before reaching the JSP. Maybe it's related to the fact that @page directive has the default session="true" so the container creates a session for the JSP.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Dumitru/Reidar ,

request.getSession(false).getAttribute("count"); does not throw null pointer exception even the first time reason being in web applications session is created as soon as request reaches the controller or view. It is just that it does not find count attribute in the session. so in the first execution it does not display 1 but in next time, it finds count attribute in the session and display the value.

You can see this happening by using the following code in jsp. Even in first execution you can see the session object.



Regards
Salil Verma
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 165

request.getSession(false).getAttribute("count"); does not throw null pointer exception even the first time reason being in web applications session is created as soon as request reaches the controller or view.

Only if its a jsp page,session is created by default.I think you also meant so.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Salil Vverma wrote:request.getSession(false).getAttribute("count"); does not throw null pointer exception even the first time reason being in web applications session is created as soon as request reaches the controller or view


This doesn't seem to be completely true to me (maybe I have misunderstood you). Session is created when request.getSession(true) is called. So if your controller doesn't call this, then session is not created. As everyone is this is because of the default true value of the session attribute of page directive. You can confirm this by explicitly setting the session attribute to false. Then you'll get a NullPointerException...

[Seems Bindu was quicker than me ]


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



As everyone is this is because of the default true value of the session attribute of page directive. You can confirm this by explicitly setting the session attribute to false. Then you'll get a NullPointerException...


Sorry what ? in JSP ?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

@Salil
You can see this happening by using the following code in jsp. Even in first execution you can see the session object.


Thats because its JSP as said by Bindu.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Balu Sadhasivam wrote:

As everyone is this is because of the default true value of the session attribute of page directive. You can confirm this by explicitly setting the session attribute to false. Then you'll get a NullPointerException...


Sorry what ? in JSP ?


Sorry, I missed a word, it should be

"As everyone is saying this is because of the default true value of the session attribute of page directive. You can confirm this by explicitly setting the session attribute to false. Then you'll get a NullPointerException..."

I can't believe what I wrote, I really need all my fingers now (if you know what I mean )
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Bindu,
You are right. Session is automatically created as long as the controller is jsp and default session page directive has not been set to false.
Thanks for clearifying my response in a more accurate way.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Why is not that default creation signalized by HttpSessionListener?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Lukas,

Even that default session creation is signalized by HttpSessionListener. HttpSessionListener signals session creation and session destruction from jsp and servlet both.
Kindly paste the code if you tried something which did not work. We are look at the code and try to find out the reason of its not signaling properly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused about getSession