
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
|
Implicit Object |
Description |
|---|---|
|
pageContext |
Provides access to the context for the JSP page. |
To obtain the request URI, you use:
${pageContext.request.requestURI}
|
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 :
${param.employeeId}
|
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 :
${sessionScope.shoppingcart}
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:
<%
session.setAttribute("user", new User("LocalAdministrator"));
%>
From now on, we can access the value using the following expression:
Hello ${sessionScope.user.name}!
which is equal to
Hello ${user.name}!
Or even
Hello ${user["name"]}!
For the user object we defined a special class User , so here is its source code:
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;
}
}
To define an array, we put it under some scope so the EL can recognize it:
<%
int[] numbersAttr={111,222,333,444};
request.setAttribute("numbers", numbersAttr);
%>
The third number of the array is: ${numbers[2]}
You can also use the EL inside custom tag attributes like this:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="x" value="1"/>
<c:out value="${x}"/>