AJAX Tutorial for Java Programmers

The Session

Web Programming uses the concept of a "session" to keep track of user data.

When a user visits your website, he/she may visit more than one pages. Usually for user interface reasons, it is a good idea to separate data entry into several pages. But we would like to be able to connect all these pieces of data with the same user. While he/she is visiting some pages, other users may be visiting other pages using the save web server. So the problem is, how to keep track of the data for each user.

The "session" concept is useful for this. A "session" is an object that is associated with a visitor. Each visitor gets his/her own session object. We can store all data related to the visitor in that session object. But that we don't know if the visitor is going to visit more pages, or has gone away to another site or has closed the browser. So sessions have a "timeout". If the user has not visited any pages within, say, 10 minutes, we assume the user has gone away and delete the session object.

Sessions are provided by web-server back ends, such as Java Application Servers.

Jaxcent uses the session object to provide a java.util.Map that is stored in the session.

Jaxcent can automatically save and restore data from forms in this Map. This data is saved by using the id or name as keys. The data stored consists of INPUT, SELECT and TEXTAREA items. You can also save your own items in this Map, as long as your keys do not collide with the form names/id.

To enable this automatic saving/restoring, the <Page> node in the config XML file needs to have one more entry:

  <AutoSessionData>true</AutoSessionData>
This tells Jaxcent to load form items with session data when the page is loaded, and to save any changes in the session when the page is unloaded.

To try this out, we will create some HTML pages with input elements on them:

  1. An HTML page Name.html with a FORM with INPUT TEXT fields for user first name and user last name (use values firstName and lastName for the NAME attribute.)
  2. An HTML page Address.html with a FORM with INPUT TEXT fields for user street address ("address"), city ("city") and a SELECT for state ("state") containing a few state names.
  3. An HTML page Message.html with a FORM containing a TEXTAREA with NAME "message" for entering a message.
  4. A summary page Summary.html with an empty TABLE with ID "summary".
  5. An index page Contents.html that has A HREF links to all these pages.
  6. Also put in links from Name.html to Address.html, from Address.html to Message.html, and from Message.html to Summary.html, and from all of them to the Contents.html.
  7. Add the JavaScript include statement in the header of all the html files except Contents.html.
Also write an empty Java framework for the Summary.html, using the package "tutorial" and class "Summary". Add a constructor and write some output to System.out, to verify that the page is connected.

Now add entries for all of these except Contents.html, in the config XML file. Note that we only have one Java class. Use jaxcent.JaxcentPage directly for all other HTML pages. In all the new Page node entries in the XML file, also add

  <AutoSessionData>true</AutoSessionData>

Now visit all of these, entering data in the forms. Any data you enter in the forms, should be staying in those forms when you visit the form again. Even if you refresh the page or visit the URL without going back, the data will stay on the page. However, if you leave the browser for more than 10 minutes and then come back, any pages you revisit will have lost their data, because the session would have been lost. (Except that if you have left your browser on one of these pages during those 10 minutes, that page's existing data would get loaded again.)

If you start another browser (not "New Window" from the same browser, but a new instance or a new type of browser, e.g. IE and Mozilla), you will not see your changes. You could enter a fresh set of data using that browser, and it will be kept track of. (Here are you acting as a second user.)

This is happening because of the AutoSession setting.

Exercise:  Get all this to work and verify that it is working by visiting http://localhost/Contents.html in the browser. Verify that all the data sticks.
 

Next Step: Retrieving Session Data