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");
    }


}
%>

Recommended Answers

All 3 Replies

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");
}

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

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()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.