Making Forms Sticky with Beans

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)
This will return either the empty string or the word "checked"
String getSelected(String key)
This will return either the empty string or the word "selected"
void setChecked(String key)
This should be called prior to calling getChecked for this value.
void setSelected(String key)
This should be called prior to calling getSelected for this value.
void setChecked(String[] keys)
This should be called prior to calling getChecked for any of the keys in the array.
void setSelected(String[] keys)
This should be called prior to calling getSelected for any of the keys in the array.


The text, password, and hidden fields all use the same technique

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("' >");


Textareas
out.print("<textarea name='area'>");
out.print(myBean.getArea());
out.println("</textarea>");


Radio Buttons

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, call setChecked
//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] + "");
  }
}


Checkboxes

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, call setChecked
//for the values that were sent in the form data
public void setColor(String[] color) {
  this.color = color;
  setChecked(color);
} 

Then generate the checkbox buttons using the map to insert "" or checked into the input element

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


Single Selection lists can be selected similarly to radion buttons. Use getSelected/setSelected instead of getChecked/setChecked.

Multiple Selection lists can be selected similarly to checkbox buttons. Use getSelected/setSelected instead of getChecked/setChecked.