JSTL Check String Empty OR NULL

JSTL Check String Empty OR NULL explains about how to evaluate a string whether it is empty or null inside JSP

Consider a JSP Page where you need to check a string and process accordingly, in this case you can follow this example.

You can check a string whether it is empty or not using "empty" keyword in JSTL

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

Above code will check value of str1 is empty or null

Required Libraries

You need to download

  1. Tomcat 9
  2. JSTL 1.2

Following jar must be in classpath

  1. jstl-1.2.jar

Project Structure

JSTL Check String Empty OR NULL

JSTL Check String Empty OR NULL

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<title>Evaluate String Empty OR NULL JSTL</title>
</head>
<body>

	<%
	   // you can also set the values into using c:set
	   request.setAttribute("str1", null);
	   request.setAttribute("str2", "hello");
	%>

	<c:if test="${empty str1}">
    	str1 is empty or null.
	</c:if>
	<br/>
	<c:if test="${not empty str2}">
    	str2 is not empty or null.
	</c:if>

</body>
</html>
Output
str1 is empty or null.
str2 is not empty or null.

 











1 Responses to "JSTL Check String Empty OR NULL"
  1. Akanksha 2013-06-25 10:44:11.0

Your email address will not be published. Required fields are marked *