The basic idea for making a form sticky is to use the query string data. A sticky form is one that re-displays 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 the setAttribute and getAttribute methods of the request to add information to the request object for the following reasons
This is how I would handle a typical input element
String temp = request.getParameter("first");
request.setAttribute("first", temp);
if (temp == null) {
request.setAttribute("first","");
}
Then, wherever I can access the request object, I can access all of the attributes. I never get null when I want "". I can change the attributes as I choose. Later in my servlet, I would refer to the value of the "first" parameter by calling the attribute. The only problem (advantage???) is that attributes can be any type, so I need to cast them when retrieving them:
out.println("first = " + (String) request.getAttribute("first"));
For the remainder of this discussion, I will assume that I have done this for all of my input variables.
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((String)
request.getAttribute("first"));
out.println("' >");
Textareas are similar
out.print("<textarea name='area'>");
out.print((String)
request.getAttribute("area"));
out.println("</textarea>");
For radio buttons, create a new map with the value
'checked'
for the value for the radio button.
Map checkedTeam = new HashMap();
//set the radio button to checked if its value was sent in the form
data
String teamChosen= (String)
request.getAttribute("team");
if (teamChosen != "") {
checkedTeam.put(teamChosen , "checked");
}
Then generate the radio buttons using the map to insert null or checked into the input element. Each null will be ignored by the browser.
out.print("<input type='radio' name='team' value='Heat'
");
out.print(checkedTeam.get("Heat"));
out.println("> Heat");
out.print("<input type='radio' name='team' value='Dolphins'");
out.print(checkedTeam.get("Dolphins"));
out.println("> Dolphins");
out.print("<input type='radio' name='team' value='Marlins'
");
out.print(checkedTeam.get("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 String teamNames[];
//Instantiate it in the init method
teamNames = new String[] {"Heat", "Dolphins",
"Marlins"};
//set the default value to the empty string for all radio buttons
for (int i=0; i<teamNames.length; i++) {
checkedTeam.put(teamNames[i] , "");
}
//set the radio button to checked if its value was sent in the form
data
String teamChosen= (String)
request.getAttribute("team");
if (teamChosen != "") {
checkedTeam.put(teamChosen , "checked");
}
//Loop through the names and create the radio buttons
for (int i=0; i<teamNames.length; i++) {
out.print("<input type='radio' name='team' value='" + teamNames[i]
+ "' ");
out.print(checkedTeam.get(teamNames[i]));
out.println("> " + teamNames[i] + "");
}
A similar technique is used for checkboxes, where more than one box can be selected.
Map checkedColor = new HashMap();
//set the radio button to checked if its value was sent in the form
data
String[] colorChosen= request.getParameterValues("color");
for (int i=0; i < colorChosen.length; i++) {
Then generate the checkbox buttons using the map to insert null
or checked into the input element. Each null will be ignored
by the browser.
checkedColor.put(colorChosen[i] , "checked");
}
out.print("<input type='checkbox' name='color' value='Red'
");
out.print(checkedColor.get("Red"));
out.println("> Red");
out.print("<input type='checkbox' name='color' value='Blue'");
out.print(checkedColor.get("Blue"));
out.println("> Blue");
out.print("<input type='checkbox' name='color' value='Green'
");
out.print(checkedColor.get("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 String colorNames[];
//Instantiate it in the init method
colorNames = new String[] {"Red", "Blue",
"Green"};
//set the default value to the empty string for all radio buttons
for (int i=0; i<colorNames.length; i++) {
checkedColor.put(colorNames[i] , "");
}
//set the checkbox button to checked if its value was sent in the form
data
String[] colorChosen= request.getParameterValues("color");
for (int i=0; i < colorChosen.length; i++) {
checkedColor.put(colorChosen[i] , "checked");
}
//Loop through the names and create the radio buttons
for (int i=0; i<colorNames.length; i++) {
out.print("<input type='checkbox' name='color' value='" + colorNames[i]
+ "' ");
out.print(checkedColor.get(colorNames[i]));
out.println("> " + colorNames[i] + "");
}
Selection lists where mulitples values can be selected are similar to checkbox buttons.