• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to loop through data on a jsp form?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm trying to create an edit form that retrieves data from a database using a result set and displays it on a table on a JSP page. The retrieving part isn't the problem, because the data is being displayed on the table in the input text boxes like I wanted them to. The rows are also being created dynamically, so if, in the database there are 2 rows, then 2 rows will be displayed on the table.
The problem I am getting is as follows:
I want to be able to edit the data in the table, but only *one* row at a time. I.e., I have an update button at the end of each row of the table so that the user can update that row only. However,if there's more than one row in the table, and I attempt to update the 2nd, 3rd, 4th, etc row on the table by click the update button at the end of that row, the *first* row is being updated instead.
I think my problem is that even though the data on the table is being displayed as a loop, the data being sent back the update servlet is not in a loop, so the servlet is only pulling the first row. What do I need to do to loop the data that the user has entered?

This is how the JSP looks:





And on the servlet, this is how the data is being retrieved:


 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, please use better variable names than aList and a. Poor variable naming makes code surprisingly hard to read.

Secondly, embedding each <tr> element in a form is not valid HTML. It might work, but its not valid.

Ignoring that for the moment, are you saying that when you click on one row, the form for a different row gets submitted? I'm finding that hard to believe. Have you used an HTTP sniffing tool to see what's actually getting submitted?

P.S. It has nothing to do with "looping".
 
Katy Spencer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Secondly, embedding each <tr> element in a form is not valid HTML. It might work, but its not valid.


Thank you, I didn't know that, I'm very new to coding.


Ignoring that for the moment, are you saying that when you click on one row, the form for a different row gets submitted?



Yes, it doesn't matter which update button I click, the first row is always the one that gets submitted.

Have you used an HTTP sniffing tool to see what's actually getting submitted?


No,but I've used System.out.println in the servlet to see which values are being retrieved, and they're the values from the first row. That's why I thought it was a problem that could be solved with looping, because the values on the form seem to be static.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. Do a view source on the HTML being sent to the browser and post it here. Somethings sounds weird. Even with the invalid HTML, I'd not suspect that a submit button in one form would submit the values of another.
 
Katy Spencer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's the source from the browser:

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, now I'm confused. That HTML shows an empty table. How can you be getting values for the wrong rows when there aren't any rows at all?
 
Katy Spencer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems it's not showing the code of the stuff that's inside the <c:forEach> and </c:forEach> tags. I don't know why...

But the servlet's picking up the values from the first row anyways.

Weird.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In any case it looks, when the table actually has rows, that all rows are in the same form and so all rows will be submitted.

So using request.getParameter() will, of course, only give you the one set of values.

Take a look at the other methods of HttpServletRequest and see if you can identify one that might give you what you are after. (Hint: it will return an array of values).

That will be step 1 -- there will be other work to do, but one thing at a time.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic