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

checkbox value to servlet

 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a form with a checkbox. The form is posted to my servlet. Taking a stepwise approach, I'm just trying to get all the data from the form over to my servlet and have the correct data.

When I submit the form, my servlet prints out the results, and for some reason the checkbox always comes back as false. What am I doing wrong here?

in my form (where 'cat' is a bean):
<INPUT TYPE="checkbox" NAME="active" VALUE="ON">

in my servlet receiving the form data:
Boolean active = Boolean.valueOf(request.getParameter("active"));
boolean activated = active.booleanValue();
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you just print out the value of the check box parameter?

if it is passed as 'ON' instead of true, then any value other than 'true' is defaulted to false.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I remember. You will only get 'on', otherwise you just get that parameter as null.

Correct me if im mistaken. Thanks.

[edited after Ben's post]
I am talking about the default, when no value is specified.
[ May 03, 2005: Message edited by: Adeel Ansari ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a checkbox, the value attribute holds the string that will be sent if the box is checked. By default it sends the string "on".

What determines whether it is checked or not is the checked attribute.

Example:


Hope that helps
 
Teck Chun Pang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I usually use checking for null to determine if a checkbox is checked after form submission.
(A checkbox which is not checked always return a null value). This is useful for checking a series of checkboxes and determining which one is checked and perform some other processing.

Hope this helps.

Teck Chun
 
Mathew Sam
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the checkbox is checked, value of the parameter 'active' will be 'ON'.

In the servlet check the following.
String active = request.getParameter("active");
if("ON".equals(active)){
// check box is selected
} else{
// check box is not selected

}
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much guys, I really appreciate the help!
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
In a checkbox, the value attribute holds the string that will be sent if the box is checked. By default it sends the string "on".

What determines whether it is checked or not is the checked attribute.

Example:


Hope that helps


Oh wait guys, I was researching the checkbox input tag and from http://www.w3schools.com/tags/tag_input.asp it says the 'checked' attribute indicates that the input element should be checked when it first loads on the page.

So I had this:
<INPUT TYPE="checkbox" NAME="active" VALUE="ON">

but in my JSP page I actualyl had this before:
<INPUT TYPE="checkbox" NAME="active" VALUE="ON" <% if(cat.getActive()) { out.print("checked") ;}%> />

where cat is a bean that holds all the info on a specific music category. This checkbox is used to allow administrators to activate and deactivate entire categories.

<% if(cat.getActive()) { out.print("checked") ;}>
I have this in my code right now to make any category that's active have the box already checked on when the admin views the edit screen and this works accordingly.

So bottomline, if the category is currently active I want the checkbox checked on if the category is currently active, but I also want to be able to return the value properly to my servlet. The final line of code should be??
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I had this:
<INPUT TYPE="checkbox" NAME="active" VALUE="ON">

but in my JSP page I actualyl had this before:
<INPUT TYPE="checkbox" NAME="active" VALUE="ON" <% if(cat.getActive()) { out.print("checked") ;}%> />

where cat is a bean that holds all the info on a specific music category. This checkbox is used to allow administrators to activate and deactivate entire categories.

<% if(cat.getActive()) { out.print("checked") ;}>
I have this in my code right now to make any category that's active have the box already checked on when the admin views the edit screen and this works accordingly.

So bottomline, if the category is currently active I want the checkbox checked on if the category is currently active, but I also want to be able to return the value properly to my servlet. The final line of code should be??


First:
You don't need to explicitly set the value property if you don't want to.
If you don't set it and the box is checked, the browser will send "on".


To answer your question:
Use the code that you have to build the checkbox tag.

When reading the form values in the servlet, just test for null.
If it's null then the box was unchecked when the form was submitted.
If there is any value then it was checked.




or the more terse but a little less clear...
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:



or the more terse but a little less clear...


Alright Ben I think I'm following here, I have no trouble with on the servlet side, I just want to make sure I have the correct input tag...

<input type="checkbox" name="active" checked /> ??

this will send the result, I will just test for the string "on" that would be sent as default like you said earlier. But then how will I be able to precheck the box if the current status of the category is active?

I was using this to achieve that result:
<% if(cat.getActive()) { out.print("checked") ;}>

which also prints 'checked' into the tag for a second time if the category is active... ??
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<input type="checkbox" name="active" checked /> ??

this will send the result, I will just test for the string "on" that would be sent as default like you said earlier. But then how will I be able to precheck the box if the current status of the category is active?

I was using this to achieve that result:
<% if(cat.getActive()) { out.print("checked") ;}>

which also prints 'checked' into the tag for a second time if the category is active... ??


If the word "checked" is inside the checkbox tag when it gets to the browser it will initially be drawn as with a checkmark in it.
The code that you have to draw the checkbox tag with the if clause will dynamically add the "checked" attribute if cat.getActive() == true.

Don't check for the word "on".
Just check to see if the "active" parameter passed in from the form submision is null. If not, it was checked.
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sweet, I understand now. Thanks so much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic