Ajax and You
It's almost impossible today to be involved in web application design or development and not be aware of Ajax, a technology that includes but is not limited to Asynchronous JavaScript and XML. That's because Ajax is currently the primary technique for driving the high responsiveness and interactivity of some of the most popular applications on the web such as Google Maps and Flickr. These applications are representative of a new generation of highly responsive, highly interactive web applications, referred to as Web 2.0 applications, that often involve users collaborating online and sharing content. Ajax has different implications for developers working in different roles. For example, component developers creating custom components for web applications build Ajax functionality into the design. Page authors use these Ajax components, along with widgets, JavaScript technology, and other techniques, to incorporate Ajax functionality into their web applications. Ajax impacts other roles too. For example, enterprise application developers need to add logic in server-side components to handle Ajax-related requests directed to the server. This article focuses on page authors and describes various techniques that you can use to add Ajax functionality to a web page. What You Need to Know About Ajax
As a page author, you don't need to understand every detail of the Ajax methodology to incorporate it into a web page. However, you should have a general idea of what Ajax is and how it works. As this article mentioned earlier, Ajax enables the high responsiveness of many web applications. For example, a web site such as Google Maps uses Ajax to provide a highly responsive user interface (UI). You can view a map, then move your cursor across it to see adjacent areas almost immediately. Ajax enables high responsiveness because it supports asynchronous and partial refreshes of a web page. A partial refresh means that when an interaction event fires -- for example, a user moves the cursor across a Google map -- a web server processes the information and returns a limited response specific to the data it receives. Significantly, the server does not send back an entire page to the client of the web application -- in this case a web browser -- as is the case for conventional "click, wait, and refresh" web applications. The client then updates the page based on the response. Asynchronous means that after sending data to the server, the client can continue processing while the server does its processing in the background. This means that a user can continue interacting with the client without noticing a lag in response. For example, a user can continue to move the mouse over a Google map and see a smooth, uninterrupted change in the display because extended parts of the map have been loaded asynchronously. The client does not have to wait for a response from the server before continuing, as is the case for the traditional synchronous approach. Ajax and JavaScript Technology
Underlying all of the approaches for adding Ajax functionality to a web page is JavaScript technology. In this article, you'll see that in some approaches, such as using an Ajax-enabled JavaServer Faces component, the component encapsulates the JavaScript code,so you don't have to do any JavaScript coding. In other approaches, such as using a widget in a JavaScript library, the library provides most of the JavaScript code, but you will need to add a little JavaScript code yourself. If you don't find components or widgets that provide the Ajax functionality you need, you must do a lot more JavaScript coding yourself. However, you don't have to rely on any one of these approaches -- You can use various approaches in combination. Scripts written in the JavaScript scripting language handle much of the Ajax-related processing in the client as illustrated in Figure 1.
Like other web applications, an Ajax-enabled web application uses a markup language such as HTML or XHTML to present web pages, or a server-side technology such as JavaServer Pages (JSP) technology to generate web pages. In addition, an Ajax-enabled web application typically uses cascading style sheets (CSS), a stylesheet language, to define presentation style, such as fonts and colors. A server-side application system such as Java Platform, Enterprise Edition (Java EE), that includes support for data validation, user identity management, and persistence fits very well with the Ajax methodology. For more information on what Ajax is and how it works see the article "Asynchronous JavaScript Technology and XML (Ajax) With the Java Platform". You can find additional learning resources at the Ajax Developer Resource Center. Adding Ajax to a Web Page
You can add Ajax functionality to a web page in many ways. In general, these approaches vary in the amount of JavaScript code you need to incorporate into the page. At one end of the spectrum, you provide all the JavaScript code required to enable Ajax in the page. At the other end of the spectrum, you take advantage of techniques that allow you to build Ajax into a web page with little or no JavaScript coding. You can also combine approaches. Let's first look at one of the approaches that requires little or no JavaScript coding -- using an Ajax-enabled component from a component framework. Using Ajax-Enabled Components
Perhaps the easiest way to incorporate Ajax into a web page is to add an Ajax-enabled component from a component framework such as the JavaServer Faces technology framework. Other component frameworks exist, but many of them -- such as the Shale Framework, ICEfaces, Ajax4jsf, and the JSF Extensions project -- are built on top of JavaServer Faces technology. So let's focus on the JavaServer Faces technology framework.
JavaServer Faces technology, often referred to as JSF, allows you to build functionally rich web pages for web applications using UI components, such as those for labels, buttons, text fields, and scrollbars. These components run on a server and are rendered to the page. The components can be enabled to respond to events such as a user clicking a button or entering characters in a text field, and to convert and validate input data. Component developers find the technology particularly powerful because it contains a set of application programming interfaces (APIs) for representing UI components and for managing their state. The ability to manage state is important because it provides a way to synchronize the properties of a UI component between the server and client. This synchronization makes it possible to perform multiphase operations on a UI component such as converting and validating input data for the component prior to handling an event. JavaServer Faces technology includes standard libraries for a basic set of UI components. However, the component model is extensible so that a component developer can add custom components. This extensibility allows a component developer to create custom components that have Ajax functionality or add Ajax functionality to existing components. An Autocompletion Component After creating an Ajax-enabled custom component, the component developer adds it to a JavaServer Faces component library. A number of libraries of Ajax-enabled custom JavaServer Faces components are available. For example, a library of Ajax-enabled components is available as part of the Java Blueprints Solutions Catalog. One of the components in that library is an autocompletion component. Let's examine this component in action and see how you can add it to a web page. The autocompletion component is designed to work with a form. When a user types into a text field that is associated with the component, it triggers a JavaScript function -- an event handler -- that suggests characters that complete the entry. For example, a demonstration of the autocompletion component in the Java Blueprints Solutions Catalog shows it in use with a name and address form. When a user types one or more characters in the City or State field in the form, the autocompletion component triggers an event handler that displays a pop-up list of cities or states beginning with those characters, as Figure 2 shows.
When the user selects a city in the list, the action triggers an event handler that fills in the city, state, and zip code for the selection. Note that the autocompletion actions are performed asynchronously. The application displays a suggested list of cities and fills in the city, state, and zip code without needing to refresh the page. Including the Autocompletion Component in a Page
You can use an Ajax-enabled custom component just as you would any other JavaServer Faces custom component. You can express a component using a JavaServer Pages technology (JSP) tag on a JSP page, although you're not limited to using JSP technology to display the page. For example, you can express a component in an XML document. In addition, JavaServer Faces components are designed to be easily importable into integrated development environments (IDEs) such as the Sun Java Studio Creator IDE or the NetBeans IDE. If an Ajax-enabled component is available in an IDE, you can drag and drop the component from the IDE's palette onto the page. The JSP Tag Approach To use a custom component such as the autocompletion component in a JSP page, you add the following to the code for the JSP page:
Following is part of the code for the JSP page shown in Figure 2. The code snippet highlights (1) the reference to the custom component's tag library and (2) the tag for the component. You can view the entire JSP page here.
Click here for a full-size version of the code example In the code snippet,
As the page author, you need to get from the component developer the name, attributes, and attribute values of any custom component-related tags that will be used on the page. You might ask where the link is to the event handler that processes the user's entry into the city or state field. The answer is that it's rendered by the custom component, saving you the trouble of having to code it. The custom component renders an
Click here for a full-size version of the code example The JavaScript attributes, What's important here is that you don't have to provide any JavaScript code to get and display the pop-up list of suggested cities or states or to process a selection from the list. Neither do you need to provide CSS to specify the presentation style of the pop-up list. The custom component encapsulates the code required for those actions. The IDE Approach
If an Ajax-enabled component is available through an IDE, you can incorporate it into a page by dragging it from the IDE's palette and dropping it on the page. For example, the autocompletion component is available in the Sun Java Studio Creator IDE. To use the component in a web page, you open the page in Sun Java Studio Creator's Visual Designer. Then you simply drag the component from the Sun Java Studio Creator palette and drop it on an appropriate place in the page such as next to a Label component. The article "Using the Ajax Text Completion Component" describes how to create a number of different web applications using the autocompletion component in the Sun Java Studio Creator IDE. In one application, the autocompletion component is used in a way similar to the address form completion example in Figure 2. However, instead of completing the name of a city or state, the component completes a word, as Figure 3 shows. A dictionary web service that comes with the Sun Java Studio Creator IDE supplies the suggested words.
Assuming that the dictionary service is available in the IDE -- the article "Using the Ajax Text Completion Component" describes how to make the service available in the IDE -- all you need to do to build the page and include the word-completion functionality is the following:
As is the case for the autocompletion component shown in Figure 2, a method in a bean returns the contents for the pop-up list. In the example of the component in the Sun Java Studio Creator IDE, the bean calls the dictionary web service. The component developer should provide in the component the code that accesses the dictionary service. Alternatively, you might need to edit the code in the component, as appropriate. The article "Using the Ajax Text Completion Component" shows different examples that use the autocompletion component and shows the code to add or change in the component for each example. Using Ajax-Enabled Widgets
Another way to add Ajax functionality to a web page is to use widgets that are enabled for Ajax. A widget is a prebuilt UI component that you can plug into a web application and customize as needed. Sounds a lot like a JavaServer Faces technology component, right? In fact, the UI components in the JavaServer Faces component libraries are widgets, and they include widgets that are enabled for Ajax. However, there are ways other than JavaServer Faces technology to access and use Ajax-enabled widgets. One way you can get Ajax-enabled widgets is from JavaScript libraries such as those in the Dojo toolkit or script.aculo.us. To include a widget from a JavaScript library in a web page, you need to include some JavaScript code in the page. A Dojo Widget Let's look at an example of a widget from a JavaScript library. One of the widgets available in the Dojo toolkit is a combobox widget. The widget is similar in operation to the autocompletion components shown previously. The widget displays a drop-down menu of choices. As a user enters text in a text field, the choices are dynamically reduced to those that match the entered text. For example, Figure 6 shows a page in which the combobox widget is used to select a U.S. state. The example is taken from the demonstration of the combobox widget on the Dojo toolkit site.
Including the Dojo Combobox Widget in a Page The Dojo toolkit contains various JavaScript libraries, each of which is organized
in packages. One of the libraries contains the JavaScript functions needed for the Dojo widgets.
Each package in the library is for a specific widget. To include a Dojo widget on a page, you need to
(1) import the package for the widget using the JavaScript function
Click here for a full-size version of the code example The Notice the reference to the Note that the Dojo toolkit provides not only the JavaScript code for a widget but also the HTML and CSS for the presentation of the widget on a page. Using jMaki Widgets
One of the issues in using widgets from a JavaScript library is that it requires page authors to do some JavaScript coding. For example, to use the Dojo combobox widget, you need to write JavaScript code to call the jMaki, pronounced jay-MAH-kee, is a framework for wrapping JavaScript widgets in JSP custom tag handlers or JavaServer Face components. The name jMaki cleverly represents the concept of wrapping things within Java technologies. The j in jMaki stands for Java technology, and Maki is the Japanese word for wrap. The framework provides a library of wrapped widgets from various JavaScript libraries such as the Dojo toolkit and script.aculo.us. You can view these widgets in the jMaki Widget Gallery. The jMaki framework also provides a way for developers to add to the set of jMaki widgets. jMaki simplifies things for page authors. You can add a jMaki widget to a web page without doing any JavaScript coding. You include a jMaki widget in a web page in the same way as you include a JSP custom tag or JavaServer Faces component. If you use JSP technology or JavaServer Faces technology to develop web pages, jMaki allows you to access and use widgets in a familiar way. And because jMaki wraps widgets in JSP tag handlers or JavaServer Faces components, it extends the benefits of JSP and JavaServer Faces technology to the widgets. For example, a jMaki widget wrapped in a JavaServer Faces component can be customized to trigger event handlers and validate user entries. In addition, jMaki makes the JavaScript code, HTML, and CSS for a widget accessible so that you can easily customize it. Recently jMaki added support for the PHP scripting language and Project Phobos, a new scripting framework that enables a blending of scripting language and Java technology. In addition to accessing a jMaki widget as a JSP custom tag or JavaServer Faces component, you can now access it using PHP code, or use the jMaki library in Project Phobos. For further information see the article "New Technologies for Ajax and Web Application Development: Project Phobos". Also see the blog entry "jMaki supports PHP!" and the blog entry "jMaki in Phobos". A jMaki Autocompleter Widget
Let's look at an example of a jMaki widget. One of the widgets distributed with jMaki is a combobox widget. It's the wrapped version of the combobox widget provided in the Dojo toolkit. As Figure 7 shows, the jMaki combobox widget works exactly the same way as the Dojo combobox widget.
Including the jMaki Combobox Widget in a Page You can include a jMaki widget on a web page as either a JSP custom tag or a custom JavaServer Faces component. In either case, you reference the appropriate jMaki tag library. If you want to include a widget as a JSP custom tag, you specify the following jMaki tag library:
If you want to include a widget as a JavaServer Faces component, you specify the following jMaki tag library:
Then you specify the tag for the widget in the appropriate place on the page. For example, here's an example of the jMaki combobox widget used as a custom JSP tag.
Here's an example of the jMaki combobox widget used as a JavaServer Faces component.
The Whether a jMaki widget is included as a JSP custom tag or a custom JavaServer Faces component, you need to get from the application developer the name, attributes, and attribute values for the widget's JSP tag. You can also find the names of the widgets distributed with jMaki by looking at the source code for the widget in the jMaki Widget Gallery.
One other thing to note: A web application that uses jMaki widgets must include the contents of the jMaki distribution package. Further, the application must place some of the jMaki files in specific locations within the application directory structure. The application developer, not the page author, needs to take these actions. However, if you would like to learn more about what steps are required to set up an application for jMaki, see the article "New Technologies for Ajax and Web Application Development: Project jMaki". Using jMaki in the NetBeans IDE
jMaki widgets are also available in the NetBeans IDE through the jMaki NetBeans plug-in. If the plug-in is installed, you can incorporate one or more jMaki widgets in a web page by opening the source file for the page in the IDE, dragging the widgets from the jMaki palette, and then dropping them at the appropriate points in the source file. The appropriate tag library references are added to the source code for the page, and the custom tags for the widgets are added at the point of the drop. For example, Figure 8 shows what happens when you drag and drop the combobox widget from the jMaki palette in the NetBeans IDE to the source file of a web page.
Here the widget is dropped after the Writing the JavaScript Code Yourself
If you don't find components or widgets that provide the Ajax functionality you need, you'll need to do the JavaScript coding yourself. Let's look at an example. Writing the JavaScript Code for Autocompletion Imagine that no autocompletion component or autocompletion widget exists. Let's examine how you could use JavaScript code to add autocompletion functionality to a web page. In the following steps, you'll add JavaScript code to an HTML page. The JavaScript code is designed to interact with a servlet on the server to handle autocompletion. You can also find a description of this example in the document "Auto-Completion With Ajax: Design Details" in the Java Blueprints Solutions Catalog. What You Need to Code As you might remember from Figure 1, here's what you need to do in the web page to provide Ajax functionality:
Let's look at what you need to code for each of these steps. 1. Map an Event to a JavaScript Function In the HTML page, you first construct a text field and then map the entries in the text field to a JavaScript function that handles the entries.
The 2. Create and Configure an In the header of the HTML file, you add the JavaScript code for the
3. Make a Request to the Server Through the To the
After initializing the Table 1. Statements Used to Communicate with a Server Through an
XMLHttpRequest Object or XMLHTTP Object
4. Process the Result From the Server in a JavaScript Callback Function The request to the server is processed by a servlet. The servlet handles the request and returns the data for the
autocompletion, for example, a list of states that begin with the characters that the user entered. The data is returned
in the
Both 5. Update the Internal Representation of the Page With the New Data The JavaScript function
Click here for a full-size version of the code example The web page is represented internally as a tree structure through the Document Object Model (DOM).
The Including JavaScript Code From a File
Although the page author can do all the JavaScript coding to enable Ajax in a web page, it's more typical that the application
developer will do the JavaScript coding and put it in a separate file. In fact, it's best to cleanly separate the content of
a web application from the JavaScript code in an Ajax-enabled application.
If the JavaScript code is in a separate file, all you need to do is reference the file in a
In response, the JavaScript code in the file is included in the code for the page. If all the JavaScript code is in a separate file, the only other thing you need to do to add Ajax functionality to a web page is map the pertinent event to a JavaScript function as described in step 1. Specifying Presentation Style Ajax-enabled components and widgets provide the CSS that defines their presentation style. If you don't use Ajax-enabled components or widgets or if their presentation style is not what you want, you'll need to code the CSS yourself. For example, here is the CSS for the pop-up list in Figure 2 that is generated by the autocompletion component in the Java BluePrints Solutions Catalog:
If you want the pop-up list to display in the same presentation style as shown in Figure 2,
you need to either use the autocompletion custom component, use a widget that provides the same CSS, or code the
same CSS yourself. If you want the pop-up list to display in a different presentation style, you need to
code the CSS accordingly. You can provide the CSS in the web page or in a separate file. If the CSS is in a separate file,
you reference the file in a
Using JavaScript Libraries
One of the approaches mentioned earlier in this article uses widgets from a JavaScript library such
as those in the Dojo toolkit or script.aculo.us. However, these libraries provide more than widgets.
If you do the JavaScript coding to add Ajax functionality to a web page, you can take advantage of additional
features in JavaScript libraries to simplify your coding. For example, the
The
Other libraries in the Dojo toolkit provide a variety of functions that simplify the code for things such as DOM manipulation and event handling. Prototype is another popular source for JavaScript
functions that can simplify the Ajax-enabling code in a web page. Prototype is a JavaScript
framework that provides a library whose components include objects that simplify the use of JavaScript.
One component presents an Ajax object that, like the Script.aculo.us and Rico are built on top of Prototype. Both provide JavaScript libraries that support Ajax as well as other functions that can be plugged into a web application. You can take advantage of additional JavaScript libraries -- see Survey of Ajax/JavaScript Libraries for an extensive list. Summary
You can add Ajax functionality to a web page in many ways. In general, these approaches vary in the amount of JavaScript code you need to incorporate into the page. Some approaches, such as using Ajax-enabled JavaServer Faces components, encapsulate the JavaScript code in the component, so you don't have to do any JavaScript coding. Other approaches, such as using widgets from the Dojo toolkit, provide some of the JavaScript code. In still other approaches, you do most or all of the JavaScript coding. Choose the approach or combination of approaches that best fits the functional requirements of your web application and with which you're most comfortable. For More Information
About The Author
Ed Ort is a is a staff member of java.sun.com. He has written extensively about relational database technology, programming languages, and web services. |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||