• Post Reply Bookmark Topic Watch Topic
  • New Topic

Taking in boolean parameters

 
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 that indicates whether a category should be active or not. The form posts to my servlet, and I'm having trouble getting the boolean result from the form.

in my servlet, I have included java.lang.*; which includes the Boolean methods.

This is the LOC I have to read in the value from the checkbox:

boolean activated = Boolean.parseBoolean(request.getParameter("active"));

I get this error:
cannot resolve symbol
symbol : method parseBoolean (java.lang.String)
location: class java.lang.Boolean
Boolean active = Boolean.parseBoolean(request.getParameter("active"));

What have I done wrong here?? Thanks to all who reply!
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static method parseBoolean(String value) of java.lang.Boolean is available only since 1.5. Also it returns a primitive boolean type and not an Object

So if you arent using jdk1.5, you should be converting the String to boolean in the following fashion.



Cheers,
Ram.
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright I'll give that a shot, I really appreciate your help. As a little side question/comment... is that the only way to go about retrieving a boolean value from a form prior to JDK 1.5? That seems like a lot of overhead for what seems to me like a simple operation!

Thanks again!!
Jason
 
Adisesha Rao Raya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I can suggest the solution as

boolean bResult = new Boolean(request.getParameter("active")).booleanValue();

The constructor converts the string value as boolean object based on the String value true/false.

We can get the primitive data type from the boolean object by using booleanValue().

If this helps you i am very happy else i will learn better solution from you

Thanks and Regards
Adisesha Rao
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using simple HTML or JSPs. How about using some MVC type of model, you can declare your boolean in your form bean, have it display on a JSP and access it in any other servlet, action class.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Kwok:
Hi,
I have a form with a checkbox that indicates whether a category should be active or not. The form posts to my servlet, and I'm having trouble getting the boolean result from the form.

in my servlet, I have included java.lang.*; which includes the Boolean methods.

This is the LOC I have to read in the value from the checkbox:

boolean activated = Boolean.parseBoolean(request.getParameter("active"));

I get this error:
cannot resolve symbol
symbol : method parseBoolean (java.lang.String)
location: class java.lang.Boolean
Boolean active = Boolean.parseBoolean(request.getParameter("active"));

What have I done wrong here?? Thanks to all who reply!


I have a couple of comments. First you don't need to import java.lang.*. The classes in this package are automagically imported for you, so you can use Boolean at will without importing it explicitly.

Second, I think you are making this more difficult than you need to. Perhaps this is partly because I don't have enough information here. You say that you are using a checkbox. Do you mean the Checkbox class from AWT or the JCheckbox class from Swing or some other class? If you are using Checkbox, then you can use the getState() method or if you are using JCheckbox, you can use the isSelected() method. Both of these return a primitive boolean directly without using the Boolean wrapper class.

Of course, I may be way off here. It will help clarify things if you show us the declaration for the request variable.

Layne
 
Junilu Lacar
Sheriff
Posts: 9999
103
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,

1. This question is more appropriate in the JSP forum

2. Be aware that for HTML checkboxes, no parameter will be submitted with the request if the checkbox is not checked. That means that you really only need to check for the presence of the parameter to see if the checkbox was checked; it really does not even matter what the value was at this point. If you do need to check the value, using equals() will do the trick.

 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JSP...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!