Expression Language)
I. Purpose: 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 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:
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.
Condition? 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:
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:
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 maps 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 maps the context initialization parameter names 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:
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.