0

i dont understant why its not going inside "error_01 or error_02" if statment?

if user enter username and hit sumbit than it goes in "worked" if statment
but if user dont enter any thing in usrname it still go in "worked" if statment.

<!-- login form -->
<form method ="POST" action ="login.jsp">
        Enter username:
        <input name="username" />
        Enter password:
        <input name="password" />
        <button type="submit" name="login">login</button>
    </form>


<%
if (request.getParameter("login") != null) 
{
    String username = request.getParameter("username");
    String password = request.getParameter("password"); 

    if(username == null)
    {
        out.print("error_01");
    }
    else if (password == null)
    {
        out.print("error_02");
    }
    else
    {
        out.print("worked");
    }


}
%>

Edited by Reverend Jim: Moved to correct forum

2
Contributors
3
Replies
18
Views
4 Years
Discussion Span
Last Post by M4trixSh4d0w
0

I think you're trying to use str1 == str2 instead of str1.equals(str2)
like

if(username.equals(""))
{
out.print("error_01");
}
else if (password.equals(""))
{
out.print("error_02");
}

Edited by M4trixSh4d0w: code tags

0

ah i c. so when compareing number we use == sign but when we comparing string we use equal?

0

yea. I think that when you use == with strings it compares the memory location, rather than the data at the menmory location, which is compared using .equals()

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.