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

Problem in Mock exam

 
Gurukant Desai
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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?
 
Garlapati Ravi
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My answer is also C

what is the explanation they have given to justify answer B
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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
 
John Pearson
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic