Ajaxjavawebservices chapter

Solo disponible en BuenasTareas
  • Páginas : 16 (3983 palabras )
  • Descarga(s) : 0
  • Publicado : 31 de enero de 2012
Leer documento completo
Vista previa del texto
C H A P T E R

1 0

Ajax and Java Web Services
In this chapter, I examine how Java Web Services can be used to support Ajax clients. Ajax, or Asynchronous JavaScript and XML, is a programming technique that enables you to create user interfaces for a Web browser that behave more like a local, stand-alone application than a collection of HTML pages. Ajax is a good fit with Java Web Services.Using these two technologies together enables you to publish software components as services (via JAXWS) and create great browser-based user interfaces on top of them (via Ajax). The entire application can then be packaged as an EAR or WAR and deployed on a Java EE application server. To demonstrate this capability, I pick up here where I left off at the end of Chapter 9. In that chapter, I showedyou how to build an online shopping application, SOAShopper, which can search across multiple Web-service-enabled sites (i.e., eBay, Yahoo! Shopping, and Amazon). In this chapter, I show how you can develop an Ajax front-end to SOAShopper. In particular, the code examined in this chapter demonstrates how to write an Ajax application that consumes RESTful Java Web Services endpoints. In the secondhalf of this chapter, I review the JavaScript code that implements the SOAShopper Ajax front-end in quite a bit of detail. For those of you who are familiar with Web front-end coding and JavaScript, this detail may seem tedious. I include it because my assumption is that many readers of this book are server-side Java programmers who do not usually do a lot of JavaScript development and, therefore,might be interested in the detailed code explanation.

463

464

Ajax and Java Web Services

10.1

Quick Overview of Ajax
Ajax is a well-documented technology, and my purpose here is not to write a detailed tutorial on Ajax programming.1 However, I do want to go over some of the basics to set the stage for a discussion of the SOAShopper front-end and how it interacts with Java EE. Asmany of you know, the major benefit of Ajax is that it allows a browser-based application to avoid the need for full-page refreshes each time new data is retrieved from the server. Ajax programmers use the JavaScript type XMLHttpRequest to exchange data with the server behind the scenes (i.e., without having to reload the entire HTML page being displayed by the browser). When new data (usually inXML format) is received by an XMLHttpRequest instance, JavaScript is used to update the DOM structure of the HTML page (e.g., inserting some rows in a table) without rebuilding the entire HTML page in memory. To see what that means in practice, I walk you through some screen shots from the SOAShopper front-end. Then, in the rest of this chapter, I will show you how to write the code behind thesescreen shots. If you build and deploy the SOAShopper application on your local machine2 and point your browser to http://:/shoashopper/ajax/search.html, you should see something similar to what appears in Figure 10–1. This is the initial search screen for SOAShopper. The three labeled items in this figure are worth pointing out for discussion: 1. The URL where the application resides remains constantthroughout its use. The search is performed and results are displayed without loading a new page. This is implemented by using JavaScript that updates the DOM residing in the browser’s memory. 2. This search page offers you four search parameters: a set of keywords; a category to search; a low price; and a high price. These parameters correspond to the parameters supported by the SOAShopperofferSearch REST endpoint discussed in Chapter 9, Section 9.3 (see Figure 9–2). This search page contains JavaScript that converts these parameters into a query string that an XMLHttpRequest instance uses to invoke the offerSearch endpoint.

1. For a good introduction to Ajax, I recommend “Ajax in Action” [AIA]. 2. For instructions, see Appendix B, Section B.9.

10.1

Quick Overview of Ajax...
tracking img