JSP Variable Scope

clangpotatoΛογισμικό & κατασκευή λογ/κού

28 Οκτ 2013 (πριν από 3 χρόνια και 7 μήνες)

64 εμφανίσεις

JSP Variable Scope
JSP Variables

The
scope
of a variable determines in what context it
maintains its value and where it can be referenced

Each JSP variable has a particular level of scope:

application scope

session scope

request scope

page scope
Application Scope

A variable with application scope can be accessed in
any JSP within the web application

It exists and maintains its value as long as the
application exists

Application scope should be reserved for data that
must be shared by many/all pages

database connections

common email addresses
Session Scope

A session is associated with a particular user of a web
application

For each user, a new session object is maintained to
track data relative to that user

Sessions expire after a particular amount of time, or
they can be destroyed programatically

Session variables can be used effectively for:

login information

shopping cart
Request Scope

A request refers to something being requested of the
server, such as a browser's request for a URL

Programmatically, this request can propagate to
multiple pages

Request scope is appropriate for any data that is
specific to a single client request
Page Scope

The scope of a page-level variable is limited to one
page

It exists only for one rendering of the page

Page scope is appropriate for any temporary variable
needed for the processing of the page

If no scope is specified when setting a variable, it
defaults to page scope
Setting and Accessing

The scope of a variable can be established in the
set

tag:
<c:set var="adminLevel" value="2" scope="session" />

Each scope level has an associated object through
which the variables can be referenced in EL:
${ requestscope.i }

If no level is specified, each level is examined in turn:
page, request, session, then application