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>");
}
%>