Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • if-else in jstl

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 981
    Comment on it

    In JSTL, the functionality if-else is provided by choose-when-otherwise statement .

    <c:choose> tag is an enclosing tag for <c:when> and <c:otherwise>. And it is also known as mutual exclusion as only a block of code within the condition of mutual exclusion be executed. <c:when> and <c:otherwise> both come under this <c:choose> tag.


    <c:when> tag is equivalent to the case statements of a switch statement. <c:when> statements are considered as more advanced than case statements as switch case statements only compares the variable mentioned in the switch statement w.r.t the constant in case statement while the <c:when> tag evaluates the expressions.


    <c:otherwise> tag is equivalent to the else statements.

    Example:

    1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    2. <html>
    3. <head>
    4. <title> Choose,Otherwise and When (if-else in JSTL)</title>
    5. </head>
    6.  
    7. <body>
    8. <c:if test="${pageContext.request.method=='POST'}">I have
    9. <c:out value="${param.enter}" />
    10.  
    11. <c:choose>
    12.  
    13. <c:when test="${param.enter=='0'}">(means no ) notebook.
    14. <br />
    15. </c:when>
    16.  
    17. <c:when test="${param.enter=='1'}">notebook.
    18. <br />
    19. </c:when>
    20.  
    21. <c:otherwise>notebooks.
    22. <br />
    23. </c:otherwise>
    24.  
    25. </c:choose>
    26. </c:if>
    27.  
    28. <form method="post">Enter a number:
    29. <input type="text" name="enter" />
    30.  
    31. <input type="submit" value="accept" />
    32.  
    33. <br />
    34. </form>
    35. </body>
    36. </html>

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: