• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another mock exam question

 
madhav changala
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


My answer was C.. as request.getSession(false) returns null at the very first request.. as null.set/getAttribute() results.. NullPointerexception.. right? But the answer given is B..

Could any help on this?

ThanksInAdvance
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I guess that the answer is C!
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will go for B. Why because by default session(Page Directive)=true is created for JSP.
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhav, did you actually try this out??? I don't have a system to try it or otherwise I would have done it practically and checked!
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3674
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhav,
Did you see the detailed explanation that is provided with it?
 
Manju Devarla
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it prints Hello! no Nullpointer exception
 
achayya matta
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it prints hello for the first request and "1 hello" for the next request.
 
madhav changala
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did n't try out this one..

But there was no detailed answer...

Could any one from above tell how hello will be the correct answer?

ThanksInadvance
 
Rishi Chopra
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Since the question has not specified the page directive in the code snippet .. so the jsp container assumes that the page directive has session set to true.So a jsp page automatically participates in session tracking.
when
<% Integer count = (Integer) request.getSession(false).getAttribute("count");
is encounteres then the session is already there so "request.getSession(false)" wud return the session created implicitly by the page but there is no attribute named count so above statement is equal to Integer count=null;

then the code enters the else clause where the count is set as an attribute with value as new Integer(1) and then Hello is printed..

I hope i am able to clear your confusion

Regards
Rishi Chopra
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes rishi you are...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic