permaculture playing cards*
The moose likes JSP and the fly likes JSP Parameters and HTML Form Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP Parameters and HTML Form Values" Watch "JSP Parameters and HTML Form Values" New topic
Author

JSP Parameters and HTML Form Values

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
When I pass a value from an HTML form to a JSP page and the user hasn't typed anything into one of the text fields, how do I test that it's null?
String name = request.getParameter("name");
if(name==null)
if(name=="")
Neither of these seem to work?! Yikes!
Thanks,
Drew
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
There I go again trying to use if(name=="") where I should be using if(name.equals(""))
If I had a dime for everytime I've done that...shit!
Sorry,
Drew
Vlad Patryshev
Ranch Hand

Joined: Jun 30, 2001
Posts: 61
To avoid this kind of bugs forever, I am using this cheap and dirty solution:

/**
* checks whether a string is empty
* @param s
* @return true if empty
*/
public static final boolean isEmpty(String s) {
return (s == null) || (s.length() == 0) || "null".equals(s);
}


Thanks,<br />Vlad
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
i liked Vlad's solution to the problem but one should know,
- if query param is having NO value then we will have "" as a value when we do getParameter().
- if query param is NOT there(doesn't exists) in the request then we will have null as return of getParameter()
-else we will have some value.
hth,
maulin.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
sorry..forgot to mention...
u gotta know == is not same as equals() method. == is just a reference equality check not the content check...
-maulin
lakshmi rajagopal
Greenhorn

Joined: Jun 22, 2002
Posts: 5
you can directly give as follows
if (request.getParamter("name") != null)
name = request.getParameter("name")


lakshmi
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
You should decide if this is what you want, as well.
if( name != null && name.trim().length() != 0 ) {
// retrieve the parameter...
}
This will catch all white space.
Sean
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSP Parameters and HTML Form Values