What is the difference between .(dot) and [] in EL?

If person is a bean class and there is an attribute “name”. Then how to get the name using EL, ${Person.name} or ${person[“name”]}
There is some limitations with the dot operator. We can not access an array or a list through dot operator. When we use dot operator the firstthing can be only Map or a bean. But if we use [] then the firstthing can be a bean, Map, array or list.
Another thing if you want to use the secondthind a string like “com.foo.hi” then it can not be evaluated by dot operator because more dots are there which is meaningless.

Accessing an array through[]:
In servlet:
String[] music={“abcd”,”efgh”,”ijkl”};
RequestDispatcher rd=request.getRequestDispatcher("/Myemp.jsp");
rd.forward(request, response);

Music is:${musiclist[1]}

Accessing a list:
In servlet:
ArrayList<String> al=new ArrayList<String>();
RequestDispatcher rd=request.getRequestDispatcher("/Myemp.jsp");
rd.forward(request, response);
in JSP:
MUSIC IS:${musiclist}// because list has overridden toString() method and does not give output like al@768768
But if you want to access the individual elements of the list then you have to use the [] operator.
To access Map elements:
${musicmap.key} or ${musicmap[“key”]}

See the difference between ${musicmap[something]} and ${musicmap[“something”]}
The first to access Map element and second to attribute of bean.
The ${musicmap.pop} is same as ${musicmap[“pop”]}

No comments:

Post a Comment