Search code examples
jspjstl

How do I compare variables between a scriptlet and JSTL?


I have JSP code of the form

<%
    int count = 1;
    request.setAttribute("count", count);
%>
<c:set var="max" scope="request" value="${100}" />
<c:if test="${request.getAttribute('count') < max}" >
  ...
</c:if>

That is, the test compares a variable defined in a scriptlet to a variable defined using <c:set>. The code fails with an error thrown at the line of the test condition. I've tried all kinds of variations on this, and I can't find anything online that explains the interaction between scriptlets and JSP EL clearly enough for me to figure out what I should do here.

How do I compare a scriptlet variable to a JSTL variable using JSTL?

Edit The error thrown is

org.apache.jasper.JasperException: An exception occurred processing JSP page

I assume that's as useless as it sounds, but there it is in case it means anything to anyone.


Solution

  • You can directly access the count variable because you already set it in the request scope:

    test="${count < max}" or test="${requestScope.count < max}"