JSP EL Comparisons.

JSP El Comparisons

  • A useful feature of the EL is to perform comparisons, either between object or numbers.
  • This is used primarily for testing values of custom tag attributes.
  • You can also write out the result of a comparison (true or false) to the JSP page.
  • There are six operators with alternatives that you can use and combine to achieve the comparison.
    Here is the difference comparisons in Expression Language (EL) :
    Type Description Example
    != or ne Not equal $(amount ne 5}    // will write true if the amount not equal 5; else false
    == or eq Equal ${amount == 3}    // will write true if the amount is equal to 3; else false
    > or gt greater than ${amount gt 3}    // will write true if the amount is greater than 3; else false
    >= or ge greater or equal to ${amount ge 3}    // will write true if the amount is greater or equal to 3; else false
    < or lt less than ${amount lt 3}    // will write true if the amount is less than 3; else false
    <= or le less or equal to ${amount %lt;= 3}    // will write true if the amount is less or equal to 3; else false
  • Each operator has a precedent that determines the order of evaluation. You'll find this precedent here.

Example of JSP El Comparisons.

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_Comparisons).

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>
    <hr>

    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 Comparisons.

  • <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP EL Comparisons</title>
        <style>
          .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;
          }
    
        </style>    
      </head>
      <body>
        <div style="max-width:500px; ">
          <%@ include file="/WEB-INF/header.jsp" %>
          <table style="width: 100%;">
            <tr class="gradientdown">
              <th>Type</th>
              <th>Comparison</th>
              <th>Result</th>
            </tr>
            <tr class="data">
              <td>Numeric less than</td>
              <td>${'${'}1 < 2}</td>
              <td>${1 < 2} 
            </tr>
            <tr class="data">
              <td>Numeric greater than</td>
              <td>${'${'}1 > 2}</td>
              <td>${1 > 2}</td>
            </tr>
            <tr class="data">
              <td>Numeric less than</td>
              <td>${'${'}1 lt 2}</td>
              <td>${1 lt 2}</td>
            </tr>
            <tr class="data">
              <td>Numeric greater than</td>
              <td>${'${'}1 gt 2}</td>
              <td>${1 gt 2}</td>
            </tr>
            <tr class="data">
              <td>Numeric Greater than or equal</td>
              <td>${'${'}1 >= 1}</td>
              <td>${1 >= 1}</td>
            </tr>
            <tr class="data">
              <td>Numeric Less than or equal</td>
              <td>${'${'}1 <= 1}</td>
              <td>${1 <= 1}
            </tr>
            <tr class="data">
              <td>Numeric less than or equal</td>
              <td>${'${'}1 le 1}</td>
              <td>${1 le 1}</td>
            </tr>
            <tr class="data">
              <td>Numeric greater than or equal</td>
              <td>${'${'}1 ge 1}</td>
              <td>${1 ge 1}</td>
            </tr>
            <tr class="data">
              <td>Numeric equal to</td>
              <td>${'${'}1 == 1}</td>
              <td>${1 == 1}</td>
            </tr>
            <tr class="data">
              <td>Numeric equal to</td>
              <td>${'${'}1 eq 1}</td>
              <td>${1 eq 1}</td>
            </tr>
            <tr class="data">
              <td>Numeric not equal to</td>
              <td>${'${'}1 != 2}</td>
              <td>${1 != 2}</td>
            </tr>
            <tr class="data">
              <td>Numeric not equal to</td>
              <td>${'${'}1 ne 2}</td>
              <td>${1 ne 2}</td>
            </tr>
            <tr class="data">
              <td>Alphabetic less than</td>
              <td>${'${'}'abe' < 'ade'}</td>
              <td>${'abe' < 'ade'}
            </tr>
            <tr class="data">
              <td>Alphabetic greater than</td>
              <td>${'${'}'abe' > 'ade'}</td>
              <td>${'abe' > 'ade'}</td>
            </tr>
            <tr class="data">
              <td>Alphabetic equal to</td>
              <td>${'${'}'abe' eq 'abe'}</td>
              <td>${'abe' eq 'abe'}</td>
            </tr>
            <tr class="data">
              <td>Alphabetic not equal to</td>
              <td>${'${'}'abe' ne 'ade'}</td>
              <td>${'abe' ne 'ade'}</td>
            </tr>      
            <tr class="data">
              <td>Alphabetic equal to with  function</td>
              <td>${'${'}'abe'.concat('d') == 'abcd'}</td>
              <td>${'abc'.concat('d') == 'abcd'}</td>
            </tr>      
          </table>
          <%@ include file="/WEB-INF/footer.jsp" %>      
        </div>
      </body>
    </html>

    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"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
        <servlet>
            <servlet-name>comparisonList</servlet-name>
            <jsp-file>/index.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>comparisonList</servlet-name>
            <url-pattern>/comparisonList</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>comparisonList</welcome-file>
        </welcome-file-list>
    </web-app>
  • 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 comparisonList. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /EL_Comparisons) for the application will in most cases be specified by a server vendor deployment descriptor.
    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.