How to add checkboxes dynamically using a JSP

14,770

Solution 1

<%
for(int i = 0; i < array.length; i++)
{
%>
<input type="checkbox" name="<%= array[i]%>">
<br/>
<%
}
%>

Solution 2

You can try c:foreach

<c:forEach var="res" items="${<your array/List>/resList}"> 
<tr>
    <td><html:checkbox property="select" value="<some value>"/></td>
        <td>${<your list.element>}</td>
        <td>${res.empName}</td>
        <td>${res.mailID}</td>
   </tr>
 </c:forEach>
Share:
14,770
Surya Chandra
Author by

Surya Chandra

Updated on June 05, 2022

Comments

  • Surya Chandra
    Surya Chandra almost 2 years

    I have a string variable which is assigned with certain no. of labels. For eg, String var ="ChkBox1,ChkBox2,ChkBox3" in a JSP.

    Now, my requirement is, I have to extract the checks out of the string and store them in an array and dynamically add checkboxes(3 checkboxes as per above example) to the page by running a for loop (based on no. of elements in the array) and display them.

    If the variable is later changed to String var = "ChkBox1,ChkBox2,ChkBox3,ChkBox4", the webpage should now contain 4 checkboxes. I am relatively new to JSP. Please help me out on how to design this logic.

    PS:I already implemented the extraction of checkBoxes from string and formed an array. I need assistance on how to use a for loop to add checkboxes dynamically