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


}
%>

Dani AI

Generated

correctly flagged the string-comparison angle, but the immediate reason the error_01 branch never ran in 's scriptlet is different: when an input element with a name is present in the form, most browsers submit that parameter even if the user leaves it blank. That means request.getParameter("username") will usually be "" (empty string), not null, so a == null check will miss it.

A safe, practical server-side check handles both "missing" and "empty-or-whitespace" cases and avoids NullPointerExceptions by checking for null first. For example:

String username = request.getParameter("username");
String password = request.getParameter("password");

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

Troubleshooting notes and best practices:

  • Always test for null before calling methods on a string to avoid NPEs.
  • trim() removes accidental spaces; use it when blank input should be treated as empty.
  • Consider StringUtils.isBlank() (Apache Commons Lang) if available for cleaner checks.
  • Relying on a submit button name to detect submission can be flaky; checking request.getMethod() for "POST" or adding a hidden field is more robust.
  • Prefer moving validation out of JSP scriptlets into a servlet/controller or use JSTL/EL for cleaner separation of concerns and easier testing.

Quick debug tip: temporarily print the raw parameter values (wrapped in quotes) to confirm whether they are null, "", or contain whitespace.

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.