Gautam Bhalla
Gautam Bhalla

Reputation: 1210

Jstl working with scriptlets

<%! int x=5; %>


        <c:choose>
            <c:when test="${x eq 5}"><p>hello1</p></c:when>            
            <c:when test="${x gt 10}"><p>}hello3</p></c:when>
            <c:otherwise>Value is ${x},Not hello</c:otherwise>
        </c:choose>

Why above code is giving output of not hello from my jsp page?why it is not giving hello1 as output?

Upvotes: 0

Views: 77

Answers (1)

JB Nizet
JB Nizet

Reputation: 691715

Because ${x} doesn't evaluate local and instance variables. It looks for a page-, then request-, then session-, then application-scoped attribute named "x". The code above would work if you used

<% pageContext.setAttribute("x", 5) %>

or, much cleaner since scriptlets should be avoided:

<c:set var="x" value="5" />

Upvotes: 3

Related Questions