Retrieving checkbox values using jsp


Below is the code which shows the way to retrieve check box values in jsp


File1: index.jsp


<form action="target.jsp" method="post">
<table>

<tr><td>

<input type="checkbox" name="chk" id="chk" value="1">
</td></tr>

<tr><td>

<input type="checkbox" name="chk" id="chk" value="2">
</td></tr>

<tr><td>

<input type="checkbox" name="chk" id="chk" value="3">
</td></tr>

<tr><td>

<input type="checkbox" name="chk" id="chk" value="4">
</td></tr>


<tr><td>
<input type="submit" name="submit" value="Submit">
</td></tr>


</table>

</form>


File2: target.jsp


<%@ page import="java.util.*"%>



ArrayList arr=new ArrayList();

HashMap submap=new HashMap();



String[] chkname=request.getParameterValues("chk");


if (chkname != null)
  {
     for (int i = 0; i < chkname.length; i++)
     {
       
        submap.put("chk"+i,chkname[i]);

        arr.add(submap);
     }//for
 
  }//if  




//Retreiving data from ArrayList



int index=0;
             
         while(index < arr.size())
     {

         HashMap data=(HashMap) arr.get(index);
                 
                                     
                                   
                 System.out.println("CHECKED VALUE: "+data.get("chk"+index));
                 
               
                   index++;

               }//while

No comments:

Post a Comment