Tuesday, 20 March 2018

Empty and null check in JSTL JAVA

Difference between empty and null check of JSTL Core tag

< c:if test="${param.name != null}">< /c:if>


The above tag is to check whether parameter name is null or not. If it is not null then it prints out the sentence and it prints nothing if the value is null.

< c:if test="${param.name ne null}">< /c:if>

A test shows that parameter name contains value of ${param.name} via "ne" operator

Above codes are working similarly with the previous codes. We just want to test JSP notation “ne” for not equal function as what “!=” does.

< c:if test="${not empty(param.name)}">< /c:if>



A test shows that parameter name contains value of ${param.name} via empty operator.

Above codes are another feature of JSP notation to check a value whether it is empty or not. It is basically pretty much the same as the second and third tag do. The difference lies on the value null and empty. 
  
So What is the difference between empty and null? 
  
Well, empty is not necessarily be null but null always be empty. Null means that the variable is simply not existed while empty means that the variable is existed and initialized but it contains nothing. Please be careful when dealing with null values as it may cause you the famous NullPointerException.

No comments:

Post a Comment