Programming Tutorials

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

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JSP )

Latest Articles (in JSP)