This is for implementing core java if statement
There are two forms available for <c:if>

  1. without body
    <c: if test = "test-condition"  var = "x" scope = "request"/>

    In this case test condition will be evaluated and it’s result into variable x.

    In the rest of the page where ever the same test condition is required we can use directly it’s value without evaluating once again. In this case both test and var attributes are mandatory.

    The scope attribute is optional & default scope is page.

  2. With body
    <c: if test = "test-condition" var= "x" scope = "request">
    </c: if>

    If the test condition is true then the body will be executed other wise with out executing body rest of the page will be continued normally.

    In this case also we can store test result for the future purpose.

    Here test attribute is the mandatory and both var and scope attributes are optional.


    <c: set var = "x" value = "10"/>
    <c: if test = "${x eq '10'}" var = "y" scope = "session"/>
    X value is : ${x}---->10
    Test result is : ${y}---->true

<c: choose>,<c: when>and<c: otherwise>

We can use these tags for implementing if else and switch statements.
Implementing if else syntax
JSTL doesn’t contain any tag directive to implement if else syntax. We can implement this indirectly as follows

 <c:when test = "test-condition">
 <c:other wise>
 </c:other wise>

If the condition is true then Action1 will be executed other wise Action2 will be executed.

Implementing switch statement

<c:when test = "test-cond1">
</c: when>
<c:when test = "test-cond2">
// Action2
<c:when test = "test-cond3">
Default action
  1. <c:choose>should compulsory contain atleast one <c:when>
  2. <c: other wise> is optional.
  3. <:other wise> should be compulsory last case.
  4. <c:choose>and <c: otherWise>won’t take any attributes but <c: when> should contain only one attribute test.
  5. every <c:when>tag implicitly contains break stmt hence there is no chance of fall through inside a switch.
<h1> select the number.
<from action = "/jsp1/switch.jsp”>
<select name = "combo">
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "7">7</option>
<input type = "submit"/>
<c: set var = "s" value = "${param.combo}"/>

Today is:
<c:when test = "${s==1}"> Sunday </c:when>
<c:when test = "${s==2}"> monday </c:when>
<c:when test = "${s==3}"> tuesday </c:when>
<c:when test = "${s==4}"> wednesday</c:when>
<c:when test = "${s==5}"> thursday</c:when>
<c:otherwise> select b/w 1 and 5 </c:otherwise>

Various translation error.

  1. Illegal “choose” without child “when” tag
  2. Illegal “c:when” after “c:otherwise” tag in “c:choose” tag.

Summary of conditional tags

Tag Description Attributes

To implement corejava
if statement.
<c:choose>,<c:when>,<c:otherWise> To implement if else and switch statements <c:when> contains only test Attribute
<c:choose>,<<c:otherWise> doesn’t contain any Attribute