request.getParameter don't have null values when they are expected

23,363

Solution 1

The reason why the statements in the if block runs is that because the field exists. If you don't want to run it when the textboxes are empty what you should do is include in your condition a check if the parameter contains an empty string, something like:

request.getParameter("ID_A") != null && !request.getParameter("ID_A").isEmpty()
|| request.getParameter("Password_A") && !request.getParameter("Password_A").isEmpty()
...

So all the request.getParameter( ) methods above don't contain null values even if I make them empty.

Yes. When getParamater() returns a null value, it means that it can't find a field in the form with that name. Use another method: isEmpty() to check whether a field is empty or not. Such as:

request.getParameter("noSuchField")==null //true
request.getParameter("ID_A")==null //false
request.getParameter("ID_A").isEmpty() //true

Solution 2

<input type="text" name="ID_A" value="" />

When you submit the form this way, you will have ID_A as the key, and "" (empty string) as the value. The way to get a null is to not send ID_A at all, with any value.

A good way around this would be to check for empty string explicitly:

private boolean isNullOrBlank(final String s) {
    return s == null || s.trim().length() == 0;
}

if ((isNullOrBlank(request.getParameter("ID_A"))||
     isNullOrBlank(request.getParameter("Password_A"))
    ) &&
    (isNullOrBlank(request.getParameter("ID_B"))|| 
     isNullOrBlank(request.getParameter("Password_B")))) {
 ...
}
Share:
23,363
Hiroki
Author by

Hiroki

Updated on January 14, 2020

Comments

  • Hiroki
    Hiroki over 4 years

    I found this issue so perplexing (but also interesting) that I'd like to ask people here for insights.

    I've been teaching myself JSP and associated technologies. What I'm tring to do is to retrieve parameters from a JSP to a servlet, then use them with If( ). Here is part my coding.

    if ((request.getParameter("ID_A") != null || request.getParameter("Password_A") != null) &&
        (request.getParameter("ID_B") != null || request.getParameter("Password_B") != null)) {
    
            errorMessage = "Information is detected for the both side";
            request.setAttribute("errorMessage", errorMessage);
            request.getRequestDispatcher("4_enter_personal_info.jsp").forward(request, response);
        } // Other conditions...
    

    Here is part of a JSP(The previous step)

    <form action="PersonalInfor_to_confirmation_servlet" method="POST">
        <h2>For an existing customer</h2>
            <p>Customer ID</p>
            <input type="text" name="ID_A" value="" />
            <p>Password</p>
            <input type="text" name="Password_A" value="" />
            <br>
        <h2>For a new customer</h2>
            <p>Set your customer ID</p>
            <input type="text" name="ID_B" value="" />
            <p>Set your password</p>
            <input type="text" name="Password_B" value="" />
        //There are other lines
    </form>
    

    I'm tring to ensure that, when a client entered information on the both sides (For an existing customer/For a new customer), there will appear the above message "Information is detected for the both side" on a JSP.

    However, even if all the text boxes are blank, the error message appears. So all the request.getParameter( ) methods above don't contain null values even if I make them empty.

    Even if I can come up with other algorithms, I'd like to know the reason this phenomenon happens.

    Any advice will be appreciated.

  • Hiroki
    Hiroki over 9 years
    Thank you so much. This ways looks concise and probably the best one. So, in JSP's world, being null is different from being empty...right?
  • lxcky
    lxcky over 9 years
    @YMD, Yes. null and empty Strings are totally different. Just remember that every field in your form when submitted is not null they have by default a value of empty string "". So remember to also include isEmpty() in your validation.
  • Hiroki
    Hiroki over 9 years
    Thank you professor... I thought that a String variable of value "" would be recognized as null, but it is NOT for JSP. Thank you so much.
  • lxcky
    lxcky over 9 years
    @YMD, but empty String and null is not the same too in Java. I'm glad I was able to help. Just read and try coding. You'll get it soon enough. Good luck! :)