This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL Qz

 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

What will happen when you attempt to compile and run the following JSP page contents?

<%
request.setAttribute("Two","2");
Integer One = new Integer(1);
%>

${One + 1}
${Two}
${Two + 1}

The answer is: 123
I can't understand it. Can anybody figure it out? Thanks alot.
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer page 399 of HFSF(Head First Servlets & JSP), 2nd Edition.

Thanks & regards,

Naveen Katoch
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand it.

What exactly don't you understand ? 1 ? 2 ? 3 ?
 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by liqiang yang:
Hi all,

What will happen when you attempt to compile and run the following JSP page contents?

<%
request.setAttribute("Two","2");
Integer One = new Integer(1);
%>

${One + 1}
${Two}
${Two + 1}

The answer is: 123
I can't understand it. Can anybody figure it out? Thanks alot.


$(One + 1) : evalauates to 0 + 1 = 1 ( Attribute with name "One" could not be found in any of the scope, so EL consider it as 0 in arithmetic operation) (considered as false in logical operation)

${Two) : it is simpe. attribute names "Two" is found in scope and its value is 2.

${Two + 1} : Simple.Value of attribute "Two" (2) + 1 = 3

so it prints: 123 (There are no line breaks)

Regards

Prem Kashyap
[ May 14, 2008: Message edited by: Prem Kashyap ]
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by liqiang yang:

<%
request.setAttribute("Two","2");
Integer One = new Integer(1);
%>

${One + 1}
${Two}
${Two + 1}

The answer is: 123
I can't understand it. Can anybody figure it out? Thanks alot.


Well,
1)One is evaluated to null so null + 1 is 1 (I am not sure ,sorry)
2)Two is String and evaluated to primitive (if you put "rft" instead of
"2" you will get big fat exception)
3)Two("2") is evaluated to 2 and 2+1 = 3

As to 'One',I am not sure so other ranchers will take care of it

best regards,
omi
[ May 14, 2008: Message edited by: omi sharma ]
 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said in my previou post, null is evaluated as 0 in arithmetic operation (+,- etc) and evaluated as false in logical operation(&&,|| etc)). (For relational operation, like <, >, >= etc , i am not sure if it evaluated to 0 or false. my guess it evaluated to false. Just write a small program and find out.

Regards
Prem Kashyap
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ranchers!!!

I make sense and feel better now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic