Tuesday, July 5, 2011

How to get whole Jsp page using AJAX ?

It is so simple just follow below two steps [use netbeans IDE]
--------------------------------------------------------------
Use Netbeans IDE
-----------------

1) Create [ index.jsp ] page as in given image and copy below ajax contents in its script tag
---------------------------------------------------------------------------------------------





function getXMLObject() //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject();
function getHomeDotJspPage(){
if(xmlhttp) {
var userName=document.getElementById("name").value;
var params = "userName="+userName;
var url = "home.jsp"
xmlhttp.open("POST",url,true);
xmlhttp.onreadystatechange = handleServerResponce;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(params);
}
}

function handleServerResponce() {

if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
var data=xmlhttp.responseText;
document.getElementById("data").innerHTML=data;
}
}
}


2) Create home.jsp page same as below image
-------------------------------------------





3) Now Run your Project
-----------------------
Your comments are heartly invited.

Thank You
---------

No comments:

Post a Comment