Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

2.3 Identifiers

Identifiers in the expression language represent the names of objects stored in one of the JSP scopes: page, request, session, or application. Those types of objects are referred to throughout this book as scoped variables.

When the expression language encounters an identifier, it searches for a scoped variable with that name in the page, request, session, and application scopes, in that order; for example, the following code fragment stores a string in page scope and accesses that string with an EL expression:

<% // Create a string
   String s = "Richard Wilson";

   // Store the string in page scope
   pageContext.setAttribute("name", s); %>

<%-- Access the string with an EL expression --%>
<c:out value='${name}'/>

In the preceding code fragment, the expression ${name} resolves to a reference to the string named name that was placed in page scope by the scriptlet. That reference is specified for the <c:out> action's value attribute. When the <c:out> action is confronted with an object reference for its value attribute, it coerces that object to a string by invoking its toString method, which in this case produces the value of the string, so the output of the preceding code fragment is Richard Wilson. If the name string had been placed in a different scope, the expression ${name} would still resolve to that string, as long as there was not another object with the same name in another scope that was searched first. For example, if two objects named name are stored in request and application scopes, the expression ${name} would resolve to the object stored in request scope.7

Identifiers must adhere to the syntax for Java programming language identifiers; for example, you cannot use characters such as - or / in an identifier.

The two sections that follow—"Accessing JavaBeans Components" and "Accessing Objects Stored in Arrays, Lists, and Maps" on page 52—illustrate how to use identifiers to access beans and collections, respectively, that are stored in JSP scopes.

  • + Share This
  • 🔖 Save To Your Account