Navigation Bar

Sunday, December 27, 2009

Using AJAX in JSP

 AJAX, which stands for Asynchronous Javascript and XML, is a web development tool used on the client side to create interactive web applications, also known as Rich Internet Applications (RIAs). With AJAX, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page. The use of AJAX has led to an increase in interactive animation on web pages and better and faster display of data due to asynchronous mode, wherein the entire page data does not get loaded again and only the impacted tags of data get loaded. Data is retrieved using XMLHttpRequest object, Despite the name (AJAX), the use of Javascript and XML is not actually required, nor do requests need to be asynchronous. 

In many cases, related web pages have the same content and even in the case of the same page, if a request on that page is made multiple times, then each time all the contents of the page need to be re loaded. However, using AJAX, the web page can request only the content which needs to be refreshed or updated, thus drastically reducing network bandwidth usage and load time. These asynchronous requests allow the clients web browser UI to be more interactive and respond more quickly to user inputs. With AJAX sections of the web page can also be reloaded individually. Thus the application becomes faster and more responsive, since scripts and stylesheets dont have to be requested or loaded again and again. State can be maintained throughout the web site. Javascript variables will persist because the main container page need not be reloaded.

AJAX is based on the following web standards

  • HTML
  • CSS
  • Javascript
  • XML

In AJAX, the JavaScript uses the XMLHttpRequest object to communicate with the server. With this object the webpage can make a request to and get a response from a web server without reloading the page. The user will not even notice that a request has been made to and data has been retrieved from the server.

The below diagram illustrates the process flow of communication that takes place between client and server during an AJAX call.




Older versions of Internet Explorer (IE5 and IE6) use an ActiveX object instead of the XMLHttpRequest object. So before using XMLHttpRequest in old browsers we have to check if the browser supports the XMLHttpRequest object, or else we have to create an ActiveX object.

varable = new ActiveXObject("Microsoft.XMLHTTP");
The XMLHttpRequest has an onreadystatechange function, which is a callback function that gets executed everytime the readyState property of the request changes.

What is Does?
The function's primary purpose is to monitor the progress and status of the asynchronous HTTP request that has been sent to the server. This functions typically checks two properties to determins if the request is complete and successful.
readyState : indicates the current state of the request. A value of 4 means the request processing is DONE.
status : is the HTTP status code returned by the server. A value of 200 means the request was successfully processed. 
Once both these conditions are met, the function processes the response, makes it in the desired format and then, will usually update the content of the webpage with the data received from the server.   

When does it get called?
The onreadystatechange function is called every time the readyState changes. The readyState property progresses through five possible values in order: 
0 (UNSENT): The open() method has not yet been called.
1 (OPENED): The open() method has been called, but the send() method has not.
2 (HEADERS_RECEIVED): The send() method has been called, and the headers and status are now available.
3 (LOADING): The server is downloading the response body; responseText holds partial data.
4 (DONE): The operation is complete, and the full response has arrived from the server  

References

God's Word for the day
True and False Wisdom
There is a rebuke that is untimely
  And there is a person who is wise enough to keep silent
How much better it is to rebuke than to fume
  And the one who admits his fault will be kept from failure
Like a eucuch lusting to violate a girl
  is the persson who does right under compulsion
Some people keep silent and are thought to be wise
  while others are detested for being talkative
Some people keep silent because they have nothing to say
  while others keep silent because they know when to speak
Sirach 20:1-6

Gospel teachings of Jesus 
Jesus thanks his Father
Come to me all you that are weary and are carrying heavy burdens
  And I will give you rest. Take my yoke upon you And learn from me
For I am gentle and humble in heart and you will find rest for your souls
  For my yoke is easy and my burden is light
Mathew 11:28-29

No comments:

Post a Comment