Wednesday, 5 July 2017

JSP - How to check if ArrayList is Empty using JSTL Example

There are multiple ways to check if an ArrayList is empty in JSP or not. For example, you can use the empty operator and the length function of JSTL to check if a list is empty or not. You can also use scriptlet, which allows you to insert Java code into JSP but that is not advisable because it makes it harder to maintain a JSP page. You can also write custom tag but that is not required because JSTL provides you necessary support. In this article, I'll show you two ways to check if a given list or ArrayList is empty or not in JSP page without using the scriptlet. You guessed it right, we will use JSTL, the Java standard tag library to solve this problem.

The first way is to use JSTL tag and empty operator to check if an ArrayList is the empty and second way is to use JSTL function, fn: length() instead of the empty operator as shown in our example. Unfortunately, both of these options are not obvious to anyone who is not a power-coder in JSP or JSTL, but if you really want to work with JSP that is the area you need to improve, using Java code in JSP is not the solution.

Solution 1 - Use empty Operator


The easiest way to find if a list is empty or not is by using the empty operator with the if tag of JSTL. This operator returns true if the list is empty, but you can also use it test if String, array, or Map is empty as shown below:

<c:if test="${empty listOfCurrency}">

The empty operator return true if the operand is null, an empty String, empty array, empty Map, or empty List; false, otherwise.

The only problem with using the empty operator is that it's not very intuitive, so until you know the exact syntax you cannot use it by just guessing. For example, following code will not work in JSP, even if you use JSP:

<c:if test="${listOfCurrency.size() == 0 }">    

<c:if test="${listOfCurrency.size == 0 }">  

<c:if test="${listOfCurrency.length == 0 }">   

You can learn more about the empty operator and if tag of JSTL in the class Head First Servlet and JSP book. Their chapter on JSTL and Custom tag is the best material to master this key concept in JSP development.

Solution 2 - Use fn:length() function


Another way to check if an ArrayList is empty or not is by using JSTL's length() function, as shown below:

<c:if test="${fn:length(list) > 0}">

but you need to import another tld file as shown below

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

The fn:length accepts a collection or string and returns an int which represents the size of the collection or array if a collection is passed and the number of characters if a string is passed.

Sample JSP to Check if ArrayList is Empty


Here is the JSP page which demonstrates how you can use both empty operator and length() function to check if an ArrayList is empty or not:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Arrays"%>
<%@page import="java.util.ArrayList"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html> 
          <head> 
                   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
                   <title> How to check if ArrayList is empty in JSP?</title> 
          </head>

          <body> 
                    <h2> 2 ways to check if ArrayList is empty in JSP</h2> 
                              <ol> 
                                       <li>By using empty operator</li>  
                                       <li>By using fn:length() function</li> 
                              </ol>
                              
<% 
                                       // Avoid Java Code in JSP - This is only for testing List<String> currencies = Arrays.asList("USD", "AUD", "INR", "JPY"); List<String> fortune500 = new ArrayList<String>(); // put the List as pageContext attribute pageContext.setAttribute("currencies", currencies); pageContext.setAttribute("fortune500", fortune500); 
                                %>

                     <h4>Testing if given list is emtpy using 'empty' operator</h4> 
                     <c:if test="${empty currencies}"> 
                                  <p>Yes, Currency ArrayList is empty" </p>
                     </c:if> 
                                   <p>No, Currency ArrayList is not empty" </p>
                   <h5>Testing again if List is empty</h5> 
                   <c:if test="${empty fortune500}">  
                                   <p> Yes, Fortune500 ArrayList is empty" <p/> 
                    </c:if> 

                     <h4>Checking if ArrayList is empty using fn:length() function</h4> 
                     <c:if test="${fn:length(fortune500) == 0}"> 
                     <p>Yes, ArrayList is empty" <p/> 
                     </c:if>

   </body>
</html> 

and here is the screenshot of the JSP page, when I run it from my Netbeans IDE:

Oracle Database JSP, Oracle DB JSTL

You can see that for currency list, which contains INR, USD, and JPY the empty operator return false hence it is printed as "no Currency ArrayList is not empty", while for Fortune500 ArrayList, the empty operator returns true, hence you see the output "Yes, Fortune500 ArrayList is empty".

That's all about how to check if an ArrayList is empty in JSTL. It's your choice to use either empty operator or length function, but remember if you use JSTL function, you need to import them using @taglib directory. They have not imported automatically, not even when you import JSTL core tags like if, forEach() and others.