• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access to a property of JavaBean

 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rancher
I have a doub, and right now, I cant test this by my self, maybe someone knows about this:
In the book: HFJS, chapter 8 question 17 says:

Which about EL Access operators are true= (Choose all that apply)
A. Anywhere the . (dot) operator is used, the [ ] colud be used instead.
B. Anywhere the [ ] operator is used, the . (dot) could be used instead.
C. If the . (dot) operator is used to Access a bean property but the property doesn’t exist, then a runtime exception is thrown.
D. …

The correct answer that give the book is, only A.
But, I can´t remember if previosly on chapters, says that if you Access to a property that no exist on a JavaBean, then will thrown a exception.
Can someone helpme with this.

Thankyou
 
Rob Spoor
Sheriff
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B is wrong because with [] you can use keys with spaces, dashes or other characters in them that are not allowed for variable names; for instance, if the key is "first-name" then is valid, while will be treated as the value of "name" subtracted from "myValue.first".

C is wrong because EL simply outputs nothing if a property does not exist, or even if the object for which to get the property is null.
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...then something is wrong.

In the book HFSJ on page 370 say:



I dont get it, I know the best thing i can do, is test my self, but right now i cant.

someone that opine about that.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i am glad you asked it. Even i was confused over that. But what Rob says is also right as the HFSJ book mentions that and that is the case in reality.
But the book seems to contradict itself here Or not???
Is it an HFSJ errata? (Some of the answers in the final mock are also wrongly marked in the book.)
 
Rob Spoor
Sheriff
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the SCWCD study guide which does not mention throwing an exception at all.
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I am very confuse right now, this night I will test my self, and I will comment the result.

 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HFSJ errata:
[437] Question 17;
Regarding the usage of the dot "." operator, the last comment on page 370 states, "If the object is a
bean but the named property doesn't exist, then as exception is thrown? However, the answer to question
17 on page 437 does not reflect this as being true as 17-C is not checked.


Added a checkmark on Option C


Errata link
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I checked and it throws an exception if the bean's named property being invoked by the dot operator does not exist.

But then is this statement on page 395 of HFSJ not contradictory:

EL is null friendly. It handles unknown or null values so that the page still displays, even if it can't find an attribute/property/key with the name in the expression.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I performed some tests and here are the findings:



HelloUser Servlet code:



Hello.jsp:



On invoking HelloUser servlet from the browser and passing say username=Harvinder as the parameter will execute LINE#1 to display Harvinder,
LINE#2 to display nothing,
LINE#3 to display nothing,
LINE#4 will display nothing,
and if LINE#5 is uncommented then it will throw an exception saying the property unknown does not exist.

1. The only difference between LINE#3 and LINE#5 is the usage of [] and dot(.) operator on an attribute name which EXISTS in the request scope. The [] operator is lenient but dot is not.

2. LINE#4 uses the dot operator on an attribute which does NOTexist in any scope, and here the dot operator is lenient and displays nothing.


 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very, very thanks Harvinder,

then. a exception will thrown

Very thank you again
 
gurneeraj singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What wil happen if Line#3 will be ${user["unknown"]} instead of ${user[unknown]}. I think it will again throw an exception.
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurneeraj, thank you for ask that, I have the same doub.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Gurneeraj.. thanks for pointing it out. you are right....
user["unknown"] will throw an exception. but user[unknown] will not.
I guess it implies that whenever an NON existing attribute is evaluated then both dot and [] operator are lenient.
But when evaluating a NON existing property both the operators are NOT lenient. as in

${nonexistingattribute.somenonexstingproperty} --> OK
${nonexistingattribute.[somenonexstingproperty]} --> OK Here technically speaking it should be
somenonexistingattribute
and NOT
somenonexstingproperty
as the expression without quotes
is evaluated as an attribute named
somenonexstingproperty
and replaces the name with its
actual value.
${existingattribute.[somenonexstingattribute]} --> OK

but
${existingattribute.somenonexstingproperty} --> Exception
${existingattribute.["somenonexstingproperty"]} --> Exception
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic