On Thu, 13 Jan 2000, Fengrui Gu wrote:
> There is a common mistake here.
> "if (pswd==null) ..." never works for checking null value of a
> string. As you know, paswd is a reference(from the point of
> compiler) to the object. pswd just refers to an String object which
> has null value. It can not be a null value. Either use equals
> method of String or use length method.
Sorry, but the mistake is yours. Checking for a string == null is
perfectly OK. A String variable is no different from any other object
variable in this regard. It either points (refers) to an instance of
the appropriate object, or is null. Your phrase "an [sic] String
object which has null value" isn't really meaningful. If you try to
use equals or length when the String is null, you will get a
NullPointerException. (One point of confusion may be that you can
print a String that is null, and "null" is what is printed. But that
is really a different issue, a special case.)
Witness the following code:
public class StringTest {
private static String s;
public static void main(String argv[]) {
if (s.equals(""))
System.out.println("sorry, s is null/empty");
else
System.out.println(s);
}
}
It compiles fine, but when you run it, you get a NullPointerException
(because s is initialized to null). If you change the condition on
the if to be:
if (s == null || s.equals(""))
it compile/works fine, and "sorry, s is null/empty" is printed.
> At 02:49 PM 1/13/00 +1000, you wrote:
> >Gidday Harish
> >You need to check the length of the parameter as well, an empty field(
> >e.g. value="") sends a empty not a null string.
> >
> >int minPassLen = 6; // this is up to you
> >if (pswd==null || pswd.length() < minPassLen) {//show the login page
> >again with a error message}
> >
> >cheers pb..
> >
> >Harish Satyanarayanrao wrote:
> >>
> >> Hi,
> >>
> >> I have a problem in a servlet which i wrote.
> >>
> >> The servlet accepts a username and password from a HTML form.
> >>
> >> I am accessing the same using getParameter function in the doGet function.
> >>
> >> String pwsd = request.getParamater("PASSWORD");
> >>
> >> To check if the user pressed the submit button without entering the
> password,
> >> I tried
> >> if (pswd==null) {//show the login page again with a error message}
> >>
> >> The program does not enter this if loop .
> >> (The string pswd is getting assigned a value if i do enter the password
> in the
> >> HTML form)
> >>
> >> Am i missing something?
Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html