Arithmetic Evaluation Using the Expression Language in JSP

By: Fazal Emailed: 1728 times Printed: 2337 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This tutorial explains using the EL to evaluate arithmetic operations. There are many cases within web-application development where you need to perform some mathematics on a page. This might be to show a number within the text of a page or to pass a number to a custom tag. In either case, the concepts are exactly the same.

Arithmetic operators are provided to act on both integer and floating-point values. There are six operators that you can use and combine to achieve the vast majority of mathematical calculations with ease:

• Addition: +
• Subtraction: -
• Multiplication: *
• Exponents: E
• Division: / or div
• Modulus: % or mod

The last two operators are presented with two alternative syntaxes (both will produce exactly the same result). This is so that the EL is consistent with both the XPath and ECMAScript syntaxes. You can use all the operators in a binary fashion (that is, with two arguments, such as 2 + 3) and the subtraction operator to represent the unary minus (that is, -4 + -2).

As you would expect, each operator has a precedence that determines the order of evaluation of an expression. This precedence is as follows:

• ()
• - (unary)
• * / div mod %
• + - (binary)

You’ll update this list when you look at the comparison operators in the next section. You can, of course, use parentheses to change the order of evaluation, as these take the highest precedence.
With operators of equal precedence, the expression is evaluated from left to right, for example:

2 * 5 mod 3

is equivalent to (2 * 5) mod 3, which evaluates to 1—rather than 2 * (5 mod 3), which evaluates to 4.

Listing below is a JSP page that shows an example of all the operators in action.

body, td {font-family:verdana;font-size:10pt;}
<h2>EL Arithmetic</h2>
<table border="1">
<td><b>EL Expression</b></td>
<td>${'${'}10 + 10 }</td>
<td>${10 + 10}</td>
<td>${'${'}10 - 10 }</td>
<td>${10 - 10}</td>
<td>${'${'}10 * 10 }</td>
<td>${10 * 10}</td>
<td>Division / </td>
<td>${'${'}10 / 3 }</td>
<td>${10 / 3}</td>
<td>Division DIV</td>
<td>${'${'}10 div 3 }</td>
<td>${10 div 3}</td>
<td>${'${'}10 % 3 }</td>
<td>${10 % 3}</td>
<td>${'${'}10 mod 3 }</td>
<td>${10 mod 3}</td>
<td>${'${'}2 * 5 mod 3 }</td>
<td>${2 * 5 mod 3}</td>
<td>Precedence with parens</td>
<td>${'${'}2 * (5 mod 3) }</td>
<td>${2 * (5 mod 3)}</td>
<td>Division by Zero</td>
<td>${'${'}10 / 0 }</td>
<td>${10 / 0}</td>
<td>Unary Minus</td>

To run this example, you need to deploy it into a JSP 2.0– or JSP 2.1–compliant web container:

  1. Copy the JSP page in Listing 3-3 to the %TOMCAT_HOME%\webapps\expressionLanguage directory.
  2. Start Tomcat, if needed, open your web browser, and go to

Figure: shows the output of the JSP page.

The arithemetic operators allow you to perform many basic math operations in a JSP page. All that this JSP page does is print out the result of the expression next to the expression itself. It also demonstrates an interesting technique: that of displaying the ${ characters on a JSP page. This is easily achieved by embedding the literal '${' in an EL statement. For example, to show the string ${2+3} on a JSP page, you can use the following expression:

${'${'}2 + 3 }

You may be thinking that the operators that are provided are not powerful enough. For example, where is the square-root operator? Advanced operators are deliberately not provided to the JSP developer because advanced calculations should not be done in a page. They should either be done in the controller layer of the application, or by using view-helper components such as custom tags or EL functions.

JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Fazal
The Tag Life Cycle with Attributes in JSP
Using Shift Operators in C
IncludeAction in Struts
typedef example program in C
Arithmetic Evaluation Using the Expression Language in JSP
Tutorial on Complicated Declarations in C
Static Variables in C
getPosition() the right strategy in getting GPS
Precedence and Order of Evaluation in C
File Copying in C
JSF Life Cycle
Getting User Input Using cin in C++
UDP Datagram sample in J2ME
GUI components and menu based J2ME Applications.
A Serialization Example in Java

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
Comparison operators in JSP
What are the different scopes in JSP?
JSP Alert Example
Sending Email using JSP
JSP CheckBox Example
JSP Program for display Date
Writing your first JSP page
Example Using Initialization Parameters
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: ` Permission denied <null>:80
Using a DataSource from WebLogic in a JSP
The Request Object in JSP
Steps to get a Free SSL certificate for your Tomcat
Automatically Refreshing a JSP
What is JSP?
Most Emailed Articles (in JSP)
Sending Email using JSP
The Servlet Life Cycle
Select Object
Embedding a QuickTime movie in a JSP
Embedding MP3 audio file in a JSP
Querying Data with the JSTL in JSP
Combining Scriptlets with HTML
Form processing in JSP
JSP Program for display Date
An Example Using Servlet Initialization and Page Modification Dates
WebClient: Talking to Web Servers Interactively
Checkbox Object
Handling Events Connection refused: connect
Calling JSP from a batch file