This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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
Consultant @ Xebia. Sonny Gill Tweets
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...

subject: Saving and Restoring Form Data
Similar Threads
how to call action on click of checkbox?
How to find Checkboxes?
jsp calls 1 of 2 servlets
How do i come back to the same page in the display tag after submitting the form.
Using Struts : Populate checkboxes from Database in JSP