0

Sir i have an issue of updating my array list connected wd jsp n action servlet along wd form bean..

here is my frst jsp page where i m displaying the list:i want to update this list wd servlet where roll no will be the condition do help??

<%Iterator itr;%>
<% ArrayList data= (ArrayList)request.getAttribute("data");
    for (itr=data.iterator(); itr.hasNext(); )
    {


%>
    <tr>

        <td width="40"> <input type="text" value="<%=itr.next()%>"/></td>
        <td width="40"><input type="text"  name="th2" value="<%=itr.next()%>"/></td>
            <td width="40"><input type="text"  value="<%=itr.next()%>"/></td>
    <td width="40"><input type="text"  value="<%=itr.next()%>"/></td>
            <td width="40"><input type="text"  value="<%=itr.next()%>"/></td>
            <td width="40"><input type="text"  value="<%=itr.next()%>"/></td>

</tr>
<%}%>
AllTooSir
  • 48,828
  • 16
  • 130
  • 164

1 Answers1

0

That is not how an iterator works. iter.hasNext specifies whether there are more elements. iter.next advances to the next. You are calling hasNext once, but next 6 times.

You are essentially asking: "Do you have a first element? If so, get me the first 6." But you may only have 2 elements and that will cause an IndexOutOfBoundsException.

The foreach loop syntax automatically fetches an iterator for you so it reduces your code.

Try this:

<% for (Object o : (ArrayList)request.getAttribute("data"))
   {
%>
    <tr>

        <td width="40"> <input type="text" value="<%=o %>"/></td>
        <td width="40"><input type="text"  name="th2" value="<%=o %>"/></td>
        <td width="40"><input type="text"  value="<%=o %>"/></td>
        <td width="40"><input type="text"  value="<%=o %>"/></td>
        <td width="40"><input type="text"  value="<%=o %>"/></td>
        <td width="40"><input type="text"  value="<%=o %>"/></td>

</tr>
<% } %>
Brandon
  • 9,822
  • 3
  • 27
  • 37
  • Thanks for dis its showing data perfectly bt the main issue is hw can i update the values specified in the table i m not able to understand that – Kunal Mahajan Jul 25 '13 at 11:00
  • I don't understand your question. – Brandon Jul 25 '13 at 12:23
  • ok forget dis code just help me wd dis how can i update table which has name and marks along wd roll no? My aim is to update the table keeping rollno as primary key – Kunal Mahajan Jul 25 '13 at 12:25