aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes could not understand the following EL Expressions? 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 "could not understand the following EL Expressions?" Watch "could not understand the following EL Expressions?" New topic
Author

could not understand the following EL Expressions?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

please consider the following 2 EL expression from hfsj 2nd edition, final mock exam, question 7, page 831.

${cookie["JSESSIONID"]["value"]}
${cookie.JSESSIONID["value"]}

the expression are valid and as i have tested prints the session id. but i couldnt understand the syntax. i marked these 2 answers as wrong. how are they correct ? please help
regards
gurpreet
Vijay Vishwa
Greenhorn

Joined: Nov 21, 2010
Posts: 28
cookie is a jsp implicit object of type Map.
What it does is in the cookie map JSESSIONID key's value is printed.
Also following could also be one of the way


SCJP 5.0 OCBCD 5.0(Formerly SCBCD)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Vijay Vishwa wrote:cookie is a jsp implicit object of type Map.
What it does is in the cookie map JSESSIONID key's value is printed.
Also following could also be one of the way


thanks for the reply vijay but i did not understand the syntax . i do know that cookie is a el implicit object(not jsp implicit object) and it is a map of cookie name and cookie object, what i did not understand is the syntax
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1675
    
  25

With a Map, there are three possibilities to get an item b from Map a:

1. ${a.b} where b follows the rules of an identifier
2. ${a["b"]} or ${a['b']}
3. ${a[c]}, where c is key of the Map of type String, Long or Double

example:

<%
HashMap map = new HashMap();
map.put(new Long("111"),"succeeded");
map.put("stringkey","Stringvalue");
map.put("@notjavaidentifier", "blablabla");
session.setAttribute("a", "stringkey");
pageContext.setAttribute("mapje",map);
%>

${mapje["stringkey"]}
${mapje.stringkey}
${mapje[111]}
${mapje[a]}
${mapje["@notjavaidentifier"]}

will give the following output:
Stringvalue Stringvalue succeeded Stringvalue blablabla


N.B. ${mapje.@notjavaidentifier} won't work because @notjavaidentifier is not a valid identifier
N.B. ${mapje[111]}, where 111 is key of the Map of type Byte, Short, Integer (or Float) will evaluate into an empty String. The reason behind this is that an EL literal is a Long (or a Double). EL will check mapje.containsKey(Long(111)), and that is not true, hence null is returned.

Regards,
Frits
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1675
    
  25

Hi Gurpeet Singh,

I just realized that I forgot to answer your question

cookie is indeed an implicit Map, and to be precise it is a Map<String, Cookie> where String contains the "name"of the Cookie (i.e. Cookie.getName()) and Cookie is the Cookie object.

With ${cookie['JSESSIONID'].value} you are accessing the Map with the name "JSESSIONID" (i.e. Map.get("JSESSIONID")) and you are getting the specific Cookie with that name.
From that cookie you need the value (Map.get("JSESSIONID")).getValue().

So these are all equivalent:

Regards,
Frits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: could not understand the following EL Expressions?