JSP series: (7) JSP advanced-el

Source: Internet
Author: User

1. Simple grammar

Expression Language (EL) is added to JSP 2.0 specification. The purpose of EL is to produce scriptless JSP pages. The syntax of EL in a JSPs is as follows:

${EXPR}

Here expr Specifies the expression itself.

(1) The two commonly used operators in a JSP El are: "." and "[]".

The most common operators in JSP EL is . and []. These operators allow you to access various attributes of Java Beans and built-in JSP objects.

(2) If JSP compiler see ${}, the value of the expression is evaluated and output.

When the JSP compiler sees the ${} form in a attribute, it generates code to evaluate the expression and substitues the V Alue of Expresson.

(3) If you want to control whether the JSP El expression is valid, you need to set it in the page directive.

To deactivate the evaluation of EL expressions, we specify the iselignored attribute of the page directive as Bel ow

<%@ page iselignored = "True|false"%>

The valid values of this attribute is true and false. If It is true, EL expressions was ignored when they appear in static text or tag attributes. If It is false, EL expressions was evaluated by the container.

2. Basic Operators in EL

JSP Expression Language (EL) supports most of the arithmetic and logical operators supported by Java.below are the list of Most Frequently used operators:


Basic Operators in EL
Serial Number Operator Description
01 . Access a Bean property or Map entry
02 [ ] Access an array or List element
03 ( ) Group a subexpression to change the evaluation order
04 (Arithmetic) + Addition
05 (Arithmetic) - Subtraction or negation of a value
06 (Arithmetic) * Multiplication
07 (Arithmetic) /or Div Division
08 (Arithmetic) % or mod Modulo (remainder)
09 (Relationship) = = or EQ Test for Equality
10 (Relationship) ! = or NE Test for inequality
11 (relationship) < or LT Test for less than
12 (Relationship) > or GT Test for greater than
13 (Relationship) <= or Le Test for less than or equal
14 (Relationship) >= or GE Test for greater than or equal
15 (logic) && OR and Test for Logical AND
16 (logic) || OR OR Test for Logical OR
17 (logic) ! Or not Unary Boolean complement
18 Empty Test for empty variable values


3. JSP EL Implicit Objects

The JSP expression language supports the following implicit objects:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/22/wKioL1dMl-DAf1dtAABLkgvzj5Q464.jpg "title=" Expression-language-example.jpg "alt=" Wkiol1dml-daf1dtaablkgvzj5q464.jpg "/>


JSP EL Implicit Objects
Serial Number Implicit Object Description
01 Pagescope Scoped variables from page scope
02 Requestscope Scoped variables from request scope
03 Sessionscope Scoped variables from session scope
04 Applicationscope Scoped variables from Application scope
05 Param Request parameters as strings
06 Paramvalues Request parameters as collections of strings
07 Header HTTP Request headers as strings
08 Headervalues HTTP request headers as collections of strings
09 Initparam Context-initialization parameters
10 Cookies Cookie values
11 PageContext The JSP PageContext object for the current page

You can use these objects on an expression as if they were variables. Here is few examples which would clear the concept:


3.1. The PageContext Object

The PageContext here is the implicit object of the El expression, which corresponds exactly to PageContext in the nine built-in objects of a JSP.

The PageContext object gives you access to the PageContext JSP object.

Through the PageContext object, you can access the request object. For example, to access the incoming query string for a request, you can use the expression:

${pagecontext.request.querystring}


3.2. The Scope Objects

The pagescope, requestscope, sessionscope, and applicationscope variables provide Access to variables stored at each scope level.

For example, If you need-explicitly access the box variable in the application scope, you can access it throug H the Applicationscope variable as applicationscope.box.


3.3. The Param and paramvalues Objects

The param and paramvalues objects give you access to the parameter values normally available through the request.getparameter and request.getparametervalues methods.

For example, to access a parameter named "Order", with the expression ${param.order} or ${param["order"}.


3.4. Header and Headervalues Objects

The header and headervalues objects give you access to the header values normally available through the request.getheader and request.getheaders methods.

For example, to access a header named "User-agent" with the expression ${header.user-agent} or ${header["User-agent"}.


4. JSP EL Important Points

(1) EL expressions is always within curly braces prefixed with $ sign, for example ${expr}

(2) We can disable EL expression in JSPs by setting JSP page directive iselignored attribute value to TRUE.

(3) JSP EL can is used to get attributes, header, cookie, init params etc, but we can ' t set the values.

(4) JSP EL implicit objects is different from JSP implicit objects except PageContext, and don ' t get confused.

(5) JSP EL PageContext Implicit object is provided to get additional properties from request, response etc, for example gettin G HTTP request method.

(6) JSP EL is null friendly, if given attribute are not found or expression returns NULL, it doesn ' t throw any exception. For arithmetic operations, El treats null as 0 and for logical operations, El treats null as false.

(7) The [] operator is more powerful than dot operator because we can access list and array data too, it can be nested and arg Ument to [] was evaluated when it's not string literal.

(8) If you are using Tomcat, the EL expressions is evaluated using

Org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate ()

Method.


(9) We can use the EL functions to call method from a Java class.

Article reference:

http://beginnersbook.com/2013/11/jsp-expression-language-el/

Http://www.journaldev.com/2064/jsp-expression-language-el-example-tutorial




JSP series: (7) JSP advanced-el

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.