Wednesday, October 14, 2009

Simple Ajax Example




function init() {
target = document.getElementById("complete-field");
}

function initRequest() {
try { // Firefox, Opera 8.0+, Safari

req1 = new XMLHttpRequest();
} catch (e) { // Internet Explorer
try {
req1 = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
req1 = new ActiveXObject("Microsoft.XMLHTTP");
}
}
}

function doCompletion() {
init();
var url = "AjaxGetCompanyDetails.jsp?action=CreditCompanyReport&id=" + escape(target.value);//getting XML data
initRequest();
req1.onreadystatechange = processRequest;
try{
Ext.MessageBox.show({ title: 'Please wait...', msg: 'Loading......', width:240, progress:false, closable:false });
}catch(k){}
req1.open("GET", url, true);
req1.send(null);

}

function processRequest() {
if (req1.readyState == 4) {
if (req1.status == 200) {
parseMessages();
} else if (req1.status == 204){

}else{
}
}
}

function parseMessages() {
var companies = req1.responseXML.documentElement.getElementsByTagName("companies")[0];
for (loop = 0; loop < companies.childNodes.length; loop++) {
var company= companies.childNodes[loop];
var name = company.getElementsByTagName("name")[0];
var address = company.getElementsByTagName("address")[0];
var tele = company.getElementsByTagName("tele")[0];
var fax = company.getElementsByTagName("fax")[0];
addRowToTable(name.childNodes[0].nodeValue,address.childNodes[0].nodeValue,tele.childNodes[0].nodeValue,fax.childNodes[0].nodeValue);
}
Ext.MessageBox.hide();
}


======================= Data Generated JSP ==============================





<!-- //================================================================

//

// Document : AjaxGetCompanyDetails.jsp

//

// Create date : 11-10-2009

//

// Author : Gayan Dissanayake

//

//=====================================================================



-->

<%@ page import="java.util.HashMap" %>

<%@ page import="AjaxController" %>

<%@ page import="java.io.IOException" %>

<%@ page import="java.util.Iterator" %>

<%@ page import="java.io.PrintWriter" %>







<%!

public void jspInit() {

}



public void jspDestroy() {

}

%>

<%@ page contentType="text/xml" %>

<%

try {

String action = request.getParameter("action");

String targetId = request.getParameter("id");

String data[][] = (String[][]) AjaxController.getCompanyDetails(targetId);
//return 2D array

StringBuffer sb = new StringBuffer();

boolean namesAdded = false;

if ("CreditCompanyReport".equals(action)) {

for (int i = 0; i < data.length; i++) {

sb.append("<company>");

sb.append("<name>" + (String) data[i][0].replace('&', '-').replace('/', '-') +
"</name>");

sb.append("<address>" + (String) data[i][1].replace('&', '-').replace('/', '-')
+ "</address>");

sb.append("<tele>" + (String) data[i][2].replace('&', '-').replace('/', '-') +
"</tele>");

sb.append("<fax>" + (String) data[i][3].replace('&', '-').replace('/', '-') +
"</fax>");

sb.append("</company>");



namesAdded = true;

}

if (namesAdded) {

StringBuffer dataXml = new StringBuffer();

dataXml.append("<xml version=\"1.0\" encoding=\"UTF-8\" ><companies>");

dataXml.append(sb);

dataXml.append("</companies></xml>");



response.setContentType("text/xml");

response.setHeader("Cache-Control", "no-cache");

response.getWriter().write(dataXml.toString().trim());

data = null;

} else {

response.setStatus(HttpServletResponse.SC_NO_CONTENT);

}

}

} catch (Exception e) {

e.printStackTrace();

}

%>

 

No comments: