The JSP 2.0 expression language lets you access different types of collections in the same way: using array notation. For instance, if attributeName is a scoped variable referring to an array, List , or Map , you access an entry in the collection with the following: ${attributeName[entryName]} If the scoped variable is an array, the entry name is the index and the value is obtained with theArray[index] . For example, if customerNames refers to an array of strings, ${customerNames[0]} would output the first entry in the array. If the scoped variable is an object that implements the List interface, the entry name is the index and the value is obtained with theList.get(index) . For example, if supplierNames refers to an ArrayList , ${supplierNames[0]} would output the first entry in the ArrayList . If the scoped variable is an object that implements the Map interface, the entry name is the key and the value is obtained with theMap.get(key) . For example, if stateCapitals refers to a HashMap whose keys are U.S. state names and whose values are city names , ${stateCapitals["maryland"]} would return "annapolis" . If the Map key is of a form that would be legal as a Java variable name, you can replace the array notation with dot notation. So, the previous example could also be written as: ${stateCapitals.maryland} However, note that the array notation lets you choose the key at request time, whereas the dot notation requires you to know the key in advance. An ExampleTo illustrate the use of EL expressions to access collections, the Collections servlet (Listing 16.8) creates an array of strings, an ArrayList , and a HashMap . The servlet then forwards the request to a JSP page (Listing 16.9, Figure 16-4) that uses uniform array notation to access the elements of all three objects. Figure 16-4. You use array notation or dots to access collections. Dots can be used only when the key is in a form that would be legal as a Java variable name.
Purely numeric values are illegal as bean properties, so the array and ArrayList entries must be accessed with array notation. However, the HashMap entries could be accessed with either array or dot notation. We use array notation for consistency, but, for example, ${company["Ellison"]} could be replaced with ${company.Ellison} in Listing 16.9. Listing 16.8 Collections.javapackage coreservlets; import java.util.*; /** Servlet that creates some collections whose elements will * be displayed with the JSP 2.0 expression language. * <P> */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Collections extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] firstNames = { "Bill", "Scott", "Larry" }; ArrayList lastNames = new ArrayList(); lastNames.add("Ellison"); lastNames.add("Gates"); lastNames.add("McNealy"); HashMap companyNames = new HashMap(); companyNames.put("Ellison", "Sun"); companyNames.put("Gates", "Oracle"); companyNames.put("McNealy", "Microsoft"); request.setAttribute("first", firstNames); request.setAttribute("last", lastNames); request.setAttribute("company", companyNames); RequestDispatcher dispatcher = request.getRequestDispatcher("/el/collections.jsp"); dispatcher.forward(request, response); } } Listing 16.9 collections.jsp<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD><TITLE>Accessing Collections</TITLE> <LINK REL=STYLESHEET HREF="/el/JSP-Styles.css" TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Accessing Collections </TABLE> <P> <UL> <LI> ${first[0]} ${last[0]} ( ${company["Ellison"]} ) <LI> ${first[1]} ${last[1]} ( ${company["Gates"]} ) <LI> ${first[2]} ${last[2]} ( ${company["McNealy"]} ) </UL> </BODY></HTML> |