Simple Form, not so simple Problem
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jesse_Uribe
Posted On:   Tuesday, December 11, 2001 09:10 PM

I sure hope somebody can help me figure this out! I new to servlets and this is my first one trying to use form data Here's part of testform.html: ************************************* And here's part of testform.java: *********************************** username=request.getParameter("username"); out.println("Username=""+username+"" "); if (username=="USER"){ out.println("Hello User"); } else{ out.println("Your Not User"); } And here's the    More>>

I sure hope somebody can help me figure this out!

I new to servlets and this is my first one trying to use form data


Here's part of testform.html:

*************************************









And here's part of testform.java:

***********************************

username=request.getParameter("username");

out.println("Username=""+username+""
");

if (username=="USER"){

out.println("Hello User");

}

else{

out.println("Your Not User");

}


And here's the result when I click the submit button:

************************************

Username="USER"

Your not User



Anybody know why, the output even says username="USER" but I can't get the "Hello User" I should. please help out, i'm using jdk1.3 in linux (I have tried jdk1.2 with the same result) Is it a bug?

   <<Less

Re: Simple Form, not so simple Problem

Posted By:   Christopher_Schultz  
Posted On:   Wednesday, December 12, 2001 08:32 AM

Here's a tip for writing Java programs:



== != .equals().



When you are checking "USER" for referential equality to username you are comparing two different references: one references the username variable you have in your method. The other references the constant "USER". They are not the same, even though their content may be the same.



Your code amounted to the following:



if(username == "Some completely unrelated string")



You should use .equals to do object comparison, as suggested by the other reply.



-chris

Re: Simple Form, not so simple Problem

Posted By:   Mittal_Bhiogade  
Posted On:   Tuesday, December 11, 2001 09:33 PM

I think some space may be there try the following way..

(Username.trim()).equals("USER")
or
(Username.trim()).equalsIgnoreCase("USER")
About | Sitemap | Contact