Objective 2) EL, arrays and collections

7.2) Given a scenario, write EL code that uses the following operators: property access (the . operator), collection access (the [] operator).

Using dot and [] to access objects

The dot operator (.) is the default way of accessing objects within EL, thus I have shown how you can access param values with


But you can also access the map elements of implicit objects using square braces ([]), thus the first example given of


Can be reformulated as


Accessing Array elements

Square braces can of course be used to access elements of arrays but the EL goes further in its interpretation of array like expressions. And thus the following code will output the string cat six times.

String [] noises = {"cat","dog","cow"};

Note how the element offset can be accessed either with the naked number, with double quotes or with single quotes. The first three examples explicitly set the implicit object (requestScope) whereas the last three leave it undefined.

Remember that requestScope has a type of map and is not the same as the request implicit object available within JSP scriptlets. Thus the following script will work.

<%= request.getMethod() %>

But the following is not valid EL


It is possible to get access to the methods of the request via the pageContext. Thus the following code will access the HTTP method of the page.


Pay close attention to EL use of maps and arrays, it is not the same as standard Java

Accessing a parameter array

If you have the same HTTP query value multiple times the values can be accessed through the EL via the paramValues array. Thus the following query string


Results in the EL having access to an array called product id with two elements. The values in this array can be accessed either via


or the equivalent


The second version would also work correctly with double quotes around productid.

Accessing Map and List elements

The EL can access Map and List objects in a similar way

   java.util.HashMap noises = new java.util.HashMap();
   noises.put("human being","speech");
<!-- the scope can be center implicit -->
square braces allows the use of names that
cannot be used for Java objects, i.e. with
 spaces or dots in them 
${requestScope.noises["human being"]}

The following code demonstrates how accessing the elements of a List is similar to access array elements.

   /* accessing List elements with the EL */
   java.util.ArrayList noises = new java.util.ArrayList();
   noises.add("human being");
<!-- note how bare numbers or numbers with quotes -->

You cannot use the dot notation with arrays or Lists, thus the following would cause a compile time error.

${noises.3}<!--will not compile -->

Accessing Bean Properties with EL

EL expressions can access properties of JavaBeans in a similar way to accessing elements of collections. Given the following code as a JavaBean.

package com.examulator;
public class Name {
   private String firstName, lastName;
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   public void setLastName(String lastName) {
      this.lastName = lastName;
   public String getFirstName() {
      return firstName;
   public String getLastName() {
      return lastName;

The bean can be instantiated within a JSP and its values can be accessed using EL syntax as follows.

  <jsp:useBean id='name' class='com.examulator.Name'>         
         <jsp:setProperty name='name'
                      property='firstName' value='Marcus'/>
         <jsp:setProperty name='name'
                      property='lastName'  value='Green'/>

The bean is instantiated with the useBean tags, the properties are set with the setProperty tags. Note how the code uses three different ways of accessing the bean values. Behind the scenes the code is translated into a call to the getName method of the JavaBean. The output from this code will be the string Marcus output three times.

Other Sources

According to Mikalai Zaikin

Last modified: Sunday, 20 September 2015, 07:21 PM