aspose file tools*
The moose likes JSP and the fly likes How to loop through data on a jsp form? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to loop through data on a jsp form?" Watch "How to loop through data on a jsp form?" New topic
Author

How to loop through data on a jsp form?

Katy Spencer
Greenhorn

Joined: Apr 18, 2010
Posts: 4
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:


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

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".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Katy Spencer
Greenhorn

Joined: Apr 18, 2010
Posts: 4

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

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

Joined: Apr 18, 2010
Posts: 4
here's the source from the browser:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

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

Joined: Apr 18, 2010
Posts: 4
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61002
    
  65

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to loop through data on a jsp form?