*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in EL [] operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in EL [] operator" Watch "Doubt in EL [] operator" New topic
Author

Doubt in EL [] operator

Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Hello,

I have following code in my JSP page:



And, the output is
El1 =
El2 = 0000


Please help me why ${sessionScope[111]} prints nothing.

Thanks for help in advance.

SCJP6
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60975
    
  65

111 is not a string.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark heningen
Greenhorn

Joined: Mar 25, 2010
Posts: 25
are you sure about the output?

i got the following output with your code:
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Mark heningen wrote:are you sure about the output?

i got the following output with your code:


Probably, you printed ${sessionScope["111"]}?

Thanks
Mark heningen
Greenhorn

Joined: Mar 25, 2010
Posts: 25
No, i did copy paste.
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
I also got this as the result



Please rechek your code mark

when i did


then I got the answer as A22 , which prooves that attribute is getting successfully set.

So what i think is when you use an integer in [] operator with a Map then it does not look for the keys of the map , as map takes <Object,Object> and not primitive in the key. So, a primitive is not interpreted as key of the map and EL since gets nothing displays ""


Parth Tiwari
| Pursuing Bachelor of Engineering | OSUM Club Leader | SCJP 6 | SCWCD 5 |...
Mark heningen
Greenhorn

Joined: Mar 25, 2010
Posts: 25
here is code, which i am using. again it is copy paste.
Mark heningen
Greenhorn

Joined: Mar 25, 2010
Posts: 25
i did an extra print in code

and now output becomes
El1 = A22
El2 = 0000
El3 =
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
@mark

i copied your code and pasted in my jsp

and got the output as



which container are you using?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Mark which servlet container are you using?? The first expression should not have worked...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
with your new code
i got the output as

El1 =
El2 = 0000
El3 =

copy paste.

Are you sure you are using Tomcat 5 or some other version?

i think he is using different version of tomcat or some other container

and
@ankit do you know why is this first one not working?
Mark heningen
Greenhorn

Joined: Mar 25, 2010
Posts: 25
tomcat 6. i will try with another version later.
it is strange if this is container dependent.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

@ankit do you know why is this first one not working?

As Beer told in the first reply, while setting the attribute, we are using String "111", so retrieving it with ${sessionScope[111]} won't work. Its like doing this
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Mark, you're not hallucinating, I have the same result on Tomcat 6.


[My Blog]
All roads lead to JavaRanch
Mark heningen
Greenhorn

Joined: Mar 25, 2010
Posts: 25
can anybody explain this phenomenon? specially with container stuff. what should we do in exam, if output is going to be container dependent?
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
Its like doing this

1. session.setAttribute("111","Howdy");
2. session.getAttribute(111); //111 is Integer, so it will return null


Well this ofcourse will never work , getAttribute takes a string and what you supplied is an integer.

what will work is session.getAttribute("111");
that means session has a atrribute with name "111"

But in EL [] operator we can use integer , and if its not quoted , then el must look that map for the given key, when this string unqouted is supplied in sessionScope[111] it can look for a attribute named "111" in session scope.



Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1660
    
  25

Hi All,

This is an interesting topic, I remember this gave me a lot of headaches when I was studying.
what should we do in exam, if output is going to be container dependent?

Tomcat 6 is wrong in this case, maybe there is already a bug report...
But in EL [] operator we can use integer , and if its not quoted , then el must look that map for the given key,

True, but try this:

and in your jsp:

The output is also empty:

El7 =

The explanation (why this Integer key is also not evaluating) is found (somewhere far) in the specs:
JSP.2.9 Collected Syntax
The value of an IntegerLiteral ranges from Long.MIN_VALUE to Long.MAX_VALUE

An Integer literal in EL is actually treated as a Long, so if you write this:

the output will finally be:
El7 = succeeded


You can imagine getting a big headache!

Regards,
Frits
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Parth Twari wrote:
Its like doing this
session.setAttribute("111","Howdy");
session.getAttribute(111);


Well this ofcourse will never work , getAttribute takes a string and what you supplied is an integer.


I was trying to show you an example. The thing is for EL sessionScope is a map. Since the key in the Map is a String, you cannot get the value for that key using an integer (or long).

Tomcat behaves differently than the specs sometimes, but we have to choose the answer based on what the spec says (generally books also give facts from specs not what tomcat does)...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Yes, always refer to the specs for the exam. Do not trust 100% what your container is showing you.
Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183

I still do not get it.

Head First says that in case of [] operator if the argument is not a string literal its evaluated.
The container searches for an attribute bound under that name and substitutes its value.

So in case of El1 = ${sessionScope[111]} container will look for an attribute named 111 and
substitutes its value which is A22 and we have ${sessionScope["A22"]} . Now since
sessionScope is a Map it looks for a key A22. It finds one so should return the
corresponding value which is 0000. So should not the result be

El1 = 0000.


What a pity I am not able to understand till now.
Parth Twari
Ranch Hand

Joined: Jan 20, 2010
Posts: 163
@simiran

See this is exceptional case with maps and integer .

What head first says is for characters not integers so when you write


[] searches for attribute A11 and gives us "A22" value of attribute

we put it in
to give us 0000

But look here as frits said
El1 = ${sessionScope[111]} here [] operator will search map sessionScope for 111 long value

that means if this was legal then we would have got
${sessionScope[111]} to give us A22 in output.

But since this is never legal you cannot do this El1 = ${sessionScope[111]} to retrieve value "A22" or 0000

right frits?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1660
    
  25

But since session.setAttribute(new Long("111"), "A22");
this is never legal you cannot do this El1 = ${sessionScope[111]} to retrieve value "A22" or 0000
right frits?

Correct, this is because all the setAttribute() methods (on the 4 scopes) use a String as a key to store the object.

In my small example with the Long object as a key to the map called "mapje", you have to look somewhat deeper into the specs:
JSP.2.3.4
To evaluate expr-a[expr-b]:-
> mapje[111]
  • Evaluate expr-a into value-a
  • -> expr-a (mapje) evaluates into map stored in the pageContext
  • If value-a is null, return null.
  • Evaluate expr-b into value-b
  • -> expr-b (111) evaluates into a EL literal Long(111)
  • If value-b is null, return null.
  • If value-a is a Map, List, or array:

  • If value-a is a Map:
    -> this is true, map is a Map
    If !value-a.containsKey(value-b) then return null.
    -> this is false, map contains a key Long(111)
    Otherwise, return value-a.get(value-b)
    -> return map.get(111) = succeeded

    So the reason why a key with the Integer object doesn't work is because expr-b (111) evaluates into a EL "Integer" literal which is in fact a Long (111). Our map called "mapje" doesnot contain a key Long(111) (it does have a Integer(111)), hence it will return null

    The same reasoning applies to a key of type Float, you will have to use a Double to be able to access it in EL.

    Regards,
    Frits
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Doubt in EL [] operator