Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on a jsp file

 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which statement is TRUE about the following jsp code snippet?
Please select one correct answer.
<% String theKey = "key";
String theValue = "value";
session.removeAttribute(theKey); //1 %> session.setAttribute("<%= theKey %>","<%= theValue %>"); //2 session.getAttribute("<%= theKey %>"); //3
<%= session.getAttribute(theKey) %> //4

A. The code compiles but might have runtime NullPointerException at //1
B. There will have compilation error at //2 and //3.
C. There will have output as null at //4.
D. There will have output as theValue at //4

The answer is option C.

Source: JavaBeat.com
Can you tell me as to how the answer is C.
With regards,
Padma priya N.G.
 
Nilesh Patel
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer would be :
B. There will have compilation error at //2 and //3.
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
at 2, the code sets the attribute "<%= theKey %>" with the value "<%= theValue %>", without reading the value of theKey and theValue. So you have really a key "<%= theKey %>" and a value "<%= theValue %>": for this reason, at 4 you get a null value, because the key is the value of "theKey", I mean "key".
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mirko,
I did not get exactly the answer. What is the option that you have chosen?
With regards,
Padma priya N.G.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi priya,

Output looks like following:

session.setAttribute("key","value"); //2
session.getAttribute("key"); //3
null //4

So answer is C. no doubt.
This question has good traps on every line.
Good one.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivasan,
How can the output at //4 be null? Can you throw some light on this?
With regards,
Padma priya N.G.
 
naresh govindaswmay
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by padmapriyagururajan priya:
Which statement is TRUE about the following jsp code snippet?
Please select one correct answer.
<% String theKey = "key";
String theValue = "value";
session.removeAttribute(theKey); //1 %> session.setAttribute("<%= theKey %>","<%= theValue %>"); //2 session.getAttribute("<%= theKey %>"); //3
<%= session.getAttribute(theKey) %> //4

A. The code compiles but might have runtime NullPointerException at //1
B. There will have compilation error at //2 and //3.
C. There will have output as null at //4.
D. There will have output as theValue at //4

The answer is option C.

Source: JavaBeat.com
Can you tell me as to how the answer is C.
With regards,
Padma priya N.G.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priya,

I think you know key has been deleted before executing getAttribute.

Here is the API for HttpSession.getAttribute

getAttribute
public java.lang.Object getAttribute(java.lang.String name)

Returns the object bound with the specified name in this session, or null if no object is bound under the name.

Parameters:
name - a string specifying the name of the object

Returns:
the object with the specified name

Throws:
IllegalStateException - if this method is called on an invalidated session
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivasan,
Thanks.
With regards,
Padma priya N.G.
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 1 is a scriptlet
It tries to remove an attribute that does not exist. No null pointer exception.

Line 2 is not a scriptlet
This is just printing stuff to the response. The setting of the attribute does not take place because it is treated as plain text.

Line 3, same as line 2. treated as plain text.

Line 4 is an expression. It tries to do a getAttribute("key"). So far no attribute has been set to the session, so you will get null.

I had to look at the generated java class to finally figure it out, this definitely is a tricky one. What I am working on now is if lines 2 and 3 are moved into scriptlet, what would be the result. I am having some trouble because of the nesting/matching of <% stuff.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic