Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session creation

 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When is a session created ?

What would be the result of request.getSession(false);
if this is the first request to this page and to the web application too.

Would a session be already created ? else it should return null ?

Please elaborate.

Thanks in advance
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A session is created when you create it.
HttpSession sess = request.getSession().
This is the line that creates the session.
With regards,
Padma priya N.G.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
request.getSession(false) will return null.
With regards,
Padma priya N.G.
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. However, my question is in context to the following question from a mock exam

8.Consider the following JSP code (See exhibit).What will it print for the very first request to this page as well as the web application that contains this page?
<html>
<body>
<% Integer count = (Integer) request.getSession(false).getAttribute("count");
if(count != null )
{ out.println(count); }
else
request.getSession(false).setAttribute("count", new Integer(1));%>
Hello!
</body>
</html>
Select 1 correct option.
A.It will print Hello!
B.It will print Hello and will set the count attribute in the session.
C.It will throw a NullPointerException at request time.
D.It will not compile.
ANS : B
If the session returns null then the answer should be C here. Any explainations ?
 
Sahid Khan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getSession(false) will return a session if there is any session associated with the requested, if not it will return null.

request.getSession() will create a session if there is no session associated with the request. request.getSession() is same as request.getSessin(true)
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swati said:
Thanks for your help. However, my question is in context to the following question from a mock exam

8.Consider the following JSP code (See exhibit).What will it print for the very first request to this page as well as the web application that contains this page?
<html>
<body>
<% Integer count = (Integer) request.getSession(false).getAttribute("count");
if(count != null )
{ out.println(count); }
else
request.getSession(false).setAttribute("count", new Integer(1));%>
Hello!
</body>
</html>
Select 1 correct option.
A.It will print Hello!
B.It will print Hello and will set the count attribute in the session.
C.It will throw a NullPointerException at request time.
D.It will not compile.
ANS : B
If the session returns null then the answer should be C here. Any explainations ?



Hi swati, in a JSP you have the implicit session variable available with you. So by the time you execute the statement "request.getSession(false).getAttribute("count");" you already have the session created. Note that there is no page directive
<%@ page session="false" %> as well. So session is available in JSP by default. If you put the above directive in the page then your answer is correct.

To get a clear idea see the java code generated for this jsp by the container.

Hope its clear for you now
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Amol, thats the answer I was looking for .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic