user1487778
user1487778

Reputation: 11

how to compare two variables in <c:when>

<% for(int count=0;count<lengthOfUser;count++) {%>
<%int tempuser= users[count]; %>

<c:forEach items="${userList}" var="user" varStatus="status">

        <c:choose>
             <c:when test="${user.userID == tempuser}">
                   hiii
                         </c:when>

    </c:choose>
</c:forEach>
<%}%>

This is my jsp page.I am Trying to comapare two variable.But this code is not workin.please help me.

Upvotes: 1

Views: 4676

Answers (1)

Visruth
Visruth

Reputation: 3450

All jstl tags will be read before any scriptlet tag. So, you have to create a variable using the jstl <c:set tag. See this example :

<% for (int i = 0; i < 3; i++) {%>
        <c:set value="${i}" var="tempuser1"></c:set>
        <c:set value="initialized with some value" var="tempuser2"></c:set>
        <c:out value="${tempuser1}"></c:out>
        <c:out value="${tempuser2}"></c:out>
        <br/>
        <%                            }
        %>

It prints in the browser :

initialized with some value 
initialized with some value 
initialized with some value

Here, <c:out value="${tempuser1}"></c:out> prints empty value because the variable (i) declared inside the scriptlet tag doesn't exist when a jstl tag is read so the variable tempuser1 (in <c:set value="${i}" var="tempuser1"></c:set>) is initialized with an empty value.

But, you can rewrite your code as follows, it works for me :

<c:forEach var="tempuser" items="users">
            <c:forEach items="${userList}" var="user" varStatus="status">

                <c:choose>
                    <c:when test="${user.userID == tempuser}">
                        hiii
                    </c:when>

                </c:choose>
            </c:forEach>
        </c:forEach>

Upvotes: 1

Related Questions