Fixed values are all well and good, but the true strength of the EL lies in expressing dynamic values. These values can be generated from a number of sources, but are always represented by scoped variables.
The concept of a variable should be familiar: a named element that represents a dynamic value that can be assigned to it. But the concept of the JSP scopes may be new.
These scopes define the order in which variable names are resolved, the lifetime of the variable, and its purview. The scopes, in their default search order, are: page, request, session and application.
In Java code, these scoped variables are created by the setAttribute() method of their respective Java classes. These classes (or interfaces), as well as the other properties of the scopes are shown in the following table:
Introducing Scoped Variables, Continued
scopeJava class/interfacepurviewlifetimepagePageContextCurrent JSP pageExecution of the current JSP page.requestServletRequestCurrent requestLifetime of the current request.sessionHttpSessionResources participating in the active sessionLifetime of the active session.applicationServletContextAll resources in the web application.Lifetime of the application context.
Scoped variables can be created in many places, but a very common scenario is for the page controller servlet to create scoped variables in request scope, then forward to a JSP page in order to display that data.
For example:
request.setAttribute("greeting","Hi there!");
This statement creates a request-scoped variable named greeting with the string value Hi there!.
Now let's see how we can use that variable in a JSP.
Scoped variables can be created in many places, but a very common scenario is for the page controller servlet to create scoped variables in request scope, then forward to a JSP page in order to display that data.
For example:
This statement creates a request-scoped variable named greeting with the string value Hi there!.
Now let's see how we can use that variable in a JSP.
![Hot Tip](/storage/rc-covers/5676-thumb.png)
Application scope is a very handy place to store information that needs to be made available to all resources within a web application. It's easy to establish such data whenever a web application starts up by doing so in context listener. A context listener is simply a Java class that implements the javax.servlet.ServletContextListener interface, and is declared as a listener in the deployment descriptor (web. xml) using the <listener> element. Methods in such a class are invoked whenever a web application is put into (and taken out of) service, making it an ideal choice for application setup (and tear down). And best of all, any scoped variables placed in application scope are available to any EL expression in any JSP in the application!