JSP EL Logical Operators

JSP El Logical

  • EL enables you to perform logical operations on Boolean arguments.
  • This is used primarily for testing values of custom tag attributes combined with Comparisons.
  • You can also write out the result of a logical evaluation (true or false) to the JSP page.
  • There are three operators with alternatives that you can use and combine to achieve the logical result.
    Here is the difference logicals in Expression Language (EL) :
    Type Description Example
    && or and logical 'and' $(amount>5 && amount<7}   // will write true if the amount is equal to 6; else false
    || or or logical 'or' ${amount<=5 or amount>=7}   // will write false if the amount is equal to 6; else true
    ! or not logical 'not' ${!amount<=5 or amount>=7}   // will write true if the amount is equal to 6; else false
  • Each operator has a precedent that determines the order of evaluation. You'll find this precedent here.

Example of JSP El Logical operators.

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

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 Logical Operators</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 logicals</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">
              <td><b>Type</b></td>
              <td><b>EL Logic</b></td>
              <td><b>Result</b></td>
            </tr>
            <tr class="data">
              <td>And (and)</td>
              <td>${'${'}true and true}</td>
              <td>${true and true}</td>
            </tr>
            <tr class="data">
              <td>And (&&)</td>
              <td>${'${'}true && false}</td>
              <td>${true && false}</td>
            </tr>
            <tr class="data">
              <td>Or (or)</td>
              <td>${'${'}true or true}</td>
              <td>${true or false}</td>
            </tr>      
            <tr class="data">
              <td>Or (||)</td>
              <td>${'${'}true || false}</td>
              <td>${true || false}</td>
            </tr>
            <tr class="data">
              <td>Not (not)</td>
              <td>${'${'}not true}</td>
              <td>${not true}</td>
            </tr>
            <tr class="data">
              <td>Not (!)</td>
              <td>${'${'}!false}</td>
              <td>${!false}</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>logicalList</servlet-name>
            <jsp-file>/index.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>logicalList</servlet-name>
            <url-pattern>/logicalList</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>logicalList</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 logicalList. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /EL_Logical) 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" 
    "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
    <sun-web-app error-url="">
      <context-root>/EL_Logical</context-root>
      <class-loader delegate="true"/>
      <jsp-config>
        <property name="keepgenerated" value="true">
          <description>
            Keep a copy of the generated servlet class' java code.
          </description>
        </property>
      </jsp-config>
    </sun-web-app>

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.