AJAX Tutorial for Java Programmers

Retrieving Session Data

It 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 getAllSessionData provided in JaxcentPage class is used. It takes a single parameter, which we will specify as false. If the parameter is true, it tells Jaxcent that we also want to get the most recent information from the current page. But our summary page has no form input, so we don't need that.

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.


Next Step: Navigation in Web Applications
Contents