• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HFSJ book - Chapter 8 Q 17

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which about EL access operators are true?

A. Anywhere the . (dot) operator is used, the [] could 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. There are some situations where the. (dot)operator must be
used and other situations where the [] operator must be used.

The book answer is A

Is C is not true ?

Thanks
Kathir
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In EL, Null is user friendly. That would mean if bean property does not exist then empty string is printled. Exception is not thrown.
 
Kathiresan Chinna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was understanding the statement C as:

${emp.midName}
here, 'emp' is exist but it does not contain 'midName' property.

So, Is the statement meant by, the actual bean 'emp' does not exist ?

Thanks
Kathir
 
Ashu Jain
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was understanding the statement C as:

${emp.midName} 'midName' property
here, 'emp' is exist but it does not contain .


Statement C meant that 'midName' property does not exist.

Goldern Rule of EL is that - It handles unknown or null values so that the page still displayseven if it can't find a property/attribute/key with the name in the expression.

There will not be any exception even if emp is null or does not exist. you will just see blank output

I hope that helps
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashu,

If you refer Kathy's book second edition page no.370 you will find there is a statement in bold which says "If the object is a bean but the named property doesn't exist, then an excpetion is thrown". Then I guess 'C' should be one of the answers.Please let me know if I am wrong.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kathir is correct.

In the errata for the book it says that the answer for Question 17 on Page 437 should include "C" as one of the answers.

I'm sorry I don't have the link to the errata handy, just a printed copy. But it's a very useful thing to have.
 
Ashu Jain
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys...i am confused now.

I am refering HFSJ 1st Edition that says C is correct answer(Pg 432) but Erata of the book
"http://oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed"

says this: -

[432] Question 17, Option C;
option C is invalid and should not be checked.

Can someone please clarify rules related to null key/property in an EL expression.

My understanding is as i said earlier as well -

EL handles unknown or null values gracefully - no exception

Eg- ${foo} - If foo attribute does not exist in any scopes then print nothing
${foo.bar} - If foo Bean/Map exist but if bar does not exist as property than also print nothing
 
Kathiresan Chinna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a bean in requestScope called "emp"
But, it does not have "midName" property.

In JSP, ${emp.midName} - throws exception

Thank you
Kathir
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Renu,

Try using : ${emp["midName"]} and please tell me the result.
 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Plese let me know

if empl.property and if property is not define on empl, a exception is thrown???
 
Kathiresan Chinna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

This is what happened

1. The bean 'emp' is not exist in any scope:
${emp.firstName} - prints nothing
${emp.firstName1} - prints nothing
${emp["firstName"]} - prints nothing


2. The bean 'emp' is exist in requestScope,
but it does not have "firstName1" property

${emp.firstName1} - throws javax.el.PropertyNotFoundException
${emp["firstName1"]} - throws javax.el.PropertyNotFoundException


Kathir
 
Vishal Chugh
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Renu.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kathir, I had this doubt too...
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic