<%@ page import="java.util.*" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/ea/core" %> JSTL: EcmaScript EL Support -- Conditional Operator Example

Conditional Operators

The Conditional operator ?: can be used as a short cut for an if/else statement. A JSTL conditional tag can also be used to verify the test condition.

<% request.setAttribute("true", "true"); %> <% request.setAttribute("false", "false"); %> <% request.setAttribute("ifResult", "IF result"); %> <% request.setAttribute("elseResult", "ELSE result"); %> <% request.setAttribute("myDate", new Date()); %>

Here are some examples:

Conditional Operators:

Operation Result
? :
? :
<c:if test="$true == true"> IF result </jx:if> IF result
<c:if test="$false == true"> IF result </jx:if> IF result
<c:if test="$myDate.year == 101"> 1900 + 101 = 2001 </jx:if> 1900 + 101 = 2001