This is the second in a series of articles that describe ways to introduce Ajax functionality into an existing web application developed with the NetBeans IDE. Readers are encouraged to read the first article in the series, Creating an Ajax-Enabled Application, a Do-It-Yourself Approach, before reading this one. At a minimum, install the example project, the NetBeans 5.5 IDE, and the GlassFish server, as described in Downloading and Installing the Tools.
Your first approach to introducing Ajax functionality into the Duke's
Bookstore application was do-it-yourself: you hand-coded JavaScript and
CSS
files on the client side and wrote a servlet to service the Ajax
The do-it-yourself approach works well enough for simple projects, but as your project grows you want to find an approach that requires less client-side coding. For your second attempt to implement pop-up balloons in your application, you use one of the available JavaScript libraries to minimize the amount of JavaScript code you need to write. In this article, you use the JavaScript libraries available with the Dojo Toolkit. The Dojo Toolkit provides a comprehensive set of JavaScript libraries and is receiving industry support from companies such as Sun Microsystems, AOL, and IBM. The Dojo Tookit is the logical choice for your project. Note that adopting Dojo libraries doesn't preclude other solutions. If at some point the Dojo libraries don't provide required functionality, they can be combined with libraries from other sources. One advantage of using Dojo libraries is that they are designed to work with various browsers when desired functionality is missing. As a minimum, JavaScript must be enabled on the client browser in order for Ajax techniques to work. However, if JavaScript has been disabled entirely on the browser, the Dojo libraries do not prevent the page from loading. In the case of your bookstore application, there is no real penalty to the user if a missing browser technology prevents the pop-up balloons from appearing, so the degraded case is easy to handle. For more feature-packed Ajax pages, degradable code is more important.
Contents
Downloading the Dojo Toolkit Library
To download and install the Dojo Toolkit library:
You are now ready to see how to use the Dojo library in a web application. Adding Dojo Libraries to an Application
To use the Dojo libraries with a web application, two sections of code are required:
The next section describes how to use these statements in your project. Incorporating the Dojo Library Into Your Project
To incorporate the Dojo libraries into the project you developed in Creating an Ajax-Enabled Application, a Do-It-Yourself Approach, you need to edit or replace the following two files:
where project is the root location of your project directory. Pre-edited versions of the files have already been placed in the project for you, to be incorporated as described in the next section.
You also need to insert the The
dojo.js File
The
You can sometimes speed up your application's response time by
customizing the
The
project The
popup_dojo.js File
To use the Dojo library in your project, you need to edit the existing
Opening the Bookstore Project in the NetBeans IDE
Replacing the bookcatalog.jsp File
You need to edit the
Running the Bookstore Project
You can now build and deploy the bookstore2 project. In the NetBeans Projects window, the bookstore2 project should be shown in bold type to indicate that it is the main project. If it is not, set it as the main project by choosing Files > Set Main Project > bookstore2.
The status of the build is shown in the NetBeans Output window. When the build finishes successfully, your browser opens to show the Duke's Bookstore application. The application behaves just as it did when you implemented pop-up balloons in the do-it-yourself approach. Examining the Dojo Files
You now examine the files that make the pop-up balloons work with the Dojo library. The
bookcatalog.jsp File
To examine the
The code of interest in
and
Lines 29–30, shown below, import two scripts:
The remainder of the file is identical to the The
popup_dojo.js File
The
The first line in the file makes sure that the
This package is, in fact, the only one used in the project.
One advantage in the Dojo version of the pop-up JSP file is that the
Dojo
libraries provide the necessary initilization request for the Ajax
As discussed in the Creating an
Ajax-Enabled Application, a Do-It-Yourself Approach, this function
can be easily changed to direct the request to either a servlet or
a JSP page on the server side by changing the value of the
The following table compares the
The code illustrates one advantage of the Dojo libraries: they handle
most browser differences for you, in this case eliminating the need for
an
The Ajax callback function, In line 53, the function signature defines the type, data, and event arguments:
whereas, in the
The function in the
In contrast, in the
Overall, the Summary
For the very simple Ajax functionality you are implementing in this example project, the benefits of a robust library aren't readily apparent, especially if the amount of hand coding required is the primary consideration. As you expand your application's functionality, however, you can take advantage of additional library modules. In that case, there can be a clear advantage to using JavaScript libraries such as those provided by the Dojo Toolkit. For example, if you want to send form data through an Ajax call, the amount of hand coding can be very large in the do-it-yourself approach. In contrast, to introduce a form with Dojo, you need only create a form node in your bind argument. One line of code can provide the necessary functionality. Even in the simplest of applications, a JavaScript library such as one provided by the Dojo Toolkit can provide some advantages. Notably,
Next Steps
In your next implementation, you create a custom JSF component to generate the Javascript that executes the Ajax interactions on the client side. The JSF component is packaged with the application bundle and accessed directly. A Java Servlet on the server side responds to the Ajax request. References
Developer Services and Training
|
| ||||||||||||||||||||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||