Ajax is revolusioner method in building web application, it’s used javascript XMLHttpRequest object to communicate with server without have to reloading the page.
I am new to Ajax, this is my “hello world” application to show how using Ajax with Java.
(ajaxHello.jsp)
<html>
<head>
<title>Hello Ajax</title>
</head>
<body>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
//document.myForm.time.value=xmlHttp.responseText;
document.getElementById("timeInfo").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET","time.jsp",true);
xmlHttp.send(null);
}
</script>
<form name="aForm">
Name: <input type="text" name="username" onkeyup="ajaxFunction();" /> <br/>
Hi, you are typing the text at
<span id="timeInfo" STYLE="font-family: sans-serif; color: red; font-size: 12pt"></span>
</form>
</body>
</html>
(time.jsp)
<%= new java.text.SimpleDateFormat("dd MMM yyyy, hh:mm:ss").format( new java.util.Date() ) %>
This is the source, rename it to AjaxWithJavaHello.war and plug into your web server.
