1

I wrote the following code:

<%
        int accountNumber = Integer.parseInt(request.getParameter("accountNumber"));
        int depositAmount = Integer.parseInt(request.getParameter("depositAmount"));
    %>
    <sql:query var='account' dataSource="jdbc/bank">
        select * from account where AccountNumber=<%= accountNumber %>      
    </sql:query>
    <c:forEach var="result" begin="0" items="${account.rows}">
        <c:set var="balance" value="${ result.balance + depositAmount }" />
        <c:out value="${ balance }" />
    </c:forEach>

The problem is that for <c:set var="balance" /> it isn't actually adding the two values together.

I'm assuming that depositAmount isn't recognized? I'm not sure why.

Can someone please explain how I can use JSTL to get the request parameter (balance) and add it to the balance obtained in the query?

This is a homework assignment where I must use JSP.

Thank you

Brian
  • 5,951
  • 14
  • 53
  • 77

1 Answers1

3

Scriptlets (those <% %> things) and EL (those ${} things) doesn't share the same variable scope.

Get rid of scriptlets and use EL only. Request parameters are in EL available by just ${param.name}.

<sql:query var="account" dataSource="jdbc/bank">
    select * from account where AccountNumber=${param.accountNumber}      
</sql:query>
<c:forEach var="result" begin="0" items="${account.rows}">
    <c:set var="balance" value="${result.balance + param.depositAmount}" />
    <c:out value="${balance}" />
</c:forEach>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That worked perfect. Thank you for the information and the help. I've been banging my head off a desk for the last two hours. – Brian May 23 '11 at 15:40