2

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?

Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
bryce
  • 842
  • 11
  • 35

2 Answers2

2

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

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • Thanks a lot for this answer. I now scriplet are very bad, but it is also true that I don't know how to avoid them. I think your link will be very helpfull for me ;-) – bryce Sep 30 '14 at 05:54
1

Your JSP is translated to servlet by server container. Every time you refresh your page _jspService is called.

Translation of JSP to servlet code :

public class HelloWorld2$jsp extends HttpJspBase {
        //code declare inside <%! %> method goes here
        public void _jspService(HttpServletRequest request,
                HttpServletResponse response)

        {
            // code declare inside <% %> method goes here

        }
    }

Instead of

<%! int i = 0; %> 

Use Below Code:

 <% int i = 0; %> 
Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31