1

I'm creating an HTML table dynamically in a JSP using Java for loop. But when I'm trying to assign a unique id to each TD by its position in table, I get a compilation error.

what I would like to do is some sort of:

<TD id="<% new String(row*ROWS+col) %>>

this is the code of the jsp:

        <TABLE border="1">
        <% for (int row = 1; row <= ROWS; row++) { %>
        <TR>
            <%      for (int col = 1; col <= COLS; col++) {%>
            <TD id="????"> (<%=col%>, <%=row%>)
            </TD>
            <% } %>
        </TR>
        <% } %>
    </TABLE>        
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
NatanC
  • 71
  • 1
  • 5

1 Answers1

0

You missed the =. Also, the String class doesn't have a constructor with a int parameter.

<TD id="<%= row * ROWS + col %>> ">  

By the way, the use of scriptlets are not recommended in JSP pages, you could get some info in these questions:

How to avoid using scriptlets in my JSP page?

How to avoid Java code in JSP files?

Community
  • 1
  • 1
David SN
  • 3,389
  • 1
  • 17
  • 21
  • tried it, but it doesnt work. I also tried with " in the end of the expression, or ; after the new command. actually what I get as an error is: "Bad value " " for attribute "id" on element "td": An ID must not contain whitespace. Syntax of id: An ID consists of at least one character but must not contain any whitespace." – NatanC May 31 '16 at 09:54