Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tomcat with [] and . operator

 
Raoul Kalk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Tomcat 5.5.26-5 as my webcontainer and on the issue of the [] and . operator it behaves different from what you might expect from the book.

This is my situation: I have a HashMap object called myMap on the request object. Het map has a key-value pair set by

myMap.put("key1","value1")

Accessing this value from a jsp can be done in several ways:

${myMap["key1"]}
${myMap.key1}

but also: ${myMap[key1]}

But in chapter 8, test question 7 it says that ${foo.bar} is not the same as ${foo[bar]}. The question doesn't mention which type foo is. Suppose it's ArrayList, then yes, it's different. But if foo is a Map it seems to work fine (under Tomcat). So maybe, stricly speaking ${foo.bar} is not the same as ${foo[bar]} as it is not always true.

Edit: But then in case foo is an array, ${foo.bar} is not equivalent to ${foo["bar"]} either, which the book claims it is.

Is there some logic I'm missing here?

 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${foo[bar]} first looks for an attribute called bar, then uses its value to look up a property or map entry of an attribute called foo.

Both ${foo.bar} and ${foo["bar"]} look for a property or map entry called "bar" of an attribute called foo.

The difference between ${foo["bar"]} and ${foo[bar]} is that the first uses the string literal "bar", whereas the second uses another attribute's value.
 
Raoul Kalk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:${foo[bar]} first looks for an attribute called bar, then uses its value to look up a property or map entry of an attribute called foo.

Both ${foo.bar} and ${foo["bar"]} look for a property or map entry called "bar" of an attribute called foo.

The difference between ${foo["bar"]} and ${foo[bar]} is that the first uses the string literal "bar", whereas the second uses another attribute's value.


You're right! My testcase was not correct (had a typo). In the case of ${foo[bar]} where a bar attribute can not be found, it just prints NULL (no exception thrown as it is simply a 'failed' map lookup). And if bar was found, it will evaluate to that value.

Thanks again for getting into this, you made me look closely at the code again. Error found, my error, not tomcat's
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But then in case foo is an array, ${foo.bar} is not equivalent to ${foo["bar"]} either, which the book claims it is.


The book says,
${foo.bar} is equivalent to ${foo["bar"]}

and NOT
${foo["bar"]} is equivalent to ${foo.bar}

So the first case is always TRUE because the DOT operator supports a MAP or a BEAN which is also supported by the [] operator.

But the second case is FALSE because DOT operator does NOT support lists and arrays.


Accessing this value from a jsp can be done in several ways:

${myMap["key1"]}
${myMap.key1}

but also: ${myMap[key1]}


${myMap[key1]} is very much different from ${myMap["key1"]} and ${myMap.key1}

${myMap[key1]} means find an attribute named *key1*. Then use the value of that attribute as the key into the Map, or return null. If myMap were a bean then the value of a property with the name equal to the value of *key1* attribute would be returned.

 
Raoul Kalk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accessing this value from a jsp can be done in several ways:

${myMap["key1"]}
${myMap.key1}

but also: ${myMap[key1]}


${myMap[key1]} is very much different from ${myMap["key1"]} and ${myMap.key1}

${myMap[key1]} means find an attribute named *key1*. Then use the value of that attribute as the key into the Map, or return null. If myMap were a bean then the value of a property with the name equal to the value of *key1* attribute would be returned.



I have seen the difference. There was small typo in my source which was a bit confusing. Thanks for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic