Title: create dynamic row in jsp using for loop
Tags: java;jsp
Question: i want to create a table with dynamic no of rows by this way

```<table width="89%" style="margin-left:30px;"&gt;                   <%
    for (int arrayCounter = 0; arrayCounter < documentList.size(); arrayCounter++) {

    %&gt;
    <% 
    int test = arrayCounter;
    if((arrayCounter%2)==0)){                       
    %&gt;
    <tr&gt;
    <%
    } %&gt;
     <td style="width:2%"&gt;

    </td&gt;
     <td style="width:20%;align:left;"&gt;
     </td&gt;

      <td style="width:30%;align:left;"&gt;

    </td&gt;
    <% 
     if((arrayCounter%2)==0){
      %&gt;
   </tr&gt;
    <%   } %&gt;

     <%
    }
     %&gt;
     </table&gt;
```

in my jsp  this way it will create 4 rows but according coding function it would create 2 row only if ```documentlist.size()=4```;
help me !



Here is another answer: remove if statements from loop and create rows normally.

change your loop with this
for (int arrayCounter = 0; arrayCounter < (documentList.size()/2); arrayCounter++)

and for last row you can have if statement which will compare
if (documentList.size()/2)-1 == arrayCounter).. then
you will get what you are looking for

else

for (int arrayCounter = 0; arrayCounter < documentList.size(); arrayCounter++) {

```if (documentList.size()/2)-1 == arrayCounter){  create 1 row}else{
create 1st row and then arraycounter ++
create 2nd row and then arraycounter ++
```

}
}

Comment for this answer: do you thing it will solve our problem bcoz your loop run by size/2 times but we need to iterate all data ?

Here is another answer: Don't use scriptlets in jsp, Jsp is is view layer, use as a view. there is servlet/java-beans to put your all java code.

There is jstl taglib, which has many inbuild functions, use it. you can get it from here

In your case to loop over a list do like this:


Add jstl library to your classpath
First import jstl taglib in top of your jsp.
Then you have jstl tags to use in your jsp.


To import jstl in jsp do like:

```<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
```

for looping a ```List``` in jstl, there is ```c:forEach``` tag, you can use it like:

```<c:forEach items="${documentList}" var="doc"&gt;
   //here you can access one element by doc like: ${doc}
</c:forEach&gt;
```

If you want to generate table rows, for each documentList element, then do like:

```<table width="89%" style="margin-left:30px;"&gt;
<c:forEach items="${documentList}" var="doc" varStatus="loop"&gt;
  <tr&gt;
    <td style="width:2%"&gt;
      //here if you want loop index you can get like: ${loop.index}
    </td&gt;
    <td style="width:20%;align:left;"&gt;
     //if you want to display some property of doc then do like: ${doc.someProperty}, 
      jstl will call getter method of someProperty to get the value.

    </td&gt;
    <td style="width:30%;align:left;"&gt;

    </td&gt;
  </tr&gt;
</c:forEach&gt;
</table&gt;
```

read more here for how to avoid java code in jsp.


Here is another answer: Obviously it will create only 2 tow when size is 4,
when size is 6 it will create 3 row. remove it statement from loop if you want to 
create rows equal to number if size


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:02/27/14', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<java><jsp>', 'Title': 'create dynamic row in jsp using for loop', 'ViewCount': 2220, 'id': 22061414, 'user_AboutMe': '<p>working as a software Engineer in java/J2ee </p>\n', 'user_AccountId': 1217487.0, 'user_DisplayName': 'Prashant Aggarwal', 'user_Reputation': 188.0, 'user_Views': 36.0, 'user_id': 1184787.0}}