We help IT Professionals succeed at work.

Check if string/object null in query string

894359
894359 asked
on
Medium Priority
658 Views
Last Modified: 2010-04-01
If I'm passing a parameter in a query string:

test.jsp?var1=value1

and on the recieving page I get:

String var1 = request.getParameter("var1");

How do I check if no parameter was passed? For example if  this happens:

test.jsp

this checks
if (var1 != null) {}

but what about this case with no value in the query string???
test.jsp?var1=

I thought
 if (var1 != "") {}

 but that doesn't work, what's the check?



Comment
Watch Question

unfortunately its also null.
in your application, you shoudl thread test.jsp?var1= the same as test.jsp
I always do this check:
if( var1 != null && var1.length() > 0 )

Author

Commented:
I tried the null check and it fails for the second case!
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
or
if( !var1.equals("") )

it's the same as:

var1.length() > 0
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.