Accessing Scoped Variables in Scripting Code
The term variable is generally used for any dynamic data an application manipulates, but when we talk about JSP and scripting elements, it’s important to be more specific.
JSP custom actions, including the JSTL actions, can expose data
through what is called scoped variables, typically named by a
var
and an optional scope
attribute. A
scoped
variable is an object that lives in one of the
JSP scopes: page, request, session, or application. As mentioned
earlier, the scopes are actually collections of named object
references that correspond to the attributes that the implicit
pageContext
, request
,
session
, and application
objects provide access to.
Another type of variable is a scripting variable. A scripting variable is a variable declared in a JSP scriptlet or declaration, using the language defined for the page (typically Java). To read or manipulate data with scripting code, you need a scripting variable that holds a reference to the object that contains the data.
The distinction between the variable types becomes apparent when you
mix actions that expose data only as scoped variables with scripting
elements. To use the data exposed by the action in a scripting
element, you must first tell the container to create a scripting
variable for it and assign it the value of the scoped variable. The
easiest way to do this is to use the standard
<jsp:useBean>
action:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ page import="java.util.Date" ...
Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.