Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing Empty String in JSP

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
I have the following in a .jsp that's called from a form:

String myString = request.getParameter("formfield");

If the user doesn't enter anything in "formfield", it should be blank.

So how come when I say:

if (myString == "") //representing null

out.println("field is blank");

It doesn't print anything. However, if I try to print myString, it is blank.

Am I testing a null string correctly?

Thanks.




Change Your Thinking, Change Your Life.
 
if (myString == null) {

}

and the opposite :

if (myString != null) {

}

 

Thanks.

I had already used myString != null, but testing myString == null still doesn't work, even if I have the attribute value="" in my form object.

There seems to be something that's set for the value of that object as I land on the JSP page or when I call request.getParameter("formfield").

Thanks, though.




Change Your Thinking, Change Your Life.
 
Then you String is not null - but it may be of zero length :

if (myString == null || myString.length() == 0) {

}
 
Yes. Now when I test the entire string by including the length, I get inside the function.

Why is that? If the user doesn't select anything, shouldn't it be null?

Thanks for your help.




Change Your Thinking, Change Your Life.
 
If you assign the field an empty string (""), then the receiving request.getParameter(...), I suppose, doesn't see this as null. If, rather, you don't assign the empty field any value, I imagine it would still be null.

You cannot use Java to do a (myString == "") check. This gets into the fact that myString may be "", but in the computer's internal memory, it is still distinct from the "" you are checking to see if it is == to! Follow? They may be equal in value, but they are still distinct (i.e., not the same, so not ==) Strings.

If, instead, you had tried if(myString.equals("")) ... then you would have found that the program was responding as expected. The X.equals(...) function checks to see if the literal value of the Strings are equal.

Note that this distinction between == and .equals(...) is for objects. Primitives respond as you would expect to ==.

'hope that answers your question.

--Dave
 
Depends on how the servlet container is implemented. I imagine tomcat is written along these lines :

Analyse the html page, and see what <input> tags there are.
Initialise an empty String for each (ie new String("");
On submit, fill these Strings with the <input> values if any.
Add them to the HashMap of parameters in the HttpServletRequest

Hence, there is a String object associated with each request parameter (ie the former submitting page's <input> tags) - now if the user doesn't enter anything, then I think it is perfectly valid for the request parameter to be an empty String - because the parameter does exist - its just empty.
Hence the difference between this scenario, and when you do a request.getParameter() on something that does not exist in the previous submitting page's <input> tags ...
 
Thanks, LookingForInfo and sedj.

I'll look more into after I complete this one page....and let you know what I find.

Thanks for your help.

scripter73


Change Your Thinking, Change Your Life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top