Reputation: 556
I'm having a weird error when reloading my index.jsp
page in a java web project.
<%
// index.jsp
// imports
String teams = Init.getTeams();
%>
<!-- default html content -->
<select class="select">
<option value="0" selected="selected">Choose Home Team</option>
<%= teams %>
</select>
<!-- default html content -->
So I'm calling Init.getTeams()
from this java method:
public static String getTeams() {
String s = "";
ArrayList<Team> teams = new ArrayList<Team>();
teams = MySQLConnection.getTeams();
for (Team t : teams) {
s += "<option value='" + t.getId() + "'>" + t.getName() + "</option>";
}
return s;
}
(Connects to a MySQLConnection class that I've written to retrieve the values from the database). And it'll return something like this:
<option value='1'>Royals</option>
<option value='2'>Red Sox</option>
<option value='3'>Athletics</option>
<option value='4'>Tigers</option>
<option value='5'>Rays</option>
<option value='6'>Angels</option>
... and correctly populate the select box. The problem is that when I reload the page, it duplicates the string that is returned and all the <option>
s are duplicated. Why is this happening?
Edit: Probably worth noting I'm running apache tomcat server, and when restarted, it resets and is displayed 1 time the first time the page is brought up, 2 times on the second time, etc. I've also tried to use meta tags to not cache the page, as that's what I suspected was happening - with no luck.
Upvotes: 1
Views: 1724
Reputation: 3404
Small suggestion: do use JSTL core tag lib for each, if you can of course. It will do code more simple and obvious, less error-prone. You even not need to implement getTeams method, as accessors used strait forward.
/* Somewhere in declarations part */
<%@taglib prefix="core_rt" uri="http://java.sun.com/jstl/core_rt"%>
...
/* inside select block */
<option value="0" selected="selected">Choose Home Team</option>
<core_rt:forEach items="${teams}" var="team">
<option value="${team.getId()}">${team.getName}</option>
</core_rt:forEach>
Upvotes: 0
Reputation: 556
I fixed this by putting <%!
at the very beginning of the index.jsp
page, replacing the <%
. I still don't understand exactly why this worked, but it did.
Upvotes: 1