6. More view facilities




6.1 Understand the four data scopes

PageContext extends JspContext and contains static variables for scope:

  • PageContext.APPLICATION_SCOPE
  • PageContext.SESSION_SCOPE
  • PageContext.REQUEST_SCOPE
  • PageContext.PAGE_SCOPE

In EL, these correspond to implicit objects:

  • applicationScope
  • sessionScope
  • requestScope
  • pageScope

6.2 Understand and use EL dot and array access operators with Java Beans, arrays, and collections

dot access operator

${left.right}
  • left must be a map or bean
  • right must be a map key or bean property. It must follow normal Java naming rules for identifiers.

array access operator

${left["right"]}
  • Using [], left can be any type, not just a bean or map. E.g. map, bean, list, array
  • right can be any identifier . e.g. Map key or a bean property, or an index into a List or array.

Examples:

${left[0]}
${left["0"]} // string index converted to int for arrays and lists (tricky when arithmetic is involved)
 
// beans and maps can use [] or .
${person.name}
${person["name"]
 
// name is evaluated to a key, not a value. the evaluated key is used to retrieve a value
${person[name]
 
// el can be used for nested expressions
$left[right[innerRight]]
 
// invalid
${musicList.1} // but this is okay: ${musicList["1"]}



6.3 Understand and use EL implicit objects

EL implicit objects are not the same as JSP scripting objects. All but pageContext is a map. These objects are used to access attributes, not properties (i.e. variables, not methods).

Remember these confusing aspects:

  • attributes = member variables
  • properties = methods (not accessible by EL using implicit objects)
  • objects added through scripting are not available to EL, unless the following is used: pageContext.setAttribute("x", x, PageContext.X_SCOPE) or request.setAttribute("ten", "10"), etc.
  • EL will find the name variables by searching through all scopes automatically, starting with page scope.

List of all implicit objects

None of these are not available in jspInit(). The bold ones also exist as JSP implicit variables.

  • param - Request parameters as strings
  • paramValues - Request parameters as collections of strings
  • header - HTTP request headers as strings
  • headerValues - HTTP request headers as collections of strings
  • initParam - Context-initialization parameters
  • cookie - Cookie values
  • pageContext - The JSP PageContext object for the current page

Scope variables:

pageScope
requestScope
sessionScope
applicationScope

param / paramValues

param          // single parameter value
 
// for a single parameter, e.g. lastname, the following are oddly equivalent
// , even though lastname is not a collection
${param.lastname}
${paramValues.lastname[0]}
 
paramValues // more than one parameter value for a given parameter name
 
// access paramValues like this:
${paramValues.food[0]} ${paramValues.food[1]}

header / headerValues

header           // single header value
headerValues //  more than one header value for a given header name
// e.g.
${header["host"]}
${header.host}

cookie

Note: it is NOT cookies, but cookie

cookie // map of cookies
// e.g. 
${cookie.userName.value} // much simpler than looping through Cookie[]

initParam

initParam // context init parameters (NOT servlet init parameters)
// e.g. 
${initParam.mainEmail}

pageContext

pageContext (not a map)



6.4 Create and use arithmetic expressions in EL

Remember:

  • Strings are non-intuitively cast to ints if numbers, or boolean if "true" or "false"
  • nulls do not throw exceptions
  • null objects may display blank, 0 or false
  • Ternary conditions can be used: A ? B : C

empty returns true for: null, empty collection, empty String

Arithmetic (5)

  • null values treated as 0
  • Division by zero may literally result in Infinity
  • none and "" are treated as zero
+
-
*
/ and div
% and mod

Logical (3)

  • null values treated as false
&& and and
|| and or
! and not

Relational (6)

== and eq
!= and ne
< and lt
> and gt
<= and le (don't confuse with lt)
>= and ge



6.4 Identify the need for iteration and selection in the view, and use JSTL tags to address those needs

The first part of the objective is obvious:

  • Iteration is needed for display multiple items in a collection
  • Selection is needed to selectively display data

The second part is covered under 8. Developing JSP pages using custom tags





Comments

If you have questions, corrections, information or examples to add, please comment below.

Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License