JSTL - if else statements using c:choose c:when c:otherwise

Home > Search
  by

This assumes you are already familiar with creating variables using c:set and printing variables using c:out. Let's take an example of a variabled called name that contains a value of Jeremy.

<c:set var="name" value="Jeremy"/>

 

The following if statement will print "Congratulations. Your name is Jeremy." if name equals Jeremy, or print "Opps. Your name is not Jeremy!" if name does not equal Jeremy.

<c:choose>
  <c:when test="${name == 'Jeremy'}">
    Congratulations. Your name is Jeremy.    
  </c:when>
  <c:otherwise>
    Opps. Your name is not Jeremy!
  </c:otherwise>
</c:choose>

 

Running this application should print "Congratulations. Your name is Jeremy." as long as the value of name is Jeremy.

 

Otherwise, "Opps. Your name is not Jeremy!" will be displayed.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments