Detailed introduction to El expressions in JSP

Source: Internet
Author: User
Document directory
  • II. Introduction to JSP el

I. jsp El language definition

Expression Language (e l) Objective: To make JSP writing easier.

The expression language is inspired by ecmascript and XPath expressions. It provides methods to simplify expressions in JSP. It is a simple language based on available namespaces (pagecontext attributes), nested attributes, and pairs of sets, operators (arithmetic, relational, and logical) and a set of implicit objects.

El provides the ability to use runtime expressions outside the scope of JSP scripting elements. A script element is an element on a page that can be used to embed Java code into a JSP file. They are usually used for object operations and computing that affects the generated content. JSP 2.0 adds an El expression as a scripting element.

II. Introduction to JSP el

1. syntax structure
$ {Expression}
2. [] and. Operators
El provides "." and "[]" operators to access data.
When the attribute name to be accessed contains some special characters, such as. Or? If it is not a letter or number, you must use "[]". For example:
$ {User. My-name} should be changed to $ {user ["My-name"]}
If you want dynamic values, you can use "[]", but "." cannot achieve dynamic values. For example:
Data in $ {sessionscope. User [data]} is a variable.
3. Variables
El is easy to access variable data, for example, $ {username }. It means to retrieve the variable named username in a range.
Because we do not specify the username of the range, it will search for the range from page, request, session, and application in sequence.
If the username is found on the way, it will be directly returned and will not be found any more, but if all the ranges are not found, null will be returned.
Name of attribute range in El
Page pagination
Request requestscope
Session sessionscope
Application applicationscope

Ii. Valid expressions in JSP el

A valid expression can contain text, operators, variables (Object references), and function calls. We will understand each of these valid expressions:

1. Text

The JSP Expression Language defines the following words that can be used in an expression:

 

Text Text Value

Boolean

True and false

Integer

Similar to Java. Can contain any positive or negative number, such as 24,-45, 567

Floating Point

Similar to Java. Can contain any positive or negative floating point numbers, such as-1.8e-45, 4.567

String

Any string limited by single or double quotation marks. For single quotes, double quotes, and backslash, The backslash character is used as the escape sequence. Note that if double quotation marks are used at both ends of a string, the single quotation marks do not need to be escaped.

Null Null

2. Operators

JSP expression language provides the following operators, most of which are commonly used operators in Java:

 

Terms Definition

Arithmetic type

+,-(Binary), *,/, Div, %, Mod,-(one dollar)

Logical type

And, &, Or, | ,! , Not

Relational

=, EQ ,! =, NE, , GT, <=, le,> =, GE. It can be compared with other values or with Boolean, String, integer, or floating-point text.

Null

The null operator is a prefix operation that can be used to determine whether the value is null.

Conditional A? B: C. Assign values to B or C based on the value of.

3. Implicit object

JSP Expression Language defines a set of implicit objects, many of which are available in JSP Scriplet and expressions:

Terms Definition

Pagecontext

The context of the JSP page. It can be used to access JSP implicit objects, such as requests, responses, sessions, outputs, and servletcontext. For example, $ {pagecontext. Response} assigns a value to the response object of the page.

In addition, it provides several implicit objects that allow simple access to the following objects:

 

Terms Definition

Param

Map the request parameter name to a single string parameter value (obtained by calling servletrequest. getparameter (string name ). The getparameter (string) method returns a parameter with a specific name. Expression $ (Param. Name) is equivalent to request. getparameter (name ).

Paramvalues

Map the request parameter name to a numeric array (obtained by calling servletrequest. getparameter (string name ). It is very similar to a Param implicit object, but it retrieves a string array instead of a single value. Expression $ {paramvalues. Name) is equivalent to request. getparamtervalues (name ).

Header

Map the request header name to a single string header value (obtained by calling servletrequest. getheader (string name ). The expression $ {header. name} is equivalent to request. getheader (name ).

Headervalues

Map the request header name to a numeric array (obtained by calling servletrequest. getheaders (string ). It is very similar to the header implicit object. The expression $ {headervalues. name} is equivalent to request. getheadervalues (name ).

Cookie Map the cookie name to a single cookie object. Client requests sent to the server can obtain one or more cookies. Expression $ {cookie. Name. Value} returns the first cookie value with a specific name. If the request contains multiple cookies with the same name, use the $ {headervalues. name} expression.
Initparam Map the context initialization parameter name to a single value (obtained by calling servletcontext. getinitparameter (string name ).

In addition to the preceding two types of implicit objects, some objects allow access to variables in various scopes, such as web context, session, request, and page:

 

Terms Definition

Pagination

Map the variable names in the page range to their values. For example, an El expression can use $ {pagination. objectname} to access a page range object in a JSP, and use $ {pagination. objectname. attributename} to access the object attributes.

Requestscope

Map the variable name in the request range to its value. This object allows access to the properties of the request object. For example, the El expression can use $ {requestscope. objectname} to access an object in the jsp request range, and use $ {requestscope. objectname. attributename} to access the attributes of the object.

Sessionscope

Map the name of the variable in the session range to its value. This object allows access to the properties of the session object. For example:

$ Sessionscope. name}

Applicationscope

Map the variable names in the application range to their values. This implicit object allows access to objects within the application range.

Iii. Special emphasis:

1. Note that when the expression references these objects based on their names, the corresponding objects instead of the corresponding attributes are returned. For example, even if the existing pagecontext attribute contains some other values, $ {pagecontext} returns the pagecontext object.

2. Note that <% @ page iselignored = "true" %> indicates whether El language is disabled. True indicates that El language is disabled. False indicates that El language is disabled by default in jsp2.0.

Iv. Examples

1. For example,

<% = Request. getparameter ("username") %> equivalent to $ {Param. Username}

2. For example, the following El language can be completed. If the username is null, null is not displayed, but the value is not displayed.

<% = User. getaddr () %> is equivalent to $ {user. ADDR }.

3. For example:

<% = Request. getattribute ("userlist") %> equivalent to $ {requestscope. userlist}

4. For example, the principle is as follows: 3.

$ {Sessionscope. userlist} 1

$ {Sessionscope. userlist} 2

$ {Applicationscope. userlist} 3

$ {Pagination. userlist} 4

$ {Uselist} indicates that the execution sequence is 4 1 2 3.

"." Is followed by a string, which is not a real built-in object and cannot be called.

4. For example,

<% = User. getaddr () %> equivalent to $ {user. ADDR}

The user in front of the first sentence is a variable.

The user must be an attribute in a certain range.

Related Article

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.