• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Does this work for checkbox ?

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I want to see if a checkbox is checked, shall I check

1. in html or JSp, I define a checkbox and give it "MyCheckBox" as "Name".
2. if it is checked, request.getParameter("MyCheckBoxName").equalsIgnoreCase("on") will be true. Right ?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ben oliver:
If I want to see if a checkbox is checked, shall I check
2. if it is checked, request.getParameter("MyCheckBoxName").equalsIgnoreCase("on") will be true. Right ?



Not necessarily.
If your checkbox is created without a "value" attribute, the default value "on" will be sent up in the form request when the box is checked.

If you set a value, then the value will be sent up, if it is checked.

In either case, if the box is unchecked, the browser will send up nothing.
It will be as if the checkbox didn't exist in the form.
In this case, getParameter will return null.

Note: It will also return null if the name is missspelled.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you won't get true or false, but only the names of checkboxes that were checked when submitted. If the checkboxex have the same name, you'll have to call getParameterValues
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
I think that you won't get true or false, but only the names of checkboxes that were checked when submitted. If the checkboxex have the same name, you'll have to call getParameterValues



In his first post, the original poster was checking for "on" (the default value for a checked box) with the String.equalsIgnoreCase method; which returns true or false.

This brings up another issue that I should have mentioned in the first post.
The code:

will throw an NullPointerException if the checkbox is not checked.
In this case you will be trying to call a method of the String object when the string is null.

A more solid way of testing this would be to turn that line around.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't it safe to assume that you can just check for null?



Seems simpler to me.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gregg Bolinger:
Isn't it safe to assume that you can just check for null?


Yep.
I was pointing out a more general issue that people run into when testing form parameters.
Checkboxes are somewhat unique in that null can be an expected value.
[ August 17, 2006: Message edited by: Ben Souther ]
 
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
[QB[/QB]




wouldn't that have thrown a null pointer if the MyCheckBoxName is null ie when the check box was not checked?
 
Sheriff
Posts: 67695
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anselm Paulinus:

wouldn't that have thrown a null pointer if the MyCheckBoxName is null ie when the check box was not checked?



No, it will not. You can pass a null into an equals method without fear of an NPE.

This idiom of comparing a value that may or not be null to a string literal is quite commonly used.
[ August 17, 2006: Message edited by: Bear Bibeault ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String _enabled=request.getParameter("enabled");
int ena=-1;
if("on".equals(_enabled))
{
ena=1;
}
else
ena=0;
<input type=checkbox" name="enabled">
<!-- Definatly it will Work ----->
 
reply
    Bookmark Topic Watch Topic
  • New Topic