JSTL: check if a string is empty
java, jsp, jstl
Solution
Add the JSTL library and declare the core taglib:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
The JSTL equivalent of
<% if (request.getAttribute("error") != null) { %>
is
<c:if test="${not empty error}">
Problem
I'm trying to test in JSTL if a session attribute is empty. However the attribute is empty JSTL sees it as a non-empty attribute. This is the hardcode I'm trying to replace with JSTL. This code is working correctly: ``` <% if (request.getAttribute("error") != null) { %> <div class="alert alert-danger"> <strong>Oh snap, something's wrong, maybe the following error could help you out?<br /></strong> <%= request.getAttribute("error")%> </div> <% } %> ``` This is how I replaced it with JSTL. When checked, the error-attribute is not empty, however it is empty. ``` <c:if test="${not empty sessionScope.error}"> <div class="alert alert-danger"> <strong>Oh snap, something's wrong, maybe the following error could help you out?<br /></strong> <c:out value="${sessionScope.error}" /> </div> </c:if> ```