JSP El Expressions detailed description of

Source: Internet
Author: User
Tags definition header numeric null null sessions

  A, JSP El language definition

E L (Expression Language) Purpose: To make JSP easier to write.

Expression languages are inspired by the ECMAScript and XPath expression languages, which provide a way to simplify expressions in a JSP. It is a simple language, based on available namespaces (PageContext properties), nested properties and accessors for collections, operators (arithmetical, relational, and logical), extensible functions that map to static methods in Java classes, and a set of implicit objects.

EL provides the ability to use run-time expressions outside the scope of the JSP scripting elements. Scripting elements are elements in a page that can be used to embed Java code in a JSP file. They are typically used for object manipulation and for performing calculations that affect what is generated. JSP 2.0 adds an EL expression as a scripting element.

Second, JSP el Introduction

1. Grammatical structure

${expression}

2, [] and. operator

EL provides "." and "[]" two operators to access the data.

When you want to access a property name that contains special characters, such as. or? etc. is not a sign of letters or numbers, be sure to use []. For example:

${user. My-name} should be changed to ${user["My-name"]}

If you want to dynamically take a value, you can use [] to do it, and "." Dynamic values cannot be achieved. For example:

${sessionscope.user[data]}, data is a variable

3, variable

The method of El Access variable data is simple, for example: ${username}. It means to remove a variable whose name is username in a range.

Since we do not specify which range of username, it will be searched sequentially from page, Request, session, application range.

If the way to find username, direct return, no longer continue to find, but if all the scope is not found, it will return null.

Name of attribute range in El

Page Pagescope

Request Requestscope

Session Sessionscope

Application Applicationscope

Ii. valid expressions in JSP EL

A valid expression can contain text, an operator, a variable (an object reference), and a function call. We'll look at each of these valid expressions separately:

1, text

The JSP expression language defines the following text that can be used in an expression:

The value of the literal text

Boolean

True and False

Integer

Similar to Java. Can contain any positive or negative numbers, 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 that is qualified by either single or double quotes. For single quotes, double quotes, and backslashes, the backslash character is used as the escape sequence. It is important to note that if you use double quotes at both ends of a string, single quotes do not need to be escaped.

NULL NULL

2. Operator

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

Terminology definition

Arithmetic type

+,-(two Yuan), *,/, Div,%, mod,-(one yuan)

Logical type

And, &&, or, | | 、!、 not

Relationship Type

= =, eq,!=, NE, GT, <=, le, >=, GE. You can compare to other values, or to a Boolean, String, Integer, or floating-point literal.

Empty

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

Conditional A? B:c. Assign a value of B or C according to the result of a assignment.

3. Implicit object

The JSP expression language defines an implicit set of objects, many of which are available in JSP Scriplet and expressions:

PageContext

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

In addition, several implicit objects are provided that allow easy access to the following objects:

Terminology definition

Param

Maps 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. The expression $ (param.name) is the equivalent of Request.getparameter (name).

Paramvalues

Maps 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 an array of strings instead of a single value. Expression ${paramvalues.name) is the equivalent of request.getparamtervalues (name).

Header

Maps the request header name to a single string header value (obtained by calling Servletrequest.getheader (String name)). The expression ${header.name} is the equivalent of Request.getheader (name).

Headervalues

Maps 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 the equivalent of request.getheadervalues (name).

The cookie maps the cookie name to a single Cookie object. A client request made to the server can obtain one or more cookies. An expression ${cookie.name.value} returns the first cookie value with a specific name. If the request contains multiple cookies with the same name, you should use the ${headervalues.name} expression. Initparam maps a context initialization parameter name to a single value (obtained by calling Servletcontext.getinitparameter (String name).

In addition to the two types of implicit objects mentioned above, there are also objects that allow access to a wide range of variables, such as Web contexts, sessions, requests, pages:

Terminology definition

Pagescope

Maps the variable name of a page range to its value. For example, an EL expression can access a page-wide object in a JSP using ${pagescope.objectname}, and you can access the object's properties using ${pagescope.objectname.attributename}.

Requestscope

Maps the variable name of the request scope to its value. This object allows access to the properties of the requested object. For example, an EL expression can access an object of a JSP request range using ${requestscope.objectname} and access the properties of the object using ${requestscope.objectname.attributename}.

Sessionscope

Maps a session-scoped variable name to its value. This object allows access to the properties of the session object. For example:

$sessionScope. Name}

Applicationscope

Maps an application-scoped variable name to its value. This implicit object allows access to application-scoped objects.

  Third, special emphasis is placed on:

1. Note that when an expression refers to one of these objects by name, the corresponding object is returned instead of the corresponding property. For example, ${pagecontext} returns the PageContext object even if the existing PageContext property contains some other values.

2. Note that the El language is disabled, true to prohibit. False indicates that the default enabled El language is not prohibited in. JSP2.0.

Iv. Illustrative examples

1, for example,

<%=request.getparameter ("username")% > equivalent to ${param.username}

2, for example, but the following El language can be completed if a username is empty, the null is not displayed, but the value is not displayed.

Equivalent to ${user.addr}.

3, for example:

Equivalent to $ {requestscope.userlist}

4, for example, the principle is as above example 3.

${Sessionscope.userlist} 1

${Sessionscope.userlist} 2

${Applicationscope.userlist} 3

${Pagescope.userlist} 4

${uselist} Meaning: Execution order is 4 1 2 3.

“.” The back is just a string, not a real built-in object, and cannot invoke the object.

4, for example,

Equivalent to ${USER.ADDR}

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

After the second sentence, 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.