SunQuest
 
       Java
  Home arrow Java arrow Page 3 - J2EE and AJAX: AJAX with Servlets
IBM developerWorks
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

J2EE and AJAX: AJAX with Servlets
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 73
    2006-07-12

    Table of Contents:
  • J2EE and AJAX: AJAX with Servlets
  • AJAX and Servlets: the Steps for Implementation
  • Steps for Implementation continued
  • AJAX and Servlets in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    J2EE and AJAX: AJAX with Servlets - Steps for Implementation continued


    (Page 3 of 4 )

    Registering and implementing the call-back handler

    There needs to be a handler that can be invoked when the response arrives. It is the callback handler. A callback handler is simply a function that is registered with the system and invoked by the system when the event for which it is registered occurs. So in this case the event is the arrival of data along with the response. To handle this event, a function has to be registered with the XMLHttpRequest which is done by the onreadystatechange property of the XMLHttpRequest. So to register a function named handleStateChange(), the statement would be:

    xmlHttp.onreadystatechange=handleStateChange;

    The next step is to implement the handler. The basic functionalities that a handler should implement are retrieving data from the XML response sent from the servlet and manipulating the current page on the browser according to the data received from the server. For example, to display the result coming from the server on the page, the code would be:

    function handleStateChange()
    {
        if(xmlHttp.readyState==4)
        {
            if(xmlHttp.status==200)
                {

        
    document.getElementById("results").innerHTML=xmlHttp.responseText;
                }
            else
            {
                alert("Error loading pagen"+ xmlHttp.status +":"+ xmlHttp.statusText);
            }
        }
    }

    Here the element having the id results is a <div> tag. So whatever may be the data coming from the server is displayed as child nodes of the results. The three properties that are being used here are readyState, status and responseText. The readyState specifies whether the data load operation is successful or not. Any value except 4 indicates that data is not yet available. Even then, if the data contains a "file not found" message, the next part wouldn’t work as required. Hence, it is always better to check the status.

    The data is usable only if the status is 200. The next step is to extract the data and use it. The XMLHttpRequest instance has two properties, responseText and responseXML to extract data from the response. If data is in the form of text, then responseTest can be used, otherwise responseXML can be used.

    The only piece of the puzzle left is the server-side part. That’s what is coming up.

    Generating the XML response

    Whatever may be the server-side technology, the implementation has to generate XML. In this case the technology is servlet. To generate the response, three things must be done: first, set the content type to text/xml; second, get a writer object; and third, set the XML into the response using the writer object. The code will be something like this:

    response.setContentType("text/xml");
    response.getWriter().write("<valid>true</valid>");

    where response is an object of HttpServletResponse. That’s it. In the next  section I will put it all together to develop a registration module that will check the availability of the user id.

    More Java Articles
    More By A.P.Rajshekhar


       · HiAjax is one of the hottest tech. in the current market. In this article I have...
       · Hi, Techie AjaxGood to see your simple and yet best explanation method of Ajax new...
       · HiThanks for your comments. Yes you can send HTML as response. However instead of...
       · Appreciate your quick reply,Could you pls, send me a sample code sinppet.Servlet...
       · hello Mr.Rajashekar gaaru,how ru?my name is bharath.Really u did experiments...
       · Hi Mr.Rajashekar,Your code is simply superb and easy to understand.But when i...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway