4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Hiroki
  • 3,893
  • 13
  • 51
  • 90
  • Related: http://stackoverflow.com/questions/7109419/what-is-the-best-practice-for-validating-parameters-in-jsp/ – BalusC Jan 13 '15 at 14:50

2 Answers2

6

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
lxcky
  • 1,668
  • 2
  • 13
  • 26
  • 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? – Hiroki Jan 13 '15 at 03:56
  • 1
    @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. – lxcky Jan 13 '15 at 05:22
  • 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. – Hiroki Jan 13 '15 at 05:29
  • @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! :) – lxcky Jan 13 '15 at 05:34
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")))) {
 ...
}
Todd
  • 30,472
  • 11
  • 81
  • 89