Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-18-2004, 10:25 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
trying to capture ""

Code:
<%
if( request.getParameters("name") == "" ) {
  out.println("No Name Selected");
}
%>
ok, really basic here i'm sure. i have a form with a text field 'name'. when there is nothing in that field, i want it to trigger the above if statment.

for some reason, i can not catch "" .. the value is NOT null either because i'm sending the field.

here would be the html form:
Code:
<form method=post action=test.jsp>
name: <input type=text name=name> <input type=submit name=submit value=submit>
</form>
how can i print 'no name selected' if someone submits the form without anything in it? ( no client side stuff )

so basic hehe, .. php spoils me.
__________________
Mike
sde is offline   Reply With Quote
Old 04-19-2004, 06:00 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,140
Belisarius is on a distinguished road
"==" doesn't do what you think it does. You want equals()."==" compares the pointers of two objects.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-19-2004, 06:02 AM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,140
Belisarius is on a distinguished road
Oh, and as a followup, I recently discovered that "==" will sometimes return true, even in instances it shouldn't due to java optimizations. Don't use "==" to compare objects, only use it to compare primitives. Always use equals() to compare objects.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-19-2004, 06:43 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
thanks!
__________________
Mike
sde is offline   Reply With Quote
Old 04-19-2004, 06:52 AM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,140
Belisarius is on a distinguished road
On the subject of best practices, you might want to check to make sure whatever you're using equals with isn't null, or else a NullPointerException will get thrown.

The way I usually tackle the problem is

if(input == null){
input = "";
}
return input.trim();
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-19-2004, 07:07 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
so then to do what i was explaining, maybe try this?
Code:
if(input == null){
  input = "";
}

if(input.trim().equals(""))
{
  out.println("Please enter a name.");
}
__________________
Mike
sde is offline   Reply With Quote
Old 04-19-2004, 07:18 AM   #7 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,140
Belisarius is on a distinguished road
Yep, that would cover the possibility of input being null. You could create a method to do it, so you don't need as many lines of code.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 02:40 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting