Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Check if string/object null in query string

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?



Avatar of kennethxu
kennethxu

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 )
Avatar of MJ

ASKER

I tried the null check and it fails for the second case!
ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or
if( !var1.equals("") )

it's the same as:

var1.length() > 0