[an error occurred while processing this directive]

Using [ ] operator of EL with the Map

                          

EL expressions are always used within curly braces, and prefixed with a dollar($) sign and $(expression). The first named variable in the expression is either an implicit object or an attribute stored in one of the the four scopes i.e. page, request, session, or application. The [] operator is much more powerful than the dot, because it lets you access Map key or a bean property name as well as the access to arrays and Lists.

Map stores the values in the form of a key- value pair. Map can't have a duplicate keys, and each key is associated with at least one value. In map we can retrieve the value by using ${map["0"]}.

We have made one program on map by using the EL . We are retrieving the values of a map firstly by setting the map attribute and thereafter retrieving the value by using the key associated with the map.

The code of the program is given below:

 

<%@ page import = "java.util.*"%>
<% Map map = new HashMap();
map.put("name", "Tim");
map.put("address", "200,Saint Thomas Loop");
map.put("state", "Virginia, USA");
request.setAttribute("useMap", map);
request.setAttribute("userName","name");
%>
<body>
<center>
<table>
<tr><td><b>The name is :</td></b> <td><b>${useMap[userName]}</tr></td></b><br>
<tr><td><b>The first element in Map is :</td></b> <td><b>${useMap["name"]}</tr></td></b><br>
<tr><td><b>The second element in Map is : </td></b> <td><b>${useMap["address"]}</tr></td></b><br>
<tr><td><b>The third element in Map is : </td></b> <td><b>${useMap["state"]}</tr></td></b><br>
</table>
</center>
</body>

The output of the program is given below:

Download this example.

                          

[an error occurred while processing this directive]