how to implement Ajax in jsp servlet?

10/16/2021
All Articles

#java #jsp #servlet #ajex #post #get 

how to implement  Ajax  in jsp servlet?

how to implement  Ajax  in Jsp and servlet ?

Asynchronous JavaScript and XML, or Ajax, isn’t a new technology in itself, and it’s not a programming language. The term was coined back in 2005 by Jesse James Garrett.

See below example to implement Ajex in jsp

Creating jsp file index.jsp 


<%@ page import="java.sql.*"%>

<%
int n=Integer.parseInt(request.getParameter("val"));

for(int i=1;i<=10;i++)
out.print(i*n+"
"); %>

Below code calling index.jsp using ajex function


<script>
var request;
function sendInfo()
{
var v=document.vinform.t1.value;
var url="index.jsp?val="+v;

if(window.XMLHttpRequest){
request=new XMLHttpRequest();
}
else if(window.ActiveXObject){
request=new ActiveXObject("Microsoft.XMLHTTP");
}

try
{
request.onreadystatechange=getInfo;
request.open("GET",url,true);
request.send();
}
catch(e)
{
alert("Unable to connect to server");
}
}

function getInfo(){
if(request.readyState==4){
var val=request.responseText;
document.getElementById('amit').innerHTML=val;
}
}

</script >
This is an example of ajax
<input name="t1" type="text" />
<input type="button" value="ShowTable" />

This Solution is provided by Shubham mishra

This article is contributed by Developer Indian team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Also folllow our instagram , linkedIn , Facebook , twiter account for more....

Article