Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about getSession

 
Reidar Gjerstad
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think request.getSession(true) is equivalent to request.getSession().
correct me if I am wrong.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


Yes . you are right.
 
Reidar Gjerstad
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9579
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 9579
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is not that default creation signalized by HttpSessionListener?
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic