• 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

Problem in Mock exam

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I just found this question in javabeat's mock-3

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

they have given answer as B but I think answer is C.
Reason :- I think answer C is correct because of the fact that request made by user is "very first request" and so session might not be available.
When session is not available then request.getSession(false) will return "null" and calling any method on null will result into null pointer exception.

Please reply whether I am right/wrong?
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answer is also C

what is the explanation they have given to justify answer B
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

The same for me... I think you're both right and the answer should be C. The documentation for getSession() clearly says

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



Have you already tried this in a real application?

Marco
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I rememembered what was wrong

A HTTP session is created by default! You have to specify it in the page directive if you don't want a session to be created:


Marco
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marco's right. If you look at the servlet source code generated for your JSP page, you'll see that the session implicit variable is, by default, initialized by:

which will create a new session if one doesn't exist already.

Tricky question, though. I like it.
 
Gurukant Desai
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marco/John for the explaination.... It was really a tricky one!!!
[ May 20, 2008: Message edited by: Gurukant Desai ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic