• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Doubt on a jsp file

 
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer would be :
B. There will have compilation error at //2 and //3.
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srinivasan,
Thanks.
With regards,
Padma priya N.G.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic