<%@ taglib uri="http://jakarta.apache.org/taglibs/input-1.2" prefix="input" %>

Checkboxes are slightly troublesome elements as when they are unchecked they submit no value at all. So it is difficult to detect whether we are seeing a form for the first time, and therefore should use the default values for a checkbox; or whether we are repopulating a form and the absense of a value in the request for a given checkbox means it was unchecked.

The input tag library previously decided whether to use defaults or not based on whether there were any request parameters at all. If there were any request parameters then it was assumed we are repopulating the form and therefore checkbox default values aren't used.

As of version 1.2 the input tag library can detect if there are parameters in the request matching other tags in the form. If there aren't then it knows that the form isn't being repopulated, even though there may be other parameters in the request. Note that it can only be sure if there are non-checkbox fields in the form, so if there are only checkboxes then it reverts back to the original behaviour as above.

To make use of this new behaviour you must use an <input:form> tag rather than the plain <form> tag.

This form has a text field and checkboxes, so it can correctly detect whether to use defaults. You can verify that by viewing the page with a query parameter that doesn't match any field in the form, and see that even though there are parameters in the request the checkboxes can still see that they should use their default values.

What is your name?:

<% String[] defaults = new String[] { "rabbits", "donkeys" }; %>

Which animals do you like?:
Cats
Dogs
Rabbits
Horses
Donkeys

Compare the above form with this form where there are only checkboxes. The checkboxes can't accurately detect whether they should use their default values or not. You can see this when the defaults aren't used, contrary to the example above, when viewing the page with a query parameter that doesn't match any field in the form.

<% String[] defaults = new String[] { "rabbits", "donkeys" }; %> Which animals do you like?:
Cats
Dogs
Rabbits
Horses
Donkeys