Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Question on HttpSession

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

<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>

Given answer: It will print Hello and will set the count attribute in the session.

According to me, since the parameter to request.getSession() is false in both the if and else clause(s), a session will not be created at all.
Shouldn't it throw a NullPointerException?
 
author
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without trying the code... I think you will get a session by default. Unless you explicitly set a page directive in the JSP to say session="false", then the very beginning of the generated _jspService() method will obtain a session object. So when you use request.getSession(false) later in the code, there will be a session object there.

I'll try the code out now!

HTH,

David.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kedar,

I don't quite get your code snippet. Does the count attribute get set in the session somewhere else before you access it from jsp page?

Thanks,
Jenny
 
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The answer is correct it will print hello. This is because the implicit session object will be present and because of that it won't throw NullPointerException.

On the other hand if you include "<%@page session="false"%>" in your jsp then we are saying that not to make the implicit session object available and that's why in this case it will throw NullPointerException.

Hope this helps.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so in this code... if we don't include this "<%@page session="false"%>
will it put session automatically even the page is viewed v.first time ?

how does it work is it like it will create a newsession before the page code runs and
when we reach the line request.getSession(false) ..
it will return only previos session and don't create new session...

so here the implicit session created is used with code getAttribute("count"); and as there is no "count" element ...it returns null


so no runtime error... is my understanding is right ?

pls reply
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so in this code... if we don't include this "<%@page session="false"%>
will it put session automatically even the page is viewed v.first time ?



Yes

so here the implicit session created is used with code getAttribute("count"); and as there is no "count" element ...it returns null so no runtime error... is my understanding is right



Yes you are right. When the page is visited first time the implicit object will be made available and request.getSession(false) will return the session object.

Hope it helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic