Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

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 a custom tag but that is not required because JSTL provides you the necessary support. In this article, I'll show you two ways to check if a given list or ArrayList is empty or not on the 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 the JSTL tag and empty operator to check if an ArrayList is empty and the second way is to use the 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 the tag of JSTL. This operator returns true if the list is empty, but you can also use it a 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 the 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"
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title> How to check if ArrayList is empty in JSP?</title>

        <h2> 2 ways to check if ArrayList is empty in JSP</h2>
            <li>By using empty operator</li>
            <li>By using fn:length() function</li>

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

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


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

How to check if ArrayList is Empty using JSTL Example

You can see that for the 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 the JSTL function, you need to import them using the @taglib directory. They have not imported automatically, not even when you import JSTL core tags like if, forEach(), and others.

Other JSP and JSTL tutorials you may like to explore
  • What is the difference between include directive and include action in JSP? (answer)
  • What is the difference between Servlet and JSP? (answer)
  • What is the difference between forward and sendRedirect? (answer)
  • The real difference between include and forward action in JSP? (answer)
  • Can you declare the constructor inside Servlet? (answer)
  • The real difference between constructor and init() method in Servlet? (answer)
  • The Java Programming Interview Exposed (book)

Thanks for reading this article so far. If you like this tutorial then please share it with your friends and colleagues. If you have questions then please drop a note and I'll try to find an answer for you. If you have any other interesting JSP questions, feel free to share.

No comments :

Post a Comment