JSP EL Arithmetic Evaluation

JSP El Arithmetic Evaluation

  • There are many cases within web-application development where you need to perform some mathematics on a page.
  • 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
    Here is the Arithmetic Evaluation in Expression Language (EL) :
    Type Description Example
    + Addition $(counter+3}    // will output the value of the counter plus 3
    - Subtraction ${counter-3}    // will output the value of the counter minus 3
    * Multiplication ${counter*3}    // will output the value of the counter multiplied by 3
    / or div Division ${counter/3}    // will output the value of the counter divided by 3
    % or mod Modulus ${counter%3}    // will output the REST value of the counter divided by 3
    E Exponents (results in floating type) ${1E3 mod 26}    // will output the value the floating point value 12.0
  • Each operator has a precedent that determines the order of evaluation. You'll find this precedent here.

Example of JSP El Arithmetic evaluation.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is EL_Arithmetic).

In this example, we will add two files, header.jsp and footer.jsp.

  • It is customary to place all include files, which are used several times in the WEB-INF folder. Some would also like the include files should have the extension .jspf, but this is not a requirement.
    Here is the file we want to include at the top of all pages:
    <h2 class="siteblue">JSP EL Arithmetic Evaluation</h2>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Header.jsp and folder should be WEB-INF).

    Here is the file we want to include at the bottom of all pages:
    <hr >
    <jsp:useBean id="today" class="java.util.Date" scope="page" />
    <span class="siteblue">Listing date ${today}</span>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Footer.jsp and folder should be WEB-INF).

We need a JSP file(s) to demonstrate use of JSP EL Arithmetic Evaluation.

  • <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP EL Arithmetic Evaluation</title>
          .gradientdown {
            background:  #e0e6ff;
            background: -webkit-linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(#e0e6ff, #eeeeee); /* For Opera 11.1 to 12.0 */
            background: -moz-linear-gradient(top, #e0e6ff, #eeeeee); /* For Firefox 3.6 to 15 */
            background: linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* W3C Standard syntax (must be last) */
          table tr.data:nth-of-type(even) { 
            background: #eeeeee; 
          table { 
            border-color: #aad5ff;
            border-style: solid;
            border-width: 0 0 1px 1px;
            border-spacing: 0;
            border-collapse: collapse;
          tr,th,td {
            border-color: #aad5ff;
            border-style: solid;
            padding: 2px;
            border-width: 1px 1px 0 0;
            vertical-align: central;  
          table tr th {
            color: blue;
          table tr td:first-child {
            font-weight: bold;
          hr { background-color: #3366ff; height: 1px; border: 0; }
          .siteblue {
            color: #3366ff;
        <div style="max-width:600px; ">
          <%@ include file="/WEB-INF/header.jsp" %>
          <table style="width: 100%;">
            <tr class="gradientdown">
            <tr class="data">
            <tr class="data">
              <td>Addition (+)</td>
              <td>${'${'}10 + 10 }</td>
              <td>${10 + 10}</td>
            <tr class="data">
              <td>Subtraction (-)</td>
              <td>${'${'}16 - 14 }</td>
              <td>${16 - 14}</td>
            <tr class="data">
              <td>Multiplication (*)</td>
              <td>${'${'}13 * 10 }</td>
              <td>${13 * 10}</td>
            <tr class="data">
              <td>Division (div)</td>
              <td>${'${'}13 div 7 }</td>
              <td>${13 div 7}</td>
            <tr class="data">
              <td>Division (/) </td>
              <td>${'${'}10 / 6 }</td>
              <td>${10 / 6}</td>
            <tr class="data">
              <td>Modulus (mod)</td>
              <td>${'${'}10 mod 7 }</td>
              <td>${10 mod 7}</td>
            <tr class="data">
              <td>Modulus (%)</td>
              <td>${'${'}12 % 7 }</td>
              <td>${12 % 7}</td>
            <tr class="data">
              <td>${'${'}2 * 6 mod 3 }</td>
              <td>${2 * 6 mod 3}</td>
            <tr class="data">
              <td>Precedence with parents</td>
              <td>${'${'}2 * (7 mod 3) }</td>
              <td>${2 * (7 mod 3)}</td>
            <tr class="data">
              <td>Division by zero</td>
              <td>${'${'}12 / 0 }</td>
              <td>${12 / 0}</td>
            <tr class="data">
              <td>${'${'}3E2 div 13}</td>
              <td>${3E2 div 13}</td>
            <tr class="data">
              <td>Unary Minus (- unary)</td>
              <td>${'${'}-12 +2}</td>
          <%@ include file="/WEB-INF/footer.jsp" %>      

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is index).

Creating Deployment descriptor.

  • To run this JSP you have to deploy it to a web-server or a Application server. To deploy means to install the JSP with some instruction to a such server.
  • The instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.

    You may need to create a Deployment descriptor file, web.xml in Netbeans.

  • The contents of the web.xml file should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a JSP file.
  • With a servlet tag we give the JSP file a servlet name, which is used in the servlet-mapping tag to specify a url for the JSP file.
  • In this way we can have many urls for the same JSP file.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the welcome file with url arithmeticList. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /EL_Arithmetic) for the application will in most cases be specified by a server vendor deployment descriptor.

    For those who participate in the review: create a Glassfish deployment descriptor in Netbeans.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, 
    Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" 
    <sun-web-app error-url="">
      <class-loader delegate="true"/>
        <property name="keepgenerated" value="true">
            Keep a copy of the generated servlet class' java code.

Run the application.

  • For those who participate in the review: right click the Web-project and select Run.
  • This results in that the application will be deployed to the server and started.
  • The browser will display:
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.