Section 3.2. Running the Example


3.2. Running the Example

Start Tomcat and open a browser. Enter the address where you deployed the application. If you deployed it using the build.xml file in this book, the address should be http://localhost:8080/ajax-decimal/index.html.

Your browser should display the page shown in Figure 3-2. When you press a key, the JavaScript will move the key pressed to the "Key Pressed:" field, clear the input field, and make a request to the server to convert the key to its decimal value. The server then does the simple conversion and returns the value, which is picked up by the callback function and put into the Decimal field.

Figure 3-2. Ajax Character Decoder running in Mozilla Firefox


Congratulations! You've just completed a full Ajax-enhanced Java application. As simple as the application is, it demonstrates most of the major components you need in order to develop Ajax applications with Java.

Notice that I said most, not all, of the components. We are still missing an important piece of Ajax: XML. And, to be sure, there are fancier, cleverer ways to do things: we can use JSF components and the like, and we can come up with better ways to handle the IE/Firefox differences. But still, we now have a working application. Everything else is icing on the cake.

3.2.1. So Where's the XML?

It's true, there is no XML in this example. I avoided XML for two reasons: I wanted to keep the first example as simple as possible, and to show that you don't really have to use XML with Ajax.

So how does XML fit into Ajax? Because Ajax is about passing data back and forth between the browser and the server, there is a need to parse that data. In this very simple example, we only passed one field from the browser to the server and one field back from the server to the browser. In this case, XML is overkill. However, for real-world applications, you'll need to move more complex data, and you'll want a more structured way to represent your data.

The next chapter will illustrate how easy it is to use XML with Ajax to parse the data coming from the server. It will also demonstrate how to use JavaScript Object Notation ( JSON), a native JavaScript data representation that you may find more convenient than XML.




Ajax on Java
Ajax on Java
ISBN: 0596101872
EAN: 2147483647
Year: 2007
Pages: 78

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net