Java jstl core tags choose when otherwise
The JSTL <c:choose>
, <c:when>
, and <c:otherwise>
tags provide a way to implement conditional logic in JSP pages.
The basic syntax for the <c:choose>
tag is as follows:
<c:choose> <c:when test="condition1">result1</c:when> <c:when test="condition2">result2</c:when> ... <c:otherwise>defaultResult</c:otherwise> </c:choose>
The <c:when>
tags are used to specify conditions that are tested in order, and the first condition that evaluates to true
causes the corresponding result to be output. The <c:otherwise>
tag is used to specify a default result that is output if no <c:when>
condition is true
.
Here's an example that shows how to use the <c:choose>
, <c:when>
, and <c:otherwise>
tags to implement conditional logic in a JSP page:
<c:choose> <c:when test="${pageContext.request.method == 'GET'}"> <p>This page was requested using a GET request.</p> </c:when> <c:when test="${pageContext.request.method == 'POST'}"> <p>This page was requested using a POST request.</p> </c:when> <c:otherwise> <p>This page was not requested using a GET or POST request.</p> </c:otherwise> </c:choose>
In this example, the conditions being tested are the HTTP request method (GET
or POST
). The result for each condition is a different message that will be output in the HTML page.
The <c:choose>
tag, along with its associated tags, provides a powerful and flexible way to implement conditional logic in JSP pages.