Skip to content
Advertisement

Display the array on the page as an HTML table. Use output scriptlet

java Create an array with the following content: 1 2 3 4 5 6 7 8 9 Display the array on the page as an HTML table.

I did this like this:

<%
  int [][] Array = {{1,2,3}, {4,5,6}, {7,8,9}};
  for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++)
      Array <%=Array[i][j]%>
  }
%>

but it doesn’t work

Advertisement

Answer

For a JSP scriptlet (using <% %>) you must print your output. With Java this is through out.println() since out is provided in the scriptlet context.

Try the following JSP:

<%
int [][] arr = {{1,2,3}, {4,5,6}, {7,8,9}};
out.println("<table>");
for (int[] row : arr) {
    out.println("<tr>");
    for (int val : row) {
        out.println("<td>" + val + "</td>");
    }
    out.println("</tr>");
}
out.println("</table>");
%>

If this works for you then please accept this as the answer.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement