The Basic Syntax Expression Language in JSP
By: Baski in JSP Tutorials on 2007-09-23
In this tutorial, you'll look at the syntax of the EL, see how to use it on a JSP page, and learn the reserved words of the language. After you've looked at the basics, you'll move on to look at how and why you might disable the EL and Java scriptlets within a page or set of pages.
Basic Syntax
No matter where the EL is used, it's always invoked in a consistent manner, via the construct ${expr} or #{expr}, where expr is the EL expression that you wish to have evaluated. In the EL 2.1 specification, the syntax of ${expr} and #{expr} are equivalent and can be used interchangeably. However, when used with some other Java Platform, Enterprise Edition API, the other API may enforce restrictions on the use of ${expr} and #{expr}. Specifically, when used with JSP pages, the two forms cannot be used interchangeably. Within a JSP page, ${expr} is used for expressions that are evaluated immediately, whereas #{expr} is used for expressions for which evaluation is deferred. Deferred expressions are used with custom actions.
A simple use of the EL is shown here. This piece of code creates a JavaBean and outputs its name property:
<jsp:useBean id="bean" class="MyBean"/>
${bean.name}
This is the recommended way to do this, rather than instantiating the object in a scriptlet.
Literals
Just as in any programming language, the EL provides several literals for developers to use. A literal can be of a Boolean, integer, floating point, string, or null type. The following are valid values for each literal type:
- Boolean: true or false.
- Integer: This is limited to values defined by the IntegerLiteral regular expression as follows:
IntegerLiteral ::= ['0'-'9']+
This regular expression says that an integer is any sequence of digits using the digits from 0 to 9. The specification also allows an integer literal to be preceded by a unary "-" symbol to form negative integer literals. For example, the following are valid integers:
-102
0
21
21234 - Floating point: This is defined by the following FloatingPointLiteral expression:
FloatingPointLiteral ::= ([''0'-'9'])+ '.' (['0'-'9'])* Exponent?
| '.' (['0'-'9'])+ Exponent?
| (['0'-'9'])+ Exponent?
Exponent ::= ['e','E'] (['+','-'])? (['0'-'9'])+
This expression is more complex. As with integer literals, a floating-point literal can be preceded by a unary "-" symbol to produce negative floating-point literals. To help you understand this, here are some valid floating-point literals:
-1.09
-1.003
1.0E10
1.
-10.0
0.1 - String: A string is any sequence of characters delimited with either single or double quotes. For example, "a string" and 'a string' are both valid; however, "as' and 'as" are not valid. If you want to represent quotes within a string, then you can use \" for double quotes, or \' for single quotes. Alternately, if the string is delimited by double quotes, you can use single quotes within the string without escaping the single quotes, and vice versa. To represent a \ in a string, you use the escape sequence \\.
- Null: You can represent null by using the literal null.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
Comments