JSTL c:choose, c:when, c:otherwise Example

This source code example shows how to use the <c:choose>, <c:when> and <c:otherwise> tags in the JSTL core tags library.
All JSP Examples at JSP Source Code Examples
These tags are used together like switch-case and default statements in Java. <c:choose> is the one which acts like switch, <c:when> like case which can be used multiple times inside <c:choose> for evaluating different-2 conditions. <c:otherwise> is similar to default statement which works when all the <c:when> statements holds false.

Syntax:

The basic structure looks like this –
<c:choose>
    <c:when test="${condition1}">
       //do something if condition1 is true
    </c:when>
    <c:when test="${condition2}">
        //do something if condition2 is true
    </c:when>
    <c:otherwise>
        //Statements which gets executed when all <c:when> tests are false.
    </c:otherwise>
</c:choose>

Example:

Student.java

Let's first create Student bean class:
package net.javaguides.jstl;

public class Student {

    private String firstName;
    private String lastName;
    private boolean goldCustomer;

    public Student(String firstName, String lastName, boolean goldCustomer) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.goldCustomer = goldCustomer;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public boolean isGoldCustomer() {
        return goldCustomer;
    }

    public void setGoldCustomer(boolean goldCustomer) {
        this.goldCustomer = goldCustomer;
    }
}
In this example, we will iterate over each student and check whether the student is a gold member or not: 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ page import="java.util.*,net.javaguides.jstl.Student" %>

<%
 // just create some sample data ... normally provided by MVC
 List<Student> data = new ArrayList<>();

 data.add(new Student("Ramesh", "Fadatare", false));
 data.add(new Student("John", "Cena", false));
 data.add(new Student("Tom", "Cruise", false));
 data.add(new Student("Tony", "Stark", false));
 data.add(new Student("Prakash", "Jadhav", true));
 pageContext.setAttribute("myStudents", data);
%>
<html>
<body>
 <table border="1">
 <tr>
  <th>First Name</th>
  <th>Last Name</th>
  <th>Gold Customer</th>
 </tr> 
 <c:forEach var="tempStudent" items="${myStudents}">  
  <tr>
   <td>${tempStudent.firstName}</td>
   <td>${tempStudent.lastName}</td>
   <td>
    <c:choose>
    
     <c:when test="${tempStudent.goldCustomer}">
      Special Discount
     </c:when>
     
     <c:otherwise>
      no soup for you!
     </c:otherwise>

    </c:choose>    
   </td> 
  </tr>  
 </c:forEach>
 </table>
</body>
</html>
Let's invoke this JSP page from a Web browser, you see the table on a browser:

All JSP Examples at JSP Source Code Examples.

Comments