<p>How do I alternate HTML table row colors using JSP?</p>

<p>My CSS looks something like:</p>

<pre><code>tr.odd {background-color: #EEDDEE}
tr.even {background-color: #EEEEDD}
</code></pre>

<p>I want to use <code>&lt;c:forEach&gt;</code> to iterate over a collection. </p>

<pre><code>&lt;c:forEach items="${element}" var="myCollection"&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;c:out value="${element.field}"/&gt;&lt;/td&gt;
    ...
  &lt;/tr&gt;
&lt;/c:forEach&gt;
</code></pre>

<p>I need an int count variable or boolean odd/even variable to track the row. Then my <code>&lt;tr&gt;</code> tag would look something like:</p>

<pre><code>&lt;tr class="odd or even depending on the row"&gt;
</code></pre>

I don't use JSP, so I can't give you an answer in your language, but here's what I do (using pseudo code)
```
```
counter = 0
foreach (elements)
    counter = counter + 1
    output: <tr class="row{counter % 2}">...</tr>
```
```
Personally, I name the classes "row0" and "row1", which lets you alternate between them with a simple modulus calculation, also, if you decide to have rows alternating in triples or quads (instead of pairs), you can easily extend it to
```
row2
```
,
```
row3
```
and change your output code to be
```
counter % 4
```
, etc.