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 how to maintain the state of checkboxes across page 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 "how to maintain the state of checkboxes across page" Watch "how to maintain the state of checkboxes across page" New topic
Author

how to maintain the state of checkboxes across page

Rema Remulta
Ranch Hand

Joined: Apr 03, 2002
Posts: 51
Hi all,

I've read in this thread the same problem i've got but didn't post a solution so i'm going to open this problem again and hoping that i able to get some hints to the solution from you all. The problem goes similarly like this:

I am currently using the JspTag Library to do the paging of a large set of data. Each row of data comes with a checkbox. I want to enable the user to check the boxes they want, and go on to other pages to check other boxes. As they go through all pages, all the boxes should remained checked. How do I do this?
I know how to use session to store information to pass to other pages, but I can't seem to do the same for this.
Basically, have a jsp file that does the displaying and paging of all the data. There's a request handler that will be responsible for the request from the jsp.Each data row has a checkbox with unique name, so that I can identify which is checked or not.
Let's say I have 40 rows of data. I can display 20 rows of data on each page. Which means there will be 2 pages. The user goes from page to page by clicking on a link.

I like to know:
How do I save the values of all the checkboxes on a page?
How do I retrieve the values of the checkboxes when this page is requested again?
Btw, the bean that i'm using doesn't have an attribute to store the state of the checkboxes so i don't think it's possible to access them via the session object?


Cheers,
Rema
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Rema,

As you know, if you want to maintain state across different requests and different pages in a JSP you need to use the session context.

You need to store an object in the session context that holds the state of all your checkboxes, e.g. a Map, a List or an array. You could (and probably should) do this via your Bean if it doesn't provide that facility already.

IMO there's no other way around this. Let me know if you require further guidance.

Jules
Rema Remulta
Ranch Hand

Joined: Apr 03, 2002
Posts: 51
Thanks Jules for the reply.

So to be able to maintain the state of checkboxes from one page to another, i should really have a bean attribute for the checkbox identifying if it's checked or not?
What is working now is that I can get the number of checked boxes of a page and their corresponding values thru getParameterValues and have them stored in an arraylist.It's actually a dynamic checkbox.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
What you need then, IMO, is an ArrayList in the session context rather than the request context. The set of checkboxes that you then display on a given page is a subset of the contents of that ArrayList, e.g. ArrayList has 30 elements; page 1 shows 1 to 10; page 2 shows 11 to 20, etc.

At the client side you display the subset of checkboxes and their selected values according to the page you're on. When the page is posted to the server-side you use getParameterValues() as you do at present to update your session ArrayList with the newly submitted values.

Does that make sense?

Jules
michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
Sorry to interrupt but once session is used is there a need to remove the session object. This is because I don't want to store the object in session forever. But the problem is I don't know where to actually put the remove session. My application is a web application with a navigation bar. I put in the session especially for a paging function. So that I can maintain the object value when user press Next and so on. What if the user click on any other links. The object will still be maintained in the session without getting removed. Don't tell me to put the remove session function at all the other pages cause there is too much of them.

Thanks
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Sounds like a job for a Filter then. If you need to minimise the memory footprint of your sessions you could always persist the checkbox state between requests.

Jules
michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
Soory but I don't get your answer. How is persist the checkbox state between requests done? And how filter can achieve this.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Those are two separate options.

If you want to remove the checkbox state object from the session for pages where it is not needed you could use a Filter.

If you're worried about using a checkbox state object in the session scope you can avoid doing this by persisting the checkbox states between requests, e.g. by writing them to a database. This option reduces the memory footprint of the session but increases the turnaround time of a page submission.

Jules
Rema Remulta
Ranch Hand

Joined: Apr 03, 2002
Posts: 51
Hi,

Have thought of using arraylist in the session without a bean attribute, but we're thinking that there will be a problem in the system's performance. The request handler where the session is managed have to maintain an arraylist containing the same number of records display in the jsp such that it can trace which index in the list is being selected.For instance if there is 1000 records to display then the arraylist should also have 1000 records just to keep track which row index has a selected checkbox.. Thus, everytime the page is reloaded when the user gets through the pages from page 1 to page 2 and so on and back, the system will always perform looping and this will certainly slown down the system, isn't it?
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Are you suggesting that you'd have to loop through all 1000 records for each page? You'd only need to loop through those records that were updated or that you want to display on the given page. Use the expression (page_num - 1) * page_size + 1 to determine the start index and page_num * page_size to determine the end index.

Regarding the performance, why don't you try it, get it to work and see if it's acceptable. If you have performance issues, identify where they are and tune them. Persisting the state of a few checkboxes between pages may be the least of your performance worries. The question is, how are you going to do it otherwise? I would expect the difference to be imperceptible unless your system is extremely heavily loaded.

Jules
Sagar J Jambhulkar
Greenhorn

Joined: Sep 10, 2004
Posts: 8
I would have used a bean with HashMap as datastructure.Each row must be having a primary key which is used as index.When you loop through to display the rows in a jsp page see if the hashmap has entry for that row if yes then that check box is selected.Offcourse the bean is in session scope.
[ September 11, 2004: Message edited by: Sagar J Jambhulkar ]

Love Work and Bless All and you will be blessed by God<br />-----Sadguru Wamanrao Pai
 
Consider Paul's rocket mass heater.
 
subject: how to maintain the state of checkboxes across page
 
Similar Threads
Maintain checked and unchecked in checkbox in pagination
URGENT! Mulitbox problem!!
EJB-QL 2.0 & aggregate COUNT() functionality
Selecting records in a jsp and displaying selected in another
How do I maintain the state of checkboxes across page?