AJAX is an acronym that stands for Asynchronous Javascript and XML. In fact, AJAX is not a new technology, because both Javascript and XML have existed for quite some time, and AJAX – a synthesis of these technologies. AJAX is most often associated with the term Web 2.0, and is presented as the latest Web application.
When using AJAX no need to update every time the entire page, because it is updated only part of it. This is much more convenient, since you do not have to wait long and economical, since not everyone has unlimited Internet. However, in this case, the developer must ensure that the user is aware of what is happening on the page. This can be realized with the help of indicators of loading, text messages about what is the exchange of data with the server. You should also understand that not all browsers support AJAX (older versions of browsers and text browsers). Plus Javascript can be disabled by the user. Therefore, you should not abuse the use of technology and resort to alternative methods of presenting information on the Web-site.
To summarize the advantages of AJAX:
Ability to create a convenient Web-interface
Active interaction with the user
Partial reloading of the page instead of complete
Ease of use
AJAX uses two methods to work with the Web page: changing the Web page without reloading it, and the dynamic appeal to the server. The second can be done in several ways, including XMLHttpRequest, which we’ll talk about, and using a hidden-frame technique.
The use of Ajax begins with a JavaScript object called XMLHttpRequest. As the name suggests, it allows the client side of your code to make HTTP requests and parses the XML server’s response. The first step in learning Ajax is to create the data of the XMLHttpRequest object. The HTTP protocol method when used for the request (GET or POST) and the paths are then set in the XMLHttpRequest object.
Now remember, why does the “a” in Ajax mean “asynchronous”? When you send an HTTP request, you don’t want the browser to hang around waiting for the server to respond. Instead, you also want to respond to the user’s action on the page and deal with the server’s response when it eventually arrives. To accomplish this, you need to register a callback function with XMLHttpRequest and send an asynchronously XMLHttpRequest request. The browser will remain in control, and the callback function will be called when the server responds.
On the Web server, the request will arrive just like any other HttpServletRequest. After analyzing the request parameters, the servlet will perform all the necessary actions for the application, serialize its request in XML format, and write it into the HttpServletResponse.
Back to the client, the callback function registered in XMLHttpRequest is now executed to process the XML document returned by the server. Finally, the UI is updated according to the server, using JavaScript code to transform the page.