By: Fazal in JSP Tutorials on 2007年09月23日 [フレーム]
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.
arithmetic.jsp
<html>
<head>
<title>Arithmetic</title>
<style>
body, td {font-family:verdana;font-size:10pt;}
</style>
</head>
<body>
<h2>EL Arithmetic</h2>
<table border="1">
<tr>
<td><b>Concept</b></td>
<td><b>EL Expression</b></td>
<td><b>Result</b></td>
</tr>
<tr>
<td>Literal</td>
<td>${'${'}10}</td>
<td>${10}</td>
</tr>
<tr>
<td>Addition</td>
<td>${'${'}10 + 10 }</td>
<td>${10 + 10}</td>
</tr>
<tr>
<td>Subtraction</td>
<td>${'${'}10 - 10 }</td>
<td>${10 - 10}</td>
</tr>
<tr>
<td>Multiplication</td>
<td>${'${'}10 * 10 }</td>
<td>${10 * 10}</td>
</tr>
<tr>
<td>Division / </td>
<td>${'${'}10 / 3 }</td>
<td>${10 / 3}</td>
</tr>
<tr>
<td>Division DIV</td>
<td>${'${'}10 div 3 }</td>
<td>${10 div 3}</td>
</tr>
<tr>
<td>Modulus</td>
<td>${'${'}10 % 3 }</td>
<td>${10 % 3}</td>
</tr>
<tr>
<td>Modulus</td>
<td>${'${'}10 mod 3 }</td>
<td>${10 mod 3}</td>
</tr>
<tr>
<td>Precedence</td>
<td>${'${'}2 * 5 mod 3 }</td>
<td>${2 * 5 mod 3}</td>
</tr>
<tr>
<td>Precedence with parens</td>
<td>${'${'}2 * (5 mod 3) }</td>
<td>${2 * (5 mod 3)}</td>
</tr>
<tr>
<td>Division by Zero</td>
<td>${'${'}10 / 0 }</td>
<td>${10 / 0}</td>
</tr>
<tr>
<td>Exponential</td>
<td>${'${'}2E2}</td>
<td>${2E2}</td>
</tr>
<tr>
<td>Unary Minus</td>
<td>${'${'}-10}</td>
<td>${-10}</td>
</tr>
</table>
</body>
</html>
To run this example, you need to deploy it into a JSP 2.0 or JSP 2.1 compliant web container:
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in JSP )
Show a calendar for user input in JSP
Encrypting Passwords in Tomcat using Servlets
JSP Tags for SQL to connect to a database
Steps to get a Free SSL certificate for your Tomcat
Uploading a file to a server using JSP
Server Side Programming using JSP
Latest Articles (in JSP)
Show a calendar for user input in JSP
Steps to get a Free SSL certificate for your Tomcat
Encrypting Passwords in Tomcat using Servlets
JSP Tags for SQL to connect to a database
Uploading a file to a server using JSP
Uploading an Image to a Database using JSP
A JSP page that gets properties from a bean
Scriptlets and Expressions in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Show a calendar for user input in JSP
Encrypting Passwords in Tomcat using Servlets
Steps to get a Free SSL certificate for your Tomcat
JSP Tags for SQL to connect to a database
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate