Ajax with HTML

In Eclipse -- Create a Dynamic Web Proj-- Create index.html (Paste the below Code in it)

<html>
<body>

<script type="text/javascript">
var xmlHttp = null;
function ajaxFunction()
{


// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();

// Internet Explorer

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");



xmlHttp.onreadystatechange=abc;
var myname=document.all.username.value;
xmlHttp.open("GET","time.jsp?name="+myname,true);
xmlHttp.send(null);
}
function abc()
{
if(xmlHttp.readyState==4)
{
if (xmlHttp.responseText=="1")
alert("Enter name is correct");
else
alert("Wrong name");
//alert(xmlHttp.responseText);

//document.myForm.time.value=xmlHttp.responseText;
}
}
</script>

<form name="myForm">
Name: <input type="text"
onblur="ajaxFunction();" name="username" />
Time: <input type="text" name="time" />
</form>

</body>
</html>

Create time.jsp (U can put any server side logic or validation here.. paste the following code there)

<% String myname=request.getParameter("name"); System.out.println("MY CONSOLE : NAME="+myname); if(myname.equals("XXXX")) { response.getWriter().write("1"); } else { response.getWriter().write("2"); } %>


Now u can Run Ur index.jsp on any Web enabled Server Say (JBOSS)

1 comments:

Sagar said...

Can u help me with AJAX and Portlets