The variable ctr
created in the page scope. To access page scope variable in JSP expression you can use pageContext
implicit object.
<table>
<% pageContext.setAttribute("questions", Questions.QUESTION_ARRAY); %>
<c:forEach var="question" items="${questions}" varStatus="ctr">
<tr>
<td>
<%=Questions.QUESTION_ARRAY[((LoopTagStatus)pageContext.getAttribute("ctr")).getIndex()]%>
</td>
</tr>
</c:forEach>
</table>
But it seems ugly if you are using it with JSTL forEach
tag. Better to construct the JSP EL expression.
<table>
<% pageContext.setAttribute("questions", new Questions(){}.QUESTION_ARRAY); %>
<c:forEach var="question" items="${questions}" varStatus="ctr">
<tr>
<td>
${questions[ctr.index]}
</td>
</tr>
</c:forEach>
</table>
Even this expression not necessary if you are using var
attribute of the forEach
tag which defines the variable referencing the element of the array, also in the page scope. You can access it like
<table>
<% pageContext.setAttribute("questions", Questions.QUESTION_ARRAY); %>
<c:forEach var="question" items="${questions}" >
<tr>
<td>
${question}
</td>
</tr>
</c:forEach>
</table>
Also see this question for other alternatives:
How to reference constants in EL?