CodesJava

Easy learning with example program codes

ajax jsp example


Ajax refers to asynchronous java script and xml. It is not a not a programming language. Ajax is of Asynchronous type. It normally uses XML, plain text or JSON to communicate with server i.e. for data transfer.

Note: Ajax is technology independent.

Example

index.html

<html>
<head>
<script type="text/javascript">  
var xmlHttpRequest;  
function sendInfo() {   
var num=document.testform.numText.value;  
var url="index.jsp?val="+num;   
if(window.XMLHttpRequest){  
	xmlHttpRequest=new XMLHttpRequest();  
} else if(window.ActiveXObject){  
	xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");  
}  
 
try {  
	xmlHttpRequest.onreadystatechange=getInfo;  
	xmlHttpRequest.open("GET",url,true);  
	xmlHttpRequest.send();  
} catch(e) {  
	alert("Not able to connect to server");  
}  
}  
 
function getInfo(){  
	if(this.readyState == 4 && this.status == 200){  
		var responseVal=xmlHttpRequest.responseText;  
		document.getElementById('test').innerHTML=responseVal;  
	}  
}  
 
</script>
</head>
<body>
	<h2>Ajax JSP Example</h2>
	<form name="testform">
		<input type="text" name="numText"/> 
		<input type="button" value="Get Table" onClick="sendInfo()"/>
	</form>
 
	<p id="test"> </p>
 
</body>
</html>

index.jsp

<%  
int number=Integer.parseInt(request.getParameter("val"));    
	for(int i=1;i<=10;i++) {
		out.print(i*number+"<br/>");   
	}
%>

index.html

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Archetype Created Web Application</display-name>
   <welcome-file-list>  
    	<welcome-file>index.html</welcome-file>  
   </welcome-file-list>  
</web-app>

Output

ajax example 1 - codesjava

Posted in Ajax   
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki