File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes EL Qz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "EL Qz" Watch "EL Qz" New topic
Author

EL Qz

liqiang yang
Ranch Hand

Joined: Jan 20, 2008
Posts: 92
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.


DY.
SCJP 5.0 (100%), SCWCD 5.0 (79%), SCBCD 5.0 (preparing...)
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Please refer page 399 of HFSF(Head First Servlets & JSP), 2nd Edition.

Thanks & regards,

Naveen Katoch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I can't understand it.

What exactly don't you understand ? 1 ? 2 ? 3 ?


[My Blog]
All roads lead to JavaRanch
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
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

Joined: Mar 18, 2008
Posts: 591
    
    1

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 ]

Most of the programming problems can't be solved because you don't know what the exact problem is.
Defining your problem correctly leads you to the right solution.
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
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

Joined: Jan 20, 2008
Posts: 92
Thanks ranchers!!!

I make sense and feel better now.
 
wood burning stoves
 
subject: EL Qz