Tuesday, May 20, 2008

AJAX simple example

Hi,
This is simple ajax example with jsp when key up in one textbox time will be displayed on another textbox

1.we need html page look follwing


<form name="myform">
Name:<input type="text" name="username" onkeyup="ajaxFunction()"/>
Time:<input type= "text" name="time"/> </form>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();//This is for other than IE browser creating
object.
}
catch(e)
{
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;
}
}
xmlHttp.open("GET","time.jsp",true);
xmlHttp.send(null);

}

</script>
2. time.jsp in same location



<%@page import="java.text.SimpleDateFormat%>
< %@page import="java.util.Calendar"%>

<%
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss a");
String time = sd.format(cal.getTime());
out.println(time);
%>

if we run this application thru server we will get the result

No comments:

Post a Comment

Contact Form

Name

Email *

Message *