0

I'm using Servlets with JSP and a Javabean to create a simple voting system. I have a JavaBean helper class (It was an assignment requirement) which gets a users cookies which contains the voting choices, tallys up the votes for each candidate, creates a new candidate object with all that info and stores the candidate object in an ArrayList in the JavaBean. The problem I'm having is I can't seem to loop through that ArrayList to output the info for each candidate. I've been Googling and looking on StackOverflow for hours but nothing I've tried works.

What I have so far is the following

Results.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <jsp:useBean id="CalculationBean" class="beans.CalculationBean" scope="session"/>
    <% CalculationBean.calculateResults(request); %>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Ishka Election - Results so Far</title>
    </head>
    <body>
        <h1>Ishka Election - Results so Far</h1>
        <table>
            <th>Candidate</th>
            <th>Score</th>
            <th>Preference</th>
        </table>
        <c:forEach items="${CalculationBean.getCandidateList()}" var="current">

            <tr>
                <td><c:out value="${current.name}"/></td>
                <td><c:out value="${current.score}"/></td>

            </tr>
        </c:forEach>

    </body>
</html>

This just outputs a page with the table headings and then "${current.getName()}" and the getScore, it doesnt print out the values from the object.

The arraylist in the CalculationBean is populated on the CalculationBean.calculateResults(request) call. I've used the debugger in eclipse to make sure of that.

I've also tried in a scriptlet, creating a new ArrayList and using the getCandidateList() method to get the list and then request.setAttribute("candidateList",arrayList); and trying for the items in the forEach using "candidateList" but this doesn't seem to work either.

If anybody could help or explain to me what I'm doing wrong it would be great, thanks.

Edit #1

I also forgot to mention that what prints out is

Candidate Score Preference ${current.name} ${current.score}

But there are 5 Candidate items in the array list so if the loop code was working should ${current.name} ${current.score} be printed out 5 times rather than 1?

Edit #2

I've also just confirmed that I can do it in normal java using a scriptlet, the following code in place of the for forEach tag code works, but I know scriptlets are frowned upon to use now.

<%
    ArrayList<Candidate> list = CalculationBean.getCandidateList();
    for(Candidate c : list){
        out.print("<tr><td>"+c.getName()+"</td>");
        out.print("<td>"+c.getScore()+"</td></tr>");
    }       
%>
Alan
  • 243
  • 1
  • 11
  • In JSTL, you cannot print methods like that. Meaning `getName()` is not a valid thing so it prints nothing. try `${current.name}` – bmarkham Feb 25 '16 at 01:21
  • That also does not work, I've also added a bit more info to the end of my original post. – Alan Feb 25 '16 at 02:19
  • Can you post your java file? I think it would help me and others as well – bmarkham Feb 25 '16 at 02:33
  • Stop jsp:useBean. Stop scriptlets. Stop ${bean.method()}. Use servlet. Use EL. Use ${bean.property}. See duplciate for kickoff example of right approach (don't forget to checkout links in the answer over there, we have very nice wiki pages on basic subjects) – BalusC Feb 25 '16 at 10:35

2 Answers2

0
<c:forEach items="${CalculationBean.CandidateList}" var="current"> 

Try this and have getter setter for the list.

0

because of this line:

 <c:forEach items="${CalculationBean.getCandidateList()}" var="current">

I guess:

<c:forEach items="${CalculationBean.candidateList}" var="current">
        <tr>
            <td><c:out value="${current.name}"/></td>
            <td><c:out value="${current.score}"/></td>

        </tr>