EL operator (web basic learning notes 17) and el learning notes
I. EL syntax 1.1. syntax structure
$ {Expression}
1.2 [] and. EL provide. 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 []. Example: $ {user. change My-Name} to $ {user ["My-Name"]}. to dynamically set the value, use. dynamic values cannot be achieved. For example, data in $ {user [data]} is a variable.
1.3. The method for variable EL to access variable data is simple.
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.
1.4 access order
Ii. EL Operators
1. There are five Arithmetic Operators: +,-, * or $,/or div, %, or mod.
2. There are six Relational operators: = or eq ,! = Or ne, <or lt,> or gt, <= or le,> = or ge
3. There are three logical operators: & and, | or ,! Or not
4. Other operators include Empty, conditional, and ().
Example: $ {empty param. name}, $ {? B: C}, $ {A * (B + C )}
To avoid JSP obfuscation operators and page keywords, many operators have alternative methods:
Relational operators |
Description |
Example |
Result |
= Or eq |
Equal |
${5 = 5} or $ {5eq5} |
True |
! = Or ne |
Not equal |
$ {5! = 5} or $ {5ne5} |
False |
<Or lt |
Less |
${3 <5} or $ {3lt5} |
True |
> Or gt |
Greater |
$ {3> 5} or {3gt5 |
False |
<= Or le |
Less than or equal |
${3 <= 5} or $ {3le5} |
True |
> = Or ge |
Greater than or equal |
$ {3> = 5} or $ {3ge5} |
False |
Note: When the EL relational operator is used, it cannot be written as $ {param. password1 }== {param. password2} or $ {$ {param. password1 }== {param. password2 }}
It should be written as $ {param. password1 = param. password2}
Logical operators
Logical operators |
Example |
Result |
& Or and |
Intersection $ {A & B} or $ {A and B} |
True/false |
| Or |
Union $ {A | B} or $ {A or B} |
True/false |
! Or not |
Not $ {! A} or $ {not} |
True/false |
3. EL access pageContext
Use EL to access the pageContext object and obtain detailed information about the page, as shown below:
$ {PageContext. request. queryString} gets the request parameter string
$ {PageContext. request. requestURL} retrieves the request URL, excluding the request parameter string
$ {PageContext. request. contextPath} indicates the name of the web application of the service.
$ {PageContext. request. method} GET the HTTP method (GET, POST)
$ {PageContext. request. protocol} gets the used protocol (HTTP/1.1, HTTP/1.0)
$ {PageContext. request. remoteUser} obtains the user name
$ {PageContext. request. remoteAddr} obtains the user's IP address
$ {PageContext. session. new} checks whether the session is new.
$ {PageContext. session. id} obtains the session ID.
$ {PageContext. servletContext. serverInfo} obtains the service information of the host.
4. EL access objects
EL expressions can access a series of objects, with a total of 11 objects. We focused on the most important 1-6.
(1) pageContext: refers to the pageContext object of JSP.
(2) pagination: A Map object, including the attributes and values of the page range
(3) requestScope: A Map object, including the attributes and values of the request range
(4) sessionScope: A Map object, including the attributes and values of the session range
(5) ApplicationScope: A Map object, including attributes and values of the application range
(6) param: A Map object, including the String value of the Web request parameter, corresponding to ServletRequest. getParameter (String)
(7) paramValues: A Map object, including multiple String values of the Web request parameter (request parameter), corresponding to ServletRequest. getParameterValues (String)
(8) header: A Map object, including the request header information value, corresponding to ServletRequest. getHeader (String)
(9) headerValues: A Map object, including multiple request header values, corresponding to ServletRequest. getHeaders (String)
(10) cookie: A Map object, including the Cookie with the corresponding name, corresponding to HttpServletRequest. getCookie (String)
(11) initParam: A Map object, including the initialization parameter value of a Web program, corresponding to ServletRequest. getInitParameter (String)