Friday, January 04, 2008

AJAX for JSF

Simple JSF AJAX requests can be achieved within JSF (JSP) as follows:

  1. JavaScript - make the request to get data from the AJAX Servlet
  2. Ajax Servlet - process the request and send the XML/JSON/plain text response
  3. JavaScript - process the response and use the data on the page

For example:

    <script type="text/javascript" language="Javascript">
// get the ajax request object (from Ajax.js)
var http = getAjaxRequestObject();
// connect to the server (URL to the AjaxServlet)
http.open('GET', '/myjsfapp/faces/AjaxSevlet?param1="foo"&param2="bar"');
// assign a handler for the response
http.onreadystatechange = processResponse;
// send the request to the server
http.send(null);

function processResponse() {
// if the response has been received from the server
if (http.readyState == 4) {
// read the response from the server
var response = http.responseText;
// process the response
// e.g. parse the XML, JSON etc.
...
}
}
</script>

Ajax.js
AjaxServlet.java
XmlUtils.java

No comments: