Checking if string is null or empty in JSTL if tag

Posted on 23-04-2013 10:06 by graham
This tutorial shows you a handy method to check whether a string is null or empty in a JSTL <c:if ...> tag.

Using the empty keyword


To check if a string is empty, use the empty keyword in the test clause:
<c:if test="${empty stringVar}">
The variable is empty
</c:if>


If you want to check if a string variable is not empty, you can use the not keyword or the ! modifier:
<c:if test="${not empty stringVar}">
The variable is not empty
</c:if>

<c:if test="${!empty stringVar}">
The variable is not empty
</c:if>
Comments

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems