The basic idea for making a form sticky is to use the query string or post data. A sticky form is one that redisplays the data that the user sent. After the data is parsed by the servlet engine into parameters, it can be used to set the default values for the input elements of a form.
I like to use Beans for the following reasons
Refer to populating Beans for more information on using Beans with servlets.
For the remainder of this discussion, I will assume that I have created a Bean and populated it with all of my input variables. Assume that the Bean is named myBean.
Be sure that your Bean extends my class PresetCheckedAndSelected.java. This will add a HashMap that is part of your Bean. Review the Documentation.
String getChecked(String key)
String getSelected(String key)
void setChecked(String key)
getChecked
for this value.
void setSelected(String key)
getSelected
for this
value.
void setChecked(String[] keys)
getChecked
for any of
the keys in the array.
void setSelected(String[] keys)
getSelected
for any of
the keys in the array.
The single quotes around the value are very important if there are embedded blanks in the value.
out.print("<input type='text' name='first' value='");
out.print(myBean.getFirst()
);
out.println("' >");
out.print("<textarea name='area'>");
out.print(myBean.getArea()
);
out.println("</textarea>");
First call setChecked
of the Bean in the setter for the radio
property. The setChecked
method will not add an entry if the
key is null or the empty string.
//In the setter for the radio button group, callsetChecked
//for the value that was sent in the form data public void setTeam(String team) { this.team = team;setChecked(team)
; }
Then generate the radio buttons using the Bean to insert "" or checked into the input element.
out.print("<input type='radio' name='team' value='Heat' "); out.print(myBean.getChecked("Heat")
); out.println("> Heat"); out.print("<input type='radio' name='team' value='Dolphins'"); out.print(myBean.getChecked("Dolphins")
); out.println("> Dolphins"); out.print("<input type='radio' name='team' value='Marlins' "); out.print(myBean.getChecked("Marlins")
); out.println("> Marlins");
If you really want to make it slick, create an array of strings for the values for the buttons. Then use a loop to generate all the radio buttons
//Declare the array as an instance variable in the class
private static String teamNames[] = {"Heat", "Dolphins", "Marlins"};
//Loop through the names and create the radio buttons
if (teamNames != null) {
for (int i=0; i<teamNames.length; i++) {
out.print("<input type='radio' name='team' value='" + teamNames[i] + "' ");
out.print( myBean.getChecked(teamNames[i])
);
out.println("> " + teamNames[i] + "");
}
}
First call setChecked
of the Bean in the setter for the checkbox
group property. The setChecked
method will not add an entry if the
key is null or the empty string.
//In the setter for the checkbox group, callThen generate the checkbox buttons using the map to insert "" or checked into the input elementsetChecked
//for the values that were sent in the form data public void setColor(String[] color) { this.color = color;setChecked(color)
; }
out.print("<input type='checkbox' name='color' value='Red' "); out.print(myBean.getChecked("Red")
); out.println("> Red"); out.print("<input type='checkbox' name='color' value='Blue'"); out.print(myBean.getChecked("Blue")
); out.println("> Blue"); out.print("<input type='checkbox' name='color' value='Green' "); out.print(myBean.getChecked("Green")
); out.println("> Green");
If you really want to make it slick, create an array of strings for the values for the buttons. Then use a loop to generate all the checkbox buttons
//Declare the array as an instance variable in the class private static String[] colorNames = {"Red", "Blue", "Green"};
//Loop through the names and create the checkbox buttons
if (colorNames != null) {
for (int i=0; i<colorNames.length; i++) {
out.print("<input type='checkbox' name='color' value='" + colorNames[i] + "' ");
out.print(myBean.getChecked(colorNames[i])
);
out.println("> " + colorNames[i] + "");
}
}
Here is a link to a directory that contains an example of the BidServlet example from the book, but with a checkbox group instead of a single checkbox: complexBean
Multiple Selection lists can be selected similarly to checkbox buttons. Use getSelected/setSelected instead of getChecked/setChecked.