• 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

EL and Exceptions, Clarification required

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this out:-

I made a Person class with:-

private String name;
private Dog dog;

I made appropriate setter and getters for all properties.

Here Dog is another class that we dont need to worry bout right now.

The servlet creates a Dog object and a Person object and sets the Dog object as the persons dog. Sets the name as something. assigns the Person as a request attribute "person" n forwards the request to a JSP.

now in the JSP :-


${person.name} prints the person objects correct name.
${request.person.name} prints the person objects correct name.

Now this is where it gets interesting:-

${person.age} throws exception, because Person has no 'age' property
${person["age"]} throws exception, because Person has no 'age' property

Now it gets more interesting:-

${request.person.age} it prints nothing.
${request.person["age"]} it prints nothing.

So how is that possible, the behavior is not consistent, its throwing an exception when I dont use the request scope implicit object???

Any suggestions??
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

${request.person.age} it prints nothing.
${request.person["age"]} it prints nothing.



The above lines print nothing, as the request is not valid implicit Object in the EL. It treated simply as non-existant identifier and the above expression returns null. So fo null value it does not print anything.

It you really test the behavior of non-existant properties Test:

${requestScope.person.age}
${requestScope.person["age"]}

This wiil search the person bean in Request Scope and throw Exception for non-exitstant property of the person bean.

Hope this help you.

Thanks
 
Liyaquat Ali
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Narendra,

Thanx for the answer, it has been a gr8 help


I tried another thing...

${requestScope.X} prints nothing... (as there is no property named X)
${requestScope.Scope.X.Y} prints nothing... (as there is no property named X)
${X} prints nothing... (as there is no property named X)
${X.Y} prints nothing... (as there is no property named X)


but is there is a attribute named SomeThing which does not have a Property P this is wat happens:-

${Something.P} will throw a Exception

So if EL finds an attribute but does not find the required property then an exception is thrown, if the attribute is not found altogether then no exception is thrown.
[ March 02, 2006: Message edited by: Liyaquat Ali ]
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If the attribute is not found,i.e. it is null. So it print nothing.
When you use Something.P, If either Something or P is null the expression is evaluated to null.

Thanks
 
Liyaquat Ali
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx again Narendra!
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When you use Something.P, If either Something or P is null the expression is evaluated to null.


Narendra, do you mean if "Something" is a valid attribute and it doesn't have a property named "P", ${Somthing.P} will print out nothing and no exception is thrown? Just want to make sure. I got confused after reading the posts.

Thank you.
[ March 05, 2006: Message edited by: Xiaoxiao Lam ]
 
Liyaquat Ali
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No its not like that....

read my post above where I mentioned what I tried out.

This is the way it wox:-

Assume EL is expression is ${Something.P}

In this case if Something is an attibute in any scope AND if it does NOT have a property named P then a Exception is thrown.

Also, if there is no property named Something in any scope then no Exception is thrown and nothing is printed.

I know its confusing but try it out UrSelf, write a couple o beans n a few EL lines in a JSP n C wat happens
 
Xiaoxiao Lam
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Thank you!
 
Xiaoxiao Lam
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused now again when I read P447 on HFSJ where it says ${fooBean.notAProperty} won't cause an exception. Anyone can explain? Thank you.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic