Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Saving and Restoring Form Data

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an HTML form with about 100 checkboxes on it.

I need to save the state of these checkboxes so that when the user logs back in, they will be set as they left them.

I will either save the data to a file or database (haven't decided yet).

The only way I can think to restore the state of the checkboxes is to have 100 "if" statements in a JSP or servlet. This seems very messy to me.

Is there a better way to do this?

Thanks,

Drew
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all you're doing is preserving checkbox states, you can model the checkboxes with a boolean array. When the time comes to initialize checkboxes, you simply iterate through the array and set the checkbox CHECKED attribute appropriately.

Whatever you do, don't go coding 100 if-else pairs.
Whatever you do, don't go coding 100 if-else pairs.
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I havent given it a lot of thought, but you can also have a String[] with 100 entries, each initialized to "CHECKED" or "", as per the saved state of the checkboxes. Then you can refer to it directly on the JSP without needing any conditinal check.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions.

I like the idea of storing the checkbox state in an array, but I think there will be a problem with this. When the form is submitted, only those checkboxes that are actually checked get sent to the server. So, the order will not be maintained. I suppose you could give each checkbox a number, and then iterate through the values to set the state of the array but it's still not an ideal solution to me.

I will have to think some more about this...

Drew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic