Using pageContext to get and set attributes
Setting a page-scoped attribute
<% Float one = new Float(42.5); %>
<% pageContext.setAttribute(“foo”, one); %>
Getting a page-scoped attribute
<%= pageContext.getAttribute(“foo”) %>
Using the pageContext to set a session-scoped attribute
<% Float two = new Float(22.4); %>
<% pageContext.setAttribute(“foo”, two, PageContext.SESSION_SCOPE); %>
Using the pageContext to get a session-scoped attribute
<%= pageContext.getAttribute(“foo”, PageContext.SESSION_SCOPE) %>
(Which is identical to: <%= session.getAttribute(“foo”) %> )
Using the pageContext to get an application-scoped attribute
Email is:
<%= pageContext.getAttribute(“mail”, PageContext.APPLICATION_SCOPE) %>
Within a JSP, the code above is identical to:
Email is:
<%= application.getAttribute(“mail”) %>
Using the pageContext to fi nd an attribute when you don’t know the scope
<%= pageContext.fi ndAttribute(“foo”) %>
find it where ?
Where does the findAttribute() method look? It looks first in the page context, so if there’s a “foo”
attribute with page context scope, then calling findAttribute(String name) on a PageContext works just like calling getAttribute(String name) on a PageContext. But if there’s no “foo” attribute, the method starts looking in other scopes, from most restricted to least restricted scope —in other words, first request scope, then session, then finally application scope. The fi rst one it fi nds with that name wins.
Static Final Fields
APPLICATION_SCOPE
PAGE_SCOPE
REQUEST_SCOPE
SESSION_SCOPE