<< Chapter < Page | Chapter >> Page > |
The list of names
Listing 7 provides a line of introductory text for the list of names to be displayed on the screen. The first time the form appears on the screen,the value stored in the name variable is null, and the word Empty is displayed as shown in Figure 1 . Later on after multiple requests, the list will be populated as shown in Figure 2 .
Listing 7 - Beginning of the list of names. |
---|
out.println("<p>Your list of names is:<br/>");
if(name == null){out.println("Empty</p>");
}//end if |
Create new hidden fields for historical data
Listing 2 retrieved all of the data in the hidden fields on the form and saved that data in a String array named items .
If that array contains data, the code in Listing 8 performs two actions using the data from each of the elements in the array:
Listing 8 - Display the historical data and also save it on the browser. |
---|
if(items != null){
for(int i = 0; i<items.length; i++){
//Display names previously saved in hidden fieldsout.println(items[i] + "<br/>");
//Save the names in hidden fields on form currently// under construction.
out.println("<input type=\"hidden\" name=\"item\" "
+ "value=\"" + items[i]+ "\">");
}//end for loop}//end if |
Limited persistence
Thus, the historical data is passed forward from the hidden fields of one HTML form to the hidden fields of the next HTML form. All of the historical dataresides on the HTML form in the browser cache in the client computer. However, that is not a location that provides long-term persistence, so this scheme forsession tracking has limited persistence.
Create new hidden field for new data
Listing 3 retrieved the field value submitted by the browser and saved that value in a variable named name .
If that variable contains data, the fragment in Listing 9 performs two actions using the data stored in the variable.
Listing 9 - Display most recent value and also store it on the browser. |
---|
if(name != null){
//Display name submitted with current GET requestout.println(name + "<br/>");
//Save name submitted with current GET request in a
// hidden field on the form currently under// construction
out.println("<input type=\"hidden\" name=\"item\" "
+ "value=\"" + name + "\">");
}//end if |
Thus, for each new request by the browser, one additional hidden field is added to the HTML form under construction. The new hidden field contains thefield value submitted by the browser.
The remaining servlet code
The remainder of the code for the servlet is typical of what you have seen before and can be viewed in the complete listing of the servlet in Listing 16 .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?