aspose file tools*
The moose likes JSP and the fly likes checkbox in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "checkbox in jsp" Watch "checkbox in jsp" New topic
Author

checkbox in jsp

Manish Sahni
Ranch Hand

Joined: Sep 09, 2009
Posts: 41

My question is that once i check the value in a check box , then i want to retain the state of that check box as "checked" whenever the user logs in again .
How can this be accomplished?

Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Store the value of that checkbox in a permanent storage. A database could be an idea


Bye,
Nicola
Manish Sahni
Ranch Hand

Joined: Sep 09, 2009
Posts: 41

hi,

i am storing all the values of check boxes in the database.
However the point is how should i retain the state of the selected check box whenever i am logging in by the same id.

do i have to use any client side scripting or what , i am bit confused??


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Just put the checked attribute on the checkboxes that should be checked.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
You could do it in the following way:

suppose you have the checkbox



if the checkbox is checked, when you submit your form you get a request parameter c1=1 and, you tell me, you store that value in a table of a database

When you retrive the value of c1 from the database where do you put it before you invoke your jsp again? In a bean perhaps?
if it is a bean let's call it yourbean.

Set for example a request attribute with that bean in your servlet:



and look at the value of c1 in you bean when you are building your checkbox again



Manish Sahni
Ranch Hand

Joined: Sep 09, 2009
Posts: 41

thanks for the solution

however i also want that if the check box is unselected again then its value should not be stored in the database i.e. database should be updated again while unchecking the values..
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
It's quite the same.
If c1, as in the example above, is not checked, then the request parameter will be null instead of 1.
Starting from this distinction you will be able to update your database accordingly.
Manish Sahni
Ranch Hand

Joined: Sep 09, 2009
Posts: 41

well, thanks for your help

its working fine now!!
 
 
subject: checkbox in jsp