Search code examples
javajspscriptlet

Reset a scriplet variable on refresh


I have a table and I want the lines to be numbered.

In my jsp, I have something like that:

<%! int i = 0; %> 
 <c:forEach items="${clients}" var="client">  
    <tr>  
       <td align="center"><%= ++i %></td>  
       <td><c:out value="${client.nomPrenom}"/></td>  
....

My problem is when I refresh the page, variable i is not reset to 0. It continues to ++

What do I do wrong?


Solution

  • You could do that using jstl as follows , as scriplets are not advised over decades

    <c:forEach items="${clients}" var="client" varStatus="loop">  
        <tr>  
           <td align="center"><c:out value="${loop.index}" /></td>  
           <td><c:out value="${client.nomPrenom}"/></td>  
        </tr>
    </ c:forEach>  
    

    see How to avoid Java code in JSP files? to learn more on using the jstl and EL