pTutorial

New way to learn Programming

AJAX tutorial with JSP

Ajax is a programming language independent concept so you can create dynamic pages with Ajax using any server side programming language like JSP (Java server page), java servlet, PHP etc. Here we are using JSP for generating the server-side code.

This section covers the basic and advance example with JSP like load another JSP file, Ajax database example and many more.

Following example simply print the user name without reloading the whole web page.

Ajax Example with JSP

<!--index.html-->

<html>
<head>
<script>
var request;
function table()
{
var u=document.frm.num.value;
var url="argument.jsp?val="+u;
if(window.XMLHttpRequest){
request=new XMLHttpRequest();
}
else if(window.ActiveXObject){
request=new ActiveXObject("Microsoft.XMLHTTP");
}
request.onreadystatechange=getInfo;
request.open("GET",url,true);
request.send();
}
function getInfo(){
if(request.readyState==4){
var val=request.responseText;
document.getElementById('uk').innerHTML=val;
}
}

</script>
</head>
<body>
    <h2>Please Enter your anything to check response</h2>
<form name="frm">
<input type="text" name="num" onKeyUp="table()">
</form>

<span id="uk"> </span>

</body>
</html>

JSP FILE

Simple JSP file to get parameter and print.

<%-- argument.jsp --%>
<% 
String name=request.getParameter("val");  
out.print(name);  

%>

See Also


QUOTES OF THE DAY

The ballot is stronger than bullet.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER