Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

From HTML Form element String to Integer to Boolean: PHP vs JSP

Thread view: 
phillip.s.powell@gmail.com - 15 Jan 2006 19:25 GMT
I can't figure this out, and sorry, I'm a PHP guy.

[CODE]
 try {
  hasSubmittedMessage =
(boolean)Integer.parseInt(request.getParameter("hasSubmittedMessage"));
 } catch (NumberFormatException nfe) {                                // NOTHING SUBMITTED =
FALSE
  hasSubmittedMessage = false;
 }
[/CODE]

hasSubmittedMessage is a "boolean" HTML form element type with the
value of "1".  In PHP it's extremely easy to work with because in PHP
"1" = 1 = true (though "1" !== 1 !== true), so for me to use
"hasSubmittedMessage" in PHP it's this extremely easy:

[CODE]
foreach ($_POST as $key => $val) if (!isset(${$key})) ${$key} = $val;
if ($hasSubmittedMessage) { // DO STUFF
}
[/CODE]

But in JSP, no clue whatsoever to do this, as the above-mentioned code
failed in JSP due to "inconvertible types" from String to Integer to
Boolean.

What do I do?

Thanx
Phil
James Westby - 15 Jan 2006 19:57 GMT
> I can't figure this out, and sorry, I'm a PHP guy.
>
[quoted text clipped - 27 lines]
> Thanx
> Phil

Hi,

I think you're code will be failing on the cast to boolean.
Integer.parseInt() returns an int, and you cannot cast between the two.

To find out whether thwy parameter is a one you could try,

hasSubmittedMessage =
Integer.parseInt(request.getParameter("hasSubmittedMessage")) == 1;

The try {} block is probably still a good idea, in case someone sends
"asdf" or something.

Or, without going through integers,

request.getParameter("hasSubmittedMessage").equals("1");

or perhaps slightly better, but a less intuitive to read

"1".equals(request.getParameter("hasSubmittedMessage"))

Though the String methods may have problems if you start getting wierd
things in the parameter.

James
Noodles Jefferson - 15 Jan 2006 20:45 GMT
> > I can't figure this out, and sorry, I'm a PHP guy.
> >
[quoted text clipped - 40 lines]
> The try {} block is probably still a good idea, in case someone sends
> "asdf" or something.

what's asdf?

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Informer" -- Snow

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

opalpa@gmail.com opalinski from opalpaweb - 15 Jan 2006 20:57 GMT
I believe in this example "asdf" is no more than four consecutive
keyboard characters.  The purpose  is to exemplify something that's an
invalid input.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
James Westby - 15 Jan 2006 21:11 GMT
> I believe in this example "asdf" is no more than four consecutive
> keyboard characters.  The purpose  is to exemplify something that's an
[quoted text clipped - 3 lines]
> opalpa@gmail.com
> http://www.geocities.com/opalpaweb/

Yes, the meaning of "asdf" here is that it has no meaning.
Noodles Jefferson - 15 Jan 2006 21:59 GMT
> > I believe in this example "asdf" is no more than four consecutive
> > keyboard characters.  The purpose  is to exemplify something that's an
[quoted text clipped - 5 lines]
>
> Yes, the meaning of "asdf" here is that it has no meaning.

I see. Thanks to both of you.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Informer" -- Snow

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

Noodles Jefferson - 15 Jan 2006 20:02 GMT
In article <1137353104.556939.208740@g49g2000cwa.googlegroups.com>,
phillip.s.powell@gmail.com took the hamburger, threw it on the grill,
and I said "Oh wow"...

> I can't figure this out, and sorry, I'm a PHP guy.
>
[quoted text clipped - 7 lines]
>   }
> [/CODE]

Not tested:

try {

 if (Integer.parseInt(request.getParameter("hasSubmittedMessage")) ==
1) {

    hasMessage = true;

   } else {

     hasMessage = false;

   }

} catch(NumberFormatException nfe) {

  nfe.printStackTrace();

}

> hasSubmittedMessage is a "boolean" HTML form element type with the
> value of "1".  In PHP it's extremely easy to work with because in PHP
[quoted text clipped - 15 lines]
> Thanx
> Phil

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Informer" -- Snow

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

phillip.s.powell@gmail.com - 16 Jan 2006 09:47 GMT
Thanx for your help everyone, but I got it! And I should have known all
along it was basically no different than what we do in PHP.

[CODE]
boolean hasSubmittedMessage;
try {
hasSubmittedMessage =
(Integer.parseInt(request.getParameter("hasSubmittedMessage")) != 0);
// ENCASE IN PARENTHESES
} catch (Exception e) {
hasSubmittedMessage = false;
}
[/CODE]

In PHP we determine a boolean by encasing an expression in parentheses!

Phil

> In article <1137353104.556939.208740@g49g2000cwa.googlegroups.com>,
> phillip.s.powell@gmail.com took the hamburger, threw it on the grill,
[quoted text clipped - 64 lines]
> world is evidently approaching."
> --Assyrian clay tablet 2800 B.C.
opalpa@gmail.com opalinski from opalpaweb - 16 Jan 2006 12:57 GMT
It's not the parantheses.  It's that you added a comparison on two
integers.  You added " != 0"

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
phillip.s.powell@gmail.com - 16 Jan 2006 16:51 GMT
Sorry but it produced a servlet error w/o the parentheses, so it must
be the parentheses as far as I can understand.

Phil

> It's not the parantheses.  It's that you added a comparison on two
> integers.  You added " != 0"
>
> Opalinski
> opalpa@gmail.com
> http://www.geocities.com/opalpaweb/
opalpa@gmail.com opalinski from opalpaweb - 16 Jan 2006 17:10 GMT
I just tried an example and it worked without parantheses.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
opalpa@gmail.com opalinski from opalpaweb - 15 Jan 2006 21:04 GMT
Like James and Noodles point out, in Java, you need be explicit about
the conversion from String to boolean.   If you are controlling a
hidden html paramter send over "true" and "false" and you can write:

 hasSubmittedMessage = new
Boolean(request.getParameter("hasSubmittedMessage"))

if you're in java 5.0

if you're in previous Java versions

 hasSubmittedMessage = new
Boolean(request.getParameter("hasSubmittedMessage")).booleanValue();

if you want a subset of integers to represent true and another subset
to represent false you can explicitly convert them in ways like
suggested by James and Noodle.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.