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,821
  • 14
  • 51
  • 76

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,040,783
  • 362
  • 3,548
  • 3,513
  • 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
  • You're welcome. If an answer helped in solving the problem, please don't forget to mark the answer accepted. See also http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 Do the same for the questions you asked previously, whenever applicable http://stackoverflow.com/users/393440/brian – BalusC May 23 '11 at 15:48