File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Saving and Restoring Form Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Saving and Restoring Form Data" Watch "Saving and Restoring Form Data" New topic

Saving and Restoring Form Data

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
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?


Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
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

Joined: Feb 02, 2002
Posts: 1211

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.

The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
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...

I agree. Here's the link:
subject: Saving and Restoring Form Data
It's not a secret anymore!