|
|
||
Retrieving Session DataIt takes no additional work beyond specifying AutoSessionData to true, to get all the user's input into session. Of course, in a real-life situation, we may want to add more code to each page to verify data etc. But the saving/restoring into forms itself is free.
To get the data from the session, the method The method returns a map, so all we have to do is retrieve values from the map. Note that there is a distinction between the values stored in the map: if the user has never visited a page, the values for that page are not in the map. So retrieving any data from that page will return a null. But if the user has visited a page and has just not entered data in a field, retrieving data from that field will return an empty string instead of a null. We can now build a small utiltiy to write out a row to our output table.
void writeValue( HtmlTable table, // Table to output to
String description, // Description of key (to output)
Object value // Value of the key
) throws Jaxception
{
if ( value == null ) // Page containing the item was never visited.
value = "<I>Not Visited</I>";
table.insertRow( -1, new String[]{ description, (String) value } );
}
Using this utility, we can modify the constructor of Summary.java to
write out all items.
public Summary()
{
try {
HtmlTable table = new HtmlTable( this, "summary" );
java.util.Map data = getAllSessionData( false );
// Add a header.
table.insertRow( -1, new String[]{ "<B>Item</B>", "<B>Value</B>" } );
// Add items.
writeValue( table, "First Name", data.get( "firstName" ));
writeValue( table, "Last Name", data.get( "lastName" ));
writeValue( table, "Address", data.get( "address" ));
writeValue( table, "City", data.get( "city" ));
writeValue( table, "State", JaxcentObject.getSelectedValue( data.get( "state" )));
writeValue( table, "User Message", data.get( "message" ));
} catch (Exception ex) {
ex.printStackTrace();
}
}
Exercise:
Get all this to work. Starting from
http://localhost/Contents.html
in the browser, verify that the summary page shows all data entered,
and that every time you visit it, it changes to reflect any changes
you made on the pages.
|
||
|