Search code examples
arraysservletsis-empty

How to check in Servlet if request.getParameterValues has empty data


My form:

<form action="/parvaz.aero/InsertUserCategories" method="post">
    <input type="text" id="fname" name="category" placeholder="">
    <input type="text" id="fname" name="category" placeholder="">
    <input type="text" id="fname" name="category" placeholder="">
    <input type="text" id="fname" name="category" placeholder="">
    <input type="text" id="fname" name="category" placeholder="">
    <input type="submit">
</form>

My Servlet is getting values either its null, empty or data.

String[] category = request.getParameterValues("category");

Checking in Servlet as:

if (null != category && category.length > 0) {
    try {
        //insert in DB
    } catch (SQLException e) {
        
    }           
} else {
    out.println("There is no data to insert in DB";
}

When write: out.print(category.length); by sending the html form empty its giving result as:

5

Why the servlet assuming empty form length as 5 whereas it's received without any data?

How do I know it's empty or has data?


Solution

  • It looks like that you expected that a form submit with 5 empty fields of same name would give you a null or empty array. This is indeed not true. This will just send an array of 5 empty strings.

    Only the complete absence of input field will give you null.

    <form>
        <input type="submit" />
    </form>
    
    String input = request.getParameter("input");
    System.out.println(input); // null
    System.out.println(input == null); // true
    
    String[] inputs = request.getParameterValues("inputs");
    System.out.println(inputs); // null
    System.out.println(inputs == null); // true
    

    Submit of an empty field will give you an empty string.

    <form>
        <input name="input" />
        <input type="submit" />
    </form>
    
    String input = request.getParameter("input");
    System.out.println(input); // empty string
    System.out.println(input == null); // false
    System.out.println(input.isEmpty()); // true
    
    String[] inputs = request.getParameterValues("input");
    System.out.println(inputs == null); // false
    System.out.println(inputs.length); // 1
    System.out.println(Arrays.toString(inputs)); // []
    System.out.println(inputs[0].isEmpty()); // true
    

    Submit of empty fields will give you an array of empty strings.

    <form>
        <input name="input" />
        <input name="input" />
        <input name="input" />
        <input name="input" />
        <input name="input" />
        <input type="submit" />
    </form>
    
    String[] inputs = request.getParameterValues("input");
    System.out.println(inputs == null); // false
    System.out.println(inputs.length); // 5
    System.out.println(Arrays.toString(inputs)); // [, , , , ]
    System.out.println(inputs[0].isEmpty()); // true
    System.out.println(inputs[1].isEmpty()); // true
    System.out.println(inputs[2].isEmpty()); // true
    System.out.println(inputs[3].isEmpty()); // true
    System.out.println(inputs[4].isEmpty()); // true
    

    You basically need to adjust your precondition as follows:

    if (inputs != null) {
        for (String input : inputs) {
            if (!input.isEmpty()) {
                // This input is not empty. Process it.
            }
        }
    }
    

    If you'd like to check beforehand if all inputs are null or empty, then do so:

    if (inputs == null) {
        // Inputs were not included in form.
    }
    else if (Arrays.stream(inputs).allMatch(String::isEmpty)) {
        // Inputs were included in form, but they are all empty.
    }
    else {
        // At least one input is included in form and not empty.
    }