Show TOC

Procedure documentationUsing EL Implicit Objects Locate this document in the navigation structure

 

Implicit objects providing access to scoped variables

Implicit Object

Description

pageScope

Provides access to a collection of all page scope objects.

requestScope

Provides access to a collection of all request scope objects.

sessionScope

Provides access to a collection of all session scope objects.

applicationScope

Provides access to a collection of scoped variables from the application's scope.

Overview

The EL can be used directly in template text, be it inside the body of a custom or standard actions or in template text outside of any action. EL expressions also can be used in any attribute that can accept a run-time expression, be it a standard action or a custom action.

Types of EL Implicit Objects

Example Example

Implicit objects providing access to the context for the JSP page

Implicit Object

Description

pageContext

Provides access to the context for the JSP page.

To obtain the request URI, you use:

  1. ${pageContext.request.requestURI}
End of the code.

Example Example

Implicit objects providing access to objects

Implicit Object

Description

param

Maps a request parameter name to a single value.

paramValues

Maps a request parameter name to an array of values.

header

Maps a request header name to a single value.

headerValues

Maps a request header name to an array of values.

cookie

Maps a cookie name to a single cookie.

initParam

Maps a context initialization parameter name to a single value.

To obtain the String value of a parameter, you use the param object :

  1. ${param.employeeId}
End of the code.

Example Example

Implicit objects providing access to scoped variables

Implicit Object

Description

pageScope

Provides access to a collection of all page scope objects.

requestScope

Provides access to a collection of all request scope objects.

sessionScope

Provides access to a collection of all session scope objects.

applicationScope

Provides access to a collection of scoped variables from the application’s scope.

To obtain a session scoped attribute, you use the sessionScope object :

  1. ${sessionScope.shoppingcart}
End of the code.

Procedure

  1. Define the object or attribute you want to access.

    You can do this in the source of a JSP or servlet.

  2. Using the expression language syntax, request the required value.

Example

We want to set an attribute to the session with name user and value LocalAdministrator. This is done using the following code in the JSP file:

Syntax Syntax

  1. <%
       session.setAttribute("user", new User("LocalAdministrator"));
    %>
    
End of the code.

From now on, we can access the value using the following expression:

Syntax Syntax

  1. Hello ${sessionScope.user.name}!
End of the code.

which is equal to

Syntax Syntax

  1. Hello ${user.name}!
End of the code.

Or even

Syntax Syntax

  1. Hello ${user["name"]}!
End of the code.

For the user object we defined a special class User, so here is its source code:

Syntax Syntax

  1. public class User {
    
    	public User() {
    
    	}
    
    	public User(String name) {
    		this.setName(name);
    	}
    
    	private String name;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    
End of the code.

To define an array, we put it under some scope so the EL can recognize it:

Syntax Syntax

  1. <%
    	int[] numbersAttr={111,222,333,444};
    	request.setAttribute("numbers", numbersAttr);
    %>
    
    The third number of the array is: ${numbers[2]}
    
End of the code.

You can also use the EL inside custom tag attributes like this:

Syntax Syntax

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <c:set var="x" value="1"/>
    <c:out value="${x}"/>
    
End of the code.