| AJAX Interview Questions
|
|
| 1. Are there any frameworks available to help speedup development with AJAX? |
| .
There are several browser-side frameworks available, each with their own uniqueness…
|
| 2. Is Adaptive Path selling Ajax components or trademarking the name? Where can I download it?
|
|
Ajax isn’t something you can download. It’s an approach — a way of thinking about the architecture of web applications using certain
technologies. Neither the Ajax name nor the approach are proprietary to Adaptive Path.
|
| 3. Should I use an HTTP GET or POST for my AJAX calls? |
|
AJAX requests should use an HTTP GET request when retrieving data where the data will not change for a given request URL.
An HTTP POST should be used when state is updated on the server. This is in line with HTTP idempotency recommendations and
is highly recommended for a consistent web application architecture.
|
| 4. How do I debug JavaScript?
|
There are not that many tools out there that will support both client-side and server-side debugging.
I am certain this will change as AJAX applications proliferate. I currently do my client-side and
server-side debugging separately. Below is some information on the client-side debuggers on some of the commonly used browsers.
» Firefox/Mozilla/Netscape - Have a built in debugger Venkman which can be helpful but there is
a Firefox add on known as FireBug which provides all the information and AJAX developer would ever need including the ability
to inspect the browser DOM, console access to the JavaScript runtime in the browser, and the ability to see the HTTP requests
and responses (including those made by an XMLHttpRequest). I tend to develop my applications initially on Firefox using Firebug
then venture out to the other browsers.
» Safari - Has a debugger which needs to be enabled. See the Safari FAQ for details.
» Internet Explorer - There is MSDN Documentation on debugging JavaScript. A developer toolbar
for Internet Explorer may also be helpful.
While debuggers help a common technique knowing as “Alert Debugging” may be used.
In this case you place “alert()” function calls inline much like you would a System.out.println. While a little
primitive it works for most basic cases. Some frameworks such as Dojo provide APIs for tracking debug statements.
|
| 5. How do I provide internationalized AJAX interactions? |
Just because you are using XML does not mean you can properly send and receive localized content using AJAX requests.
To provide internationalized AJAX components you need to do the following:
» Set the charset of the page to an encoding that is supported by your target languages.
I tend to use UTF-8 because it covers the most languages. The following meta declaration in a HTML/JSP page will set the content type:
» In the page JavaScript make sure to encode any parameters sent to the server. JavaScript
provides the escape() function which returns Unicode escape strings in which localized text will appear in hexadecimal format.
For more details on JavaScript encoding see Comparing escape(), encodeURI(), and encodeURIComponent().
» On the server-side component set the character encoding using the
HttpServletRequest.setCharacterEncoding() method. Before you access the localized parameter using the HttpServletRequest.getParameter() call.
In the case of UTF this would be request.setCharactherEncoding(”UTF-8?);.
A server-side component returning AJAX responses needs to set the encoding of
the response to the same encoding used in the page.
response.setContentType(”text/xml;charset=;UTF-8?);
response.getWriter().write(” invalid “);
For more information on using AJAX with Java Enterprise Edition technologies see AJAX and Internationalization and for developing multi-lingual applications see Developing Multilingual Web Applications Using JavaServer Pages Technology.
|
| 6. Some of the Google examples you cite don’t use XML at all. Do I have to use XML and/or XSLT in an Ajax application? |
|
No. XML is the most fully-developed means of getting data in and out of an Ajax client, but there’s no
reason you couldn’t accomplish the same effects using a technology like JavaScript Object Notation or any
similar means of structuring data for interchange.
|
| 7. Are Ajax applications easier to develop than traditional web applications? |
|
Not necessarily. Ajax applications inevitably involve running complex JavaScript code on the client.
Making that complex code efficient and bug-free is not a task to be taken lightly, and better development
tools and frameworks will be needed to help us meet that challenge.
|
| 8. When do I use a synchronous versus a asynchronous request? |
|
Good question. They don’t call it AJAX for nothing! A synchronous request would block in page event processing and
I don’t see many use cases where a synchronous request is preferable.
|
| 9. How do I handle concurrent AJAX requests? |
With JavaScript you can have more than one AJAX request processing at a single time. In order to insure the proper post
processing of code it is recommended that you use JavaScript Closures. The example below shows an XMLHttpRequest object
abstracted by a JavaScript object called AJAXInteraction. As arguments you pass in the URL to call and the function to
call when the processing is done.
function AJAXInteraction(url, callback) {
var req = init();
req.onreadystatechange = processRequest;
function init() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject(”Microsoft.XMLHTTP”);
}
}
function processRequest () {
if (req.readyState == 4) {
if (req.status == 200) {
if (callback) callback(req.responseXML);
}
}
}
this.doGet = function() {
req.open(”GET”, url, true);
req.send(null);
}
this.doPost = function(body) {
req.open(”POST”, url, true);
req.setRequestHeader(”Content-Type”, ”
application/x-www-form-urlencoded”);
req.send(body);
}
}
function makeRequest() {
var ai = new AJAXInteraction(”processme”,
function() { alert(”Doing Post Process”);});
ai.doGet();
}
The function makeRequest() in the example above creates an AJAXInteraction with a URL to of “processme” and an
inline function that will show an alert dialog with the message “Doing Post Process”. When ai.doGet() is called
the AJAX interaction is initiated and when server-side component mapped to the URL “processme” returns a document
which is passed to the callback function that was specified when the AJAXInteraction was created.
Using this closures insures that the proper callback function associated with a specific AJAX interaction is called.
Caution should still be taken when creating multiple closure objects in that make XmlHttpRequests as to there is a
limited number of sockets that are used to make requests at any given time. Because there are limited number of
requests that can be made concurrently. Internet Explorer for example only allows for two concurrent AJAX requests
at any given time. Other browsers may allow more but it is generally between three and five requests. You may
choose to use pool of AJAXInteraction objects.
One thing to note when making multiple AJAX calls from the client is that the calls are not guaranteed to
return in any given order. Having closures within the callback of a closure object can be used to ensure
dependencies are processed correctly.
|
| 10. How do I send an image using AJAX?
|
While it may appear that images are being sent when using AJAX with an application like Google Maps what
is really happening is that the URLs of images are being send as the response of an AJAX request and
those URLs are being set using DHTML.
In this example an XML document is returned from an AJAX interaction and the category bar is populated.
<categories>
<ategory>
<cat-id>1</cat-id>
<name>Books</name>
<description>Fun to read</description>
<image-url>books_icon.gif</image-url>
</category>
<category>
<cat-id>2</cat-id>
<name>Electronics</name>
<description>Must have gadgets</description>
<image-url>electronics.gif</image-url>
</category>
</categories>
Notice that the image-url element contains the location of the URL for the image representing a category.
The callback method of an AJAX interaction will parse the response XML document and call the addCategory
function for each category included in the response XML document. The addCategory function looks up a table
row element "categoryTable" in body of the page and adds a row to the element which contains the image.
<scrip type="text/javascript" >
...
function addCategory(id, name, imageSrc) {
var categoryTable = document.getElementById("categoryTable");
var row = document.createElement("tr");
var catCell = document.createElement("td");
var img = document.createElement("img");
img.src = ("images\\" + imageSrc);
var link = document.createElement("a");
link.className ="category";
link.appendChild(document.createTextNode(name));
link.setAttribute("onclick", "catalog?command=category&catid=" + id);
catCell.appendChild(img);
catCell.appendChild(link);
row.appendChild(catCell);
categoryTable.appendChild(row);
}
</script>
...
<table>
<tr>
<td width="300" bgoclor="lightGray">
<table id="categoryTable" border="0" cellpadding="0"></table>
</td>
<td id="body" width="100%">Body Here</td>
</tr>
</table>
Note that the source of the image is set to the image source. The image is loaded by a subsequent HTTP request for
the image at the URL "images/books_icon.gif" or "images/electronic_icon.gif" that occurs when the img element
is added to the categoryTable.
|
| 11. Will HTML_AJAX integrate with other Javascript AJAX libraries such as scriptaculous ? How would this integration look like? |
HTML_AJAX doesn't have specific plans to integrate with other JavaScript libraries. Part of this is because external
dependencies make for a more complicated installation process. It might make sense to offer some optional dependencies
on a library like scriptaculous automatically using its visual effects for the loading box or something, but there
isn't a lot to gain from making default visuals like that flashier since they are designed to be easily replaceable
.
Most integration would take place in higher level components. Its unclear whether higher level components like that should
be part of HTML_AJAX delivered through PEAR or if they should just be supported by HTML_AJAX and made available from
http://htmlajax.org or some other site. If your interested in building widgets or components based on HTML_AJAX please let me know.
.
HTML_AJAX does however offer the ability to use its library loading mechanism with any JavaScript library. I use scriptaculous
in conjunction with HTML_AJAX and I load both libraries through the server.
.
To do this you just need to register the library with your server and load add its flag to your include line.
<?php
$this->server->registerJSLibrary('scriptaculous', array('prototype.js','scriptaculous.js','builder.js','effects.js','dragdrop.js','controls.js','slider.js'),
'/pathto/scriptaculous/');?>
<script type="text/javascrpt" src="server.php?client=scriptaculous"></script>
|
| 12. When should I use an Java applet instead of AJAX?
|
|
Applets provide a rich experience on the client side and there are many things they can do that an AJAX application cannot do,
such as custom data streaming, graphic manipulation, threading, and advanced GUIs. While DHTML with the use of AJAX has been
able to push the boundaries on what you can do on the client, there are some things that it just cannot do. The reason AJAX
is so popular is that it only requires functionality built into the browser (namely DHTML and AJAX capabilities). The user
does not need to download and/or configure plugins. It is easy to incrementally update functionality and know that that
functionality will readily available, and there are not any complicated deployment issues. That said, AJAX-based functionality
does need to take browser differences into consideration. This is why we recommend using a JavaScript library such as Dojo
which abstracts browser differences. So the "bottom line" is: If you are creating advanced UIs where you need more advanced
features on the client where you want UI accuracy down to the pixel, to do complex computations on the client, use specialized
networking techniques, and where you know that the applet plugin is available for your target audience, applets are the way
to go. AJAX/DHTML works well for applications where you know the users are using the latest generation of browsers, where
DHTML/AJAX "good enough" for you, and where your developers have JavaScript/DHTML/AJAX skills. Many amazing things can
be done with AJAX/DHTML but there are limitations. AJAX and applets can be used together in the same UIs with AJAX
providing the basic structure and applets providing more advanced functionality. The Java can communicate to JavaScript
using the Live-Connect APIs. The question should not be should framed as do I use AJAX or applets, but rather which
technology makes the best sense for what you are doing. AJAX and applets do not have to be mutually exclusive.
|
| 13. What kinds of applications is Ajax best suited for?
|
|
We don?t know yet. Because this is a relatively new approach, our understanding of where Ajax can best be applied is
still in its infancy. Sometimes the traditional web application model is the most appropriate solution to a problem.
|
| 14. Does this mean Adaptive Path is anti-Flash? |
|
Not at all. Macromedia is an Adaptive Path client, and we?ve long been supporters of Flash technology.
As Ajax matures, we expect that sometimes Ajax will be the better solution to a particular problem,
and sometimes Flash will be the better solution. We?re also interested in exploring ways the
technologies can be mixed (as in the case of Flickr, which uses both).
|
| 15. Where can I find examples of AJAX?
|
|
While components of AJAX have been around for some time (for instance, 1999 for XMLHttpRequest),
it really didn't become that popular until Google took...
|
| 16.What is the XMLHttpRequest object?
|
|
It offers a non-blocking way for JavaScript to communicate back to the web server to update only part of the web page.
|
| 17.Does Ajax have significant accessibility or browser compatibility limitations? Do Ajax applications break the back button? Is Ajax compatible with REST? Are there security considerations with Ajax development? Can Ajax applications be made to work for users who have JavaScript turned off?
|
|
The answer to all of these questions is ?maybe?. Many developers are already working on ways to address these concerns.
We think there?s more work to be done to determine all the limitations of Ajax, and we expect the Ajax development
community to uncover more issues like these along the way.
|
| 18. How do I access data from other domains to create a mashup with Java? |
|
From your JavaScript clients you can access data in other domains if the return data is provide in JSON format. In essence
you can create a JavaScript client that runs operates using data from a different server. This technique is know as JSON
with Padding or JSONP. There are questions as to whether this method is secure as you are retrieving data from outside
your domain and allowing it to be excuted in the context of your domain. Not all data from third parties is accessible
as JSON and in some cases you may want an extra level of protection. With Java you can provide a proxy to third party
services using a web component such as a servlet. This proxy can manage the communication with a third party service
and provide the data to your clients in a format of your choosing. You can also cache data at your proxy and reduce
trips to service. For more on using a Java proxy to create mashups see The XmlHttpProxy Client for Java.
|
| 19. Does Java have support for Comet style server-side push?
|
|
Current AJAX applications use polling to communicate changes data between the server and client. Some applications,
such as chat applications, stock tickers, or score boards require more immediate notifications of updates to the
client. Comet is an event based low latency server side push for AJAX applications. Comet communication keeps
one of the two connections available to the browser open to continously communicate events from the server to
the client. A Java based solution for Comet is being developed for Glassfish on top of the Grizzly HTTP connector.
See Enabling Grizzly by Jean-Francois Arcand for more details.
|
| 20. How do I create a thread to do AJAX polling?
|
JavaScript does not have threads. JavaScript functions are called when an event happens in a page such as the page is loaded,
a mouse click, or a form element gains focus. You can create a timer using the setTimeout which takes a function name and
time in milliseconds as arguments. You can then loop by calling the same function as can be seen in the JavaScript example below.
function checkForMessage() {
// start AJAX interaction with processCallback as the callback function
}
// callback for the request
function processCallback() {
// do post processing
setTimeout("checkForMessage()", 10000);
}
Notice that the checkForMessage will continue to loop indefinitely. You may want to vary the increment the interval
based on activity in the page or your use cases. You may also choose to have logic that would break out of the loop
based on some AJAX response processing condition.
|
| 21. Is the XMLHttpRequest object part of a W3C standard? |
|
No. Or not yet. It is part of the DOM Level 3 Load and Save Specification proposal.
|
| 22. What's AJAX?
|
AJAX (Asynchronous JavaScript and XML) is a newly coined term for two powerful browser features that have been around
for years, but were overlooked by many web developers until recently when applications such as Gmail, Google Suggest,
and Google Maps hit the streets.
Asynchronous JavaScript and XML, or Ajax (pronounced "Aye-Jacks"), is a web development technique for creating interactive
web applications using a combination of XHTML (or HTML) and CSS for marking up and styling information. (XML is commonly
used, although any format will work, including preformatted HTML, plain text, JSON and even EBML).
The Document Object Model manipulated through JavaScript to dynamically display and interact with the information presented
The XMLHttpRequest object to exchange data asynchronously with the web server. In some Ajax frameworks and in some situations,
an IFrame object is used instead of the XMLHttpRequest object to exchange data with the web server.
Like DHTML, LAMP, or SPA, Ajax is not a technology in itself, but a term that refers to the use of a group of technologies
together. In fact, derivative/composite technologies based substantially upon Ajax, such as AFLAX, are already appearing.
Ajax applications are mostly executed on the user's computer; they can perform a number of tasks without their performance
being limited by the network. This permits the development of interactive applications, in particular reactive and rich
graphic user interfaces.
Ajax applications target a well-documented platform, implemented by all major browsers on most existing platforms.
While it is uncertain that this compatibility will resist the advent of the next generations of browsers (in particular, Firefox),
at the moment, Ajax applications are effectively cross-platform.
While the Ajax platform is more restricted than the Java platform, current Ajax applications effectively fill part of
the one-time niche of Java applets: extending the browser with portable, lightweight mini-applications.
Ajax isn’t a technology. It’s really several technologies, each flourishing in its own right,
coming together in powerful new ways. Ajax incorporates:
* standards-based presentation using XHTML and CSS;
* dynamic display and interaction using the Document Object Model;
* data interchange and manipulation using XML and XSLT;
* asynchronous data retrieval using XMLHttpRequest;
* and JavaScript binding everything together.
|
| 23. Who’s Using Ajax ? |
Google is making a huge investment in developing the Ajax approach. All of the major products Google has introduced
over the last year — Orkut, Gmail, the latest beta version of Google Groups, Google Suggest, and Google Maps — are
Ajax applications. (For more on the technical nuts and bolts of these Ajax implementations, check out these excellent
analyses of Gmail, Google Suggest, and Google Maps.) Others are following suit: many of the features that people
love in Flickr depend on Ajax, and Amazon’s A9.com search engine applies similar techniques.
These projects demonstrate that Ajax is not only technically sound, but also
practical for real-world applications. This isn’t another technology that only works in a laboratory.
And Ajax applications can be any size, from the very simple, single-function Google Suggest to the
very complex and sophisticated Google Maps.
|
| 24. Should I consider AJAX? |
AJAX definitely has the buzz right now, but it might not be the right thing for you. AJAX is limited to the latest browsers,
exposes browser compatibility issues, and requires new skill-sets for many. There is a good blog entry by Alex Bosworth
on AJAX Mistakes which is a good read before you jump full force into AJAX.
On the other hand you can achieve highly interactive rich web applications that are
responsive and appear really fast. While it is debatable as to whether an AJAX based application is really faster,
the user feels a sense of immediacy because they are given active feedback while data is exchanged in the background.
If you are an early adopter and can handle the browser compatibility issues, and are willing to learn some more skills,
then AJAX is for you. It may be prudent to start off AJAX-ifying a small portion or component of your application first.
We all love technology, but just remember the purpose of AJAX is to enhance your user's experience and not hinder it.
|
| 25. Does AJAX work with Java?
|
|
Absolutely. Java is a great fit for AJAX! You can use Java Enterprise Edition servers to generate AJAX client pages and to
serve incoming AJAX requests, manage server side state for AJAX clients, and connect AJAX clients to your enterprise resources.
The JavaServer Faces component model is a great fit for defining and using AJAX components.
|
| 26. Won't my server-side framework provide me with AJAX? |
You may be benefiting from AJAX already. Many existing Java based frameworks already have some level of AJAX interactions and
new frameworks and component libraries are being developed to provide better AJAX support. I won't list all the Java frameworks
that use AJAX here, out of fear of missing someone, but you can find a good list at www.ajaxpatterns.org/Java_Ajax_Frameworks.
If you have not chosen a framework yet it is recommended you consider using JavaServer Faces
or a JavaServer Faces based framework. JavaServer Faces components can be created and used to abstract many of the details
of generating JavaScript, AJAX interactions, and DHTML processing and thus enable simple AJAX used by JSF application developer
and as plug-ins in JSF compatible IDE's, such as Sun Java Studio Creator.
|
| 27. Where should I start? |
Assuming the framework you are using does not suffice your use cases and you would like to develop your own AJAX components
or functionality I suggest you start with the article Asynchronous JavaScript Technology and XML (AJAX) With Java 2 Platform,
Enterprise Edition
If you would like to see a very basic example that includes source code you
can check out the tech tip Using AJAX with Java Technology. For a more complete list of AJAX resources
the Blueprints AJAX home page.
Next, I would recommend spending some time investigating AJAX libraries and frameworks. If you choose
to write your own AJAX clients-side script you are much better off not re-inventing the wheel.
AJAX in Action by Dave Crane and Eric Pascarello with Darren James is good resource. This book is
helpful for the Java developer in that in contains an appendix for learning JavaScript for the Java developer.
|
|
Back to Top |