In this section, you will learn how to use the jstl when tag in jsp. Basically, the choose, when, and otherwise tags can be used to construct an if statement. The choose tag embedded the subtag when which evaluates the test condition. If none of the test conditions of when tags evaluates to true, then the otherwise tag is evaluated. In the given example, we prompt the user to enter to enter any number. After submitting the button, a message will be displayed showing whether the number is odd or even.
Here is the code of when.jsp:
| <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <body> <form method="post">Enter a number: <input type="text" name="enter" /> <input type="submit" value="Accept" /> <br /> </form> <c:if test="${pageContext.request.method=='POST'}">Entered value is: <c:choose> <c:when test="${param.enter%2==0}">even. <br /> </c:when> <c:otherwise>odd. <br /> </c:otherwise> </c:choose> </c:if> </body> </html> |
Output will be displayed as:

If the entered number is even:

If the entered number is odd:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: JSP JSTL c:when tag
Post your Comment