JSP El expression in detail

Time:2021-6-8

1、 JSP El language definition

        E L(Expression Language)   Purpose: to makeJSP is easier to write.

       The inspiration of expression language comes from ECMAScript and XPath expression language, which provides a way to simplify expressions in JSP. It is a simple language based on the available namespace (pagecontext attribute), nested attributes and accessors to collections, operators (arithmetic, relational and logical), extensible functions mapped to static methods in Java classes, and a set of implicit objects.

       El provides the ability to use runtime expressions outside the scope of JSP scripting elements. Scripting elements refer to the elements in the page that can be used to embed java code in JSP files. They are often used for object manipulation and to perform calculations that affect the generated content. JSP 2.0 adds an EL expression as a scripting element.

 

2、 Introduction to JSP el

1. Grammatical structure
     ${expression}
2. ] and. Operator
     El provides “.” and “[]” operators to access data.
     When the attribute name to be accessed contains some special characters, such as. Or? For symbols that are not letters or numbers, you must use “[]”. For example:
         ${ User. My name} should be changed to ${user [“my name”]}
     If you want to take a dynamic value, you can use “[]” to do it, but “.” can’t take a dynamic value. For example:
         ${ Data is a variable in sessionscope. User [data]}
3. Variable
     El has a simple way to access variable data, such as ${user name}. It means to take out a variable named username in a certain range.
     Because we don’t specify which range of user name, it will search from page, request, session and application in order.
     If the user name is found on the way, it will be returned directly and will not continue to search. However, if all the scopes are not found, null will be returned.
     The name of the property range in El
         Page          PageScope
         Request          RequestScope
         Session          SessionScope
         Application      ApplicationScope

2、 Valid expressions in JSP el

       Valid expressions can contain text, operators, variables (object references), and function calls. We will look at each of these valid expressions separately:

1. Words

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


written words The value of the text

Boolean

True and false

Integer

Similar to Java. It 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 number, for example -1.8e-45, 4.567

String

Any string bounded by single or double quotation marks. For single quotes, double quotes, and backslashes, use the backslash character as the escape sequence. It is important to note that if double quotes are used at both ends of a string, single quotes do not need to be escaped.

Null null

 

2. Operator

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


term definition

Arithmetic type

+, – (binary), *, /, div,%, mod, – (unitary)

Logical type

and、&&、or、||、!、not

Relational

==、eq、!=、 ne、、gt、<=、le、>=、ge。 You can compare with other values, or with Boolean, string, integer, or floating-point text.

empty

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

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

 

3. Implicit object

JSP expression language defines a set of implicit objects, many of which are available in JSP scripts and expressions





pageContext

The context of the JSP page. It can be used to access JSP implicit objects, such as request, response, session, output, ServletContext, etc. For example, ${pagecontext. Response} assigns a value to the response object of a page.


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


term 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. The 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 the param implicit object, but it retrieves an array of strings instead of a single value. The 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. A client request to the server can obtain one or more cookies. The expression ${cookie. Name. Value} returns the first cookie value with a specific name. If the request contains more than one cookie with the same name, the ${headervalues. Name} expression should be used.
initParam Map the context initialization parameter name to a single value (obtained by calling ServletContext. Getinitparameter (string name)).


In addition to the above two types of implicit objects, some objects allow access to a wide range of variables, such as Web context, session, request, and page


term definition

pageScope

Map page wide variable names to their values. For example, El expressions can use ${pagescope. Objectname} to access page wide objects in a JSP, and they can also use ${pagescope. Objectname. Attributename} to access object properties.

requestScope

Map the variable name of the request scope to its value. This object allows access to the properties of the request object. For example, El expressions can use ${requestscope. Objectname} to access an object within the scope of a JSP request, and they can also use ${requestscope. Objectname. Attributename} to access an object’s attributes.

sessionScope

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


$sessionScope.name} 

applicationScope

Map application wide variable names to their values. This implicit object allows access to application wide objects.

 

3、 Special emphasis on:

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

2、Note that <% @ page iselignored = “true”% > indicates whether the El language is disabled, true indicates that it is disabled, and false indicates that it is not disabled. By default, the El language is enabled in JSP 2.0.

4、 Give an example

1. For example,

< %=request.getParameter(“username”)% >        Equivalent to ${ param.username }

2. For example, but the following sentence can be completed in El language. If you get a username that is empty, null will not be displayed, but the value will not be displayed.

 

    <%=user.getAddr( ) %>        Equivalent to     ${user.addr}。

3. For example:

<% =request.getAttribute(“userlist”) %>      Equivalent to$     { requestScope.userlist }

4. For example, the principle is as above3。

${ sessionScope.userlist } 1

${ sessionScope.userlist } 2

${ applicationScope.userlist } 3 

${ pageScope.userlist } 4

The meaning of ${uselist}: the execution order is4 1 2 3。

After “.” is just a string. It is not a real built-in object and cannot be called.

4. For example,

<%=user.getAddr( ) %>       Equivalent to ${user.addr}

Before the first sentenceUser is a variable.

After the second sentence, user must be an attribute in a certain range.