1 / 12

JSP Expression Language

JSP Expression Language. Celsina Bignoli bignolic@smccd.net. Expression Language. Introduced as part of standard JSP in version 2.0 simple expression language for accessing variables starts with the ${ delimiter and ends with } ${anExpression} Case sensitive. All keywords are lowercase.

nassor
Download Presentation

JSP Expression Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JSP Expression Language Celsina Bignoli bignolic@smccd.net

  2. Expression Language • Introduced as part of standard JSP in version 2.0 • simple expression language for accessing variables • starts with the ${ delimiter and ends with } ${anExpression} • Case sensitive. All keywords are lowercase

  3. Using Expressions • in static text • The value of the expression is computed and inserted into the current output • In any standard or custom tag attribute that can accept an expression <some:tag value="${expr}"/> • The expression is evaluated and the result is coerced to the attribute's expected type <some:tag value="some${expr}${expr}text${expr}"/> • The expressions are evaluated from left to right. Each expression is coerced to a String and then concatenated with any intervening text. The resulting String is then coerced to the attribute's expected type.

  4. Variables • The web container evaluates a variable that appears in an expression by looking up its value according to the behavior of PageContext.findAttribute(String). • For example, when evaluating the expression ${product}, the container will look for product in the following scopes: • page • request • session • application • If product is not found, null is returned.

  5. Variables • Properties of variables are accessed using the “. “ operator and can be nested arbitrarily • For example: • ${pageContext.session} • ${myBean.attribute1.attribute2}

  6. Implicit Variables • pageContext: The context for the JSP page. Provides access to various other objects including: • session: The session object for the client. • request: The request triggering the execution of the JSP page. • response: The response returned by the JSP page.

  7. Implicit Variables • param: Maps a request parameter name to a single value • <c:out value=“${param.userName}” /> • paramValues: Maps a request parameter name to an array of values • header: Maps a request header name to a single value • headerValues: Maps a request header name to an array of values • cookie: Maps a cookie name to a single cookie • initParam: Maps a context initialization parameter name to a single value

  8. Implicit Variables • pageScope: Maps page-scoped variable names to their values • requestScope: Maps request-scoped variable names to their values • sessionScope: Maps session-scoped variable names to their values • applicationScope: Maps application-scoped variable names to their values

  9. Literals • The JSP expression language defines the following literals: • Boolean: true and false • Integer: as in Java • Floating point: as in Java • String: with single and double quotes; " is escaped as \", ' is escaped as \', and \ is escaped as \\. • Null: null

  10. Literals • the JSP expression language provides the following operators: • Arithmetic: +, - (binary), *, / and div, % and mod, - (unary) • Logical: and, &&, or, ||, not, ! • Relational: ==, eq, !=, ne, <, lt, >, gt, <=, ge, >=, le. Comparisons can be made against other values, or against boolean, string, integer, or floating point literals. • Empty: The empty operator is a prefix operation that can be used to determine whether a value is null or empty. • Conditional: A ? B : C. Evaluate B or C, depending on the result of the evaluation of A.

  11. Reserved Words • The following words are reserved for the JSP expression language and should not be used as identifiers: and   eq   gt   true   instanceof or    ne   le   false  empty not   lt   ge   null   div   mod

  12. Example • ${4.0 > 3} true • ${empty param.add} true if parameter add in the request is null or empty string • ${pageContext.request.contextPath} The context path • ${sessionScope.cart.numberOfItems} The value of the numberOfItems property of the session-scoped attribute named cart

More Related