Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
AJAX
AJAX: Asynchronous Java and XML
The aim is to increase the interactivity of a website When you work with a web page, the web page itself can be interacting with the server, but without reloading the (whole) page you are on The result is an uninterrupted user experience, with full server-side capabilities
JavaScript handles all the client-side functionality, such as capturing events XMLHttpRequest is used to submit an HTTP request to the server to exchange data
This occurs within the JavaScript of the page XMLHttpRequest was developed for Internet Explorer, and adopted by all other browsers
The page is not reloaded, but the JavaScript can use this method to send/receive data to/from the server
JDBC, etc.
JavaScript Module
HTTP
JSP Page
XMLHttpRequest
XMLHttpRequest can load HTTP requests:
Asynchronously: The browser will not wait, but the object will call an event handler when the loading status changes
This is what you would use in practice
XMLHttpRequest Methods
abort()
Opens a connection and retrieves response from the specified URL. Can also specify optional values method (GET/POST), username and password for secured sites
send(content)
XMLHttpRequest Properties
onreadystatechange
AJAX: Examples
This is a very simple idea, but it can have very nice results, such as:
AJAX: Practical
Ok, so lets create a real example Remember in HaggleCity, when selling an item, and we had to choose category and subcategory?
I put in JavaScript that posted the page back to the server, which generated the same form, except with the correct subcategories for the currently selected category The result was that the form reloaded every time I selected a category from the list
One obvious solution is to write the subcategory population code completely in JavaScript
However, if the subcategories change, I have to manually edit the code (which is not acceptable)
The selection of a list item still results in an HTTP request, and suffers the latency thereof
This latency necessitates the Please Wait message, to prevent the user from clicking the buttons/links or selecting from the lists a second time
A Second Example
Lets make another AJAX-enabled web page The form provides a TV show listing
When a TV show is clicked, the details of that show are displayed However, the page is not reloaded The TV show details are loaded using XMLHttpRequest, and inserted into an empty <div> tag when received
A Third Example
An AJAX-enabled Tic Tac Toe game is developed
enterPlayerMove.jsp
This page adds a move for X to the player at (x,y)
enterComputerMove.jsp
This page adds a move for O at a random open position
AJAX-Enabled Websites
Kiko (Calendar) - www.kiko.com Writely (Word Processor) www.writely.com A9 (Search Engine) www.a9.com A9 Maps (Maps) maps.a9.com Google Maps (Maps) maps.google.com Google Suggest (Search Helper) www.google.com Google Mail (E-Mail) gmail.google.com Meebo (Instant Messaging) www15.meebo.com Kayak (Flights Search) www.kayak.com
AJAX Libraries
Ajax Tags: JSP tags Apache Ajax: JSP tags Java Web Parts: JSP tags Atlas: API for .NET Ajax.NET: API for .NET Direct Web Remoting: Java Javascript & Javascript Java DoJo: JavaScript lib Backbase: Complete Framework, GUI + integration with popular architectures (e.g. J2EE)
AJAX Tags
The tags created for AJAX tags are basically a ready made taglib for AJAX apps:
autocomplete: A field is filled in with matching words (in a list generated on the server side), similar to Google Suggest callout: A popup (e.g. for definitions) updateField: One form field is updated when data is supplied to another form field select: A listbox is populated based on the value of another listbox (i.e. like the sellItem.jsp page)
AJAX Tags
The tags created for AJAX tags are basically a ready made taglib for AJAX apps: