Introduction to JSP El

Source: Internet
Author: User
Tags null null sessions

Introduction to JSP el:
1. Syntax:
${expression}

2.[] with the. operator
EL provides "." and "[]" operators to access data.
When the name of the property to be accessed contains some special characters, such as. or? Other than letters or numbers, be sure to use "[]". For example:
${user. My-name} should be changed to ${user["My-name"}


If you want to dynamically fetch values, you can do it with "[]" and "." cannot be dynamically evaluated. For example:
${sessionscope.user[data]} in data is a variable

3. Variables
The EL Access variable data method is simple, for example: ${username}. It means to take out a variable whose name is username in a range.


Because we do not specify a range of username, it will be searched sequentially from page, Request, Session, application range.
If the way to find username, direct callback, no longer continue to find, but if all the scope is not found, the return null.


Name of the attribute range in El
Page Pagescope
Request Requestscope
Session Sessionscope
Application Applicationscope

Valid expressions in 4.JSP EL
Valid expressions can contain literals, operators, variables (object references), and function calls.

(1) Text
The following text can be used in an El expression:

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 single or double quotation marks. For single quotes, double quotes, and backslashes, use a backslash character as an escape sequence. It is important to note that single quotes do not need to be escaped if double quotes are used at both ends of the string.

NULL NULL


(2) operator
The following operators are available, most of which are commonly used in Java:
Arithmetic type
+,-(two Yuan), *,/, Div,%, mod,-(one yuan)

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

Relationship Type
= =, EQ,! =, NE, GT, <=, le, >=, GE. Can be compared with other values, or with Boolean, String, Integer, or floating-point literals.

Empty
An empty operator is a prefix operation that can be used to determine whether a value is empty.

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


(3) An implicit object
The JSP expression language defines a set of implicit objects, many of which are available in JSP Scriplet and expressions:

JSP page Context (PageContext): 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 available that allow easy access to the following objects:
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 (string name ).
For example, $ (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.
For example: ${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).
For example: ${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 an implicit object.
For example: ${headervalues.name} is equivalent to Request.getheadervalues (name).

The cookie maps the cookie name to a single Cookie object. A client request to the server can obtain one or more cookies.
For example: ${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 maps 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, there are also objects that allow access to a wide range of variables, such as Web contexts, sessions, requests, and pages:
Pagescope
Maps the variable name of a page range to its value.
For example, an EL expression can use ${pagescope.objectname to access a page-scoped object in a JSP, and you can use ${pagescope.objectname.attributename} to access the properties of an object.

Requestscope
Map the variable name of the request scope to its value. The object allows access to the properties of the requested object.
For example, an EL expression can use ${requestscope.objectname} to access an object of a JSP request scope, and you can use ${requestscope.objectname.attributename} to access the properties of an object.

Sessionscope
Maps a session-scoped variable name to its value. The object allows access to the properties of the session object.
Example: $sessionScope. Name}


Applicationscope
Maps application-scoped variable names to their values. The implicit object allows access to application-scoped objects.



Third, special emphasis:
1. Note that when an expression references one of these objects by name, the corresponding object is returned instead of the corresponding property. For example, if an existing PageContext property contains some other value, ${pagecontext} also returns the PageContext object.

Introduction to JSP El

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.