• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP values (checkbox) - True or On

 
Gabriel Allen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really much to do with JSP. Moved to HTML.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will. Thanks again.
 
Gabriel Allen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has strayed far from the topic of HTML. I've moved it to the JSP forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic