EL Property AccessS2C Home « EL Property Access

In the first two lessons of this section we have looked at the basics of EL by looking at syntax, naming rules, keywords, literals and operators. We can also use EL to access objects as well as attributes from any of the four scopes available. In this lesson we learn how to use EL to access object properties and attribute values from any scope.

EL expressions can return any type and so if the EL expression results in an object that has properties, these can be accessed using the . and [] operators.

The following code snippet shows the syntactical form of both operators:


${object.propertyName}

${object["propertyName"]}

The . OperatorTop

The . operator can be used to access JavaBean or java.util.Map objects.

The first named variable (before the . operator) is either an EL Implicit Object (more on these in the next lesson), or an attribute within one of the four scopes.

The second named variable (after the . operator) is either a property of the JavaBean or a key within tha java.util.Map object.

This variable must also conform to Java naming rules as discussed within the EL Naming Rules & Reserved Words section.

So within the following code snippet, aCat must be either a JavaBean or Map object and name must be either a property of the JavaBean or a key within the map.


${aCat.name}

Let's take a look at a more detailed code example using a java.util.Map object and the output produced. The following code extracts the values of the three entries in the java.util.HashMap using the name of the map as the first named variable (before the . operator) and the name of the key as the second named variable (after the . operator).


<%@ page import="java.util.Map, java.util.HashMap" %>

<%
   Map<String, String> hm = new HashMap<String, String>();
   hm.put("hello", "goodbye");
   hm.put("yes", "no");
   hm.put("high", "low");
   pageContext.setAttribute("hm", hm);
%>

${hm.hello} ${hm.yes} ${hm.high}

The above code produces a screen that looks like the following and as you can see the map values for each key are output:

run expr6jsp JSP

The [] OperatorTop

The [] operator can be used to access JavaBean, Array, java.util.List or java.util.Map objects and can also be used when the object we want to access does not follow Java naming conventions. This makes the [] operator a lot more powerful and flexible than the . operator.

The first named variable (before the [] operator) is either an EL Implicit Object (more on these in the next lesson), or an attribute within one of the four scopes.

The second named variable (after the [] operator) is either a property of the JavaBean, an index within the Array or java.util.List object, or a key within the java.util.Map object.

This variable doesn't have to conform to Java naming rules as discussed within the EL Naming Rules & Reserved Words section.

So within the following code snippet, aCat must be either a JavaBean, Array, java.util.List or java.util.Map object and name must be either a property of the JavaBean, an index within the Array or java.util.List object, or a key within the java.util.Map object.


${aCat["name"]}

Key ExtractionTop

The following code extracts the values of the three entries in the java.util.HashMap using the name of the map as the first named variable (before the . operator) and the name of the key as the second named variable (after the . operator). This is similar to the code above but we are trying to access the yes key value via a pageContext attribute named YES:


<%@ page import="java.util.Map, java.util.HashMap" %>

<%
   Map<String, String> hm = new HashMap<String, String>();
   hm.put("hello", "goodbye");
   hm.put("yes", "no");
   hm.put("high", "low");
   pageContext.setAttribute("hm", hm);
   pageContext.setAttribute("YES", "yes");
%>

${hm.hello} ${hm.YES} ${hm.high}

The above code produces a screen that looks like the following and as you can see the map value for YES isn't found because it doesn't exist within the map using the . operator, which will just use the value given for the key to directly read from the map.

run expr7jsp JSP

Now lets see what happens when we use the [] operator to try and access the yes key value via a pageContext attribute named YES:


<%@ page import="java.util.Map, java.util.HashMap" %>

<%
   Map<String, String> hm = new HashMap<String, String>();
   hm.put("hello", "goodbye");
   hm.put("yes", "no");
   hm.put("high", "low");
   pageContext.setAttribute("hm", hm);
   pageContext.setAttribute("YES", "yes");
%>

${hm["hello"]} ${hm[YES]} ${hm["high"]}

The above code produces a screen that looks like the following and as you can see the map value for YES has been found using the [] operator, which reads the value from the pageContext attribute named YES and uses the value to read the key from the map.

run expr8jsp JSP

Non-conforming VariablesTop

We can also use the [] operator to access variables that use non-conforming Java identifiers. Lets see what happens when we try this using the . operator.


${header.accept-encoding}

The above code produces an exception as shown by the following screenshot:

run expr9jsp JSP

So what happens when we try this using the [] operator.


${header["accept-encoding"]}

The above code produces a screen that looks like the following and as you can see this time there is no exception and the value of accept-encoding is output

run expr10jsp JSP

Chaining Property AccessTop

We can also access the property of an object, where that object itself has been returned as the property of another object. This can be done using either the [] operator or . operator, assuming there is no naming conflict. We can also mix 'n match the operators as shown by the following code example:


${pageContext.request.requestURL}<br>
${pageContext.request["requestURL"]}<br>
${pageContext["request"].requestURL}<br>
${pageContext["request"]["requestURL"]}

The above code produces a screen that looks like the following showing how the different mix 'n match examples all produce the same result:

run expr11jsp JSP

Lesson 3 Complete

In this lesson we learnt how to use EL to access object properties and attribute values from any scope.

What's Next?

In the next lesson we look at EL implicit objects and their usage.

go to home page Homepage go to top of page Top