(This is for a Cattle Drive assignment, so technically you're not helping me with homework!)
I am coding a servlet class that gets input from a form, creates an object using that input ( there are 6 parameters needed to create the object ) and puts it into an ArrayList. This servlet class also needs to provide a way to access the objects in the ArrayList and send the values stored for each of the six attributes to a jsp where they will be displayed in a table. I've got everything working <b>except</b> only the last object in the ArrayList is being displayed in the table. I've been going around in circles until I'm dizzy trying to figure out how the attributes in the request object are sent from the servlet class to the jsp, and exactly what is in that response. I can't figure out if the problem is in my servlet class, where the code for setting the attributes for the request object is part of a for block that iterates through my ArrayList, getting each object and setting the 6 attributes for each, and when the block completes, forwards the whole thing to the jsp, where only the last object is being printed in the table; <b>or</b> in the jsp itself, where the code for printing out the table data might be faulty (i.e. it only prints one row of the table, rather than iterating through the who;e list list). How can I check to see what is being forwarded with the response from the servlet to the jsp? [ May 01, 2006: Message edited by: Bear Bibeault ]
Joined: Jul 06, 2005
Does it print only one line in the table, or does it print multiple lines, but all the same values?
So many potential places for trouble. Here are a couple of common mistakes I have seen
1 - Putting/editing the same object into the arraylist
The problem with the above code, is that only one instance of the user object is created, and the loop overrides the values each time. So you end up with the same object in the list multiple times. To fix - the line "user = new User()" needs to be added in the loop.
2 - scope issues. You say it is in request scope. Request scope attributes only last for one request/response (ie one page) every click of a link/button is a new request Maybe the list needs to be kept in session scope, so that it retains all the items added in this session?
Well thats my shot in the dark as to your problem. To be of more help, we'll have to see some code.