• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Session creation

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Amol, thats the answer I was looking for .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic