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 JSP values (checkbox) - True or On 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 "JSP values (checkbox) - True or On" Watch "JSP values (checkbox) - True or On" New topic
Author

JSP values (checkbox) - True or On

Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
I am trying to grab the value of a checkbox from a previous jsp with:


If it's true, do this. If it's false, do that. However, when I print the value, I get "on". Since I am new to this, what I understand is that is should be a boolean(true/false), so why am I getting "on". If that's the way it is, fine. However, when I test for it, do I do this:

[code]
<% if (request.getParameter("checkBoxName") == "on") {%>
[code/]

Or is there a better way?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Not really much to do with JSP. Moved to HTML.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Originally posted by Gabriel Allen:
what I understand is that is should be a boolean(true/false)


Where did you come by this understanding? That's not the way checkboxes work.

First of all, you will never receive anything but a string for any request parameter ever submitted; that's the way HTTP works.

This is reflected in that getParameter() always returns a String.

Checkboxes, in particular, will not return anything at all if they are unchecked. This will give you a null when you try to retrieve its value. If you don't specify a value attribute on the checkbox, it will return "on" when checked.

If you want it to return the string "true" when checked, specify "true" as the value attribute. You can easily convert that string to a boolean using the Boolean class.

See the HTML 4 Specification with regards to this.
[ January 22, 2007: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Here are some code snippets of the idioms that I usually use.

In the HTML:



In the servlet (never do processing in a JSP):



This will set isWhatever to true if the parameter is the string "true", Any other value, including null, will set it to false.
[ January 22, 2007: Message edited by: Bear Bibeault ]
Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
Thanks! As I am learning this, I tend to make assumptions and that's why I thought checkboxes were true/false values. That's why I was asking about the "on" I recieved when I printed the value.


Thanks again for the info.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Originally posted by Gabriel Allen:
I tend to make assumptions and that's why I thought checkboxes were true/false values.


Not a good way to proceed. The specifications for all the tehcnologies you are using are freely available on the web. Get them. Consult them often.
Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
I will. Thanks again.
Gabriel Allen
Greenhorn

Joined: Jan 09, 2007
Posts: 12
Hi! I am running into another problem with this. After I grab the value of the checkbox:


I then try to create some logic by doing this:


Doesn't seem to work. Is this wrong? What I am trying to do is turn off some tables in the calling page based on a checkbox selected in the previous page.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

This has strayed far from the topic of HTML. I've moved it to the JSP forum.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Originally posted by Gabriel Allen:




How familiar are you with Java? The == operator is not the way to check strings for equality.

By the way, what version of JSP are you using? If you are using a modern version, you shouldn't be putting any Java on your pages at all.
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
In addition to what Bear said,

Always do "on".equals(checkBox) and not checkBox.equals("on").

checkBox.equals("on") will throw a NullPointerException if the checkbox variable is null.


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP values (checkbox) - True or On
 
Similar Threads
Set default checkbox value
Begginer question, assigning a java string the value of a passed parameter
Checking Status of a Checkbox
problem in reading from checkbox
send checkbox values from a jsp to other jsp