Simple Ajax script for JSP
Lot of script is available for auto complete, load page, validation, and menu navigation in Ajax which can use in JSP.
A simple example of ajax show you to use in JSP
index.jsp
<html>
<head>
<script src=”ajaxjs.js”></script>
</head>
<body>
<a href=”javascript:loadContent(‘parameterValue’)”>Load Ajax content</a>
<div id=”prtCnt”></div>
</body>
</html>
ajaxjs.js
var xmlhttp
function loadContent(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert (“Your browser does not support Ajax HTTP”);
return;
}
var url=”loadJSP.jsp”;
url=url+”?q=”+str;
xmlhttp.onreadystatechange=getOutput;
xmlhttp.open(“GET”,url,true);
xmlhttp.send(null);
}
function getOutput()
{
if (xmlhttp.readyState==4)
{
document.getElementById(“prtCnt”).innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
return new ActiveXObject(“Microsoft.XMLHTTP”);
}
return null;
}
JSP file which load in background
loadJSP.jsp
<%@ page contentType=”text/html; charset=iso-8859-1″ language=”java” %>
<%
String q =request.getParameter(“q”);
String str=”This is JSP string loading from JSP page in ajax, loading time :”;
java.util.Date dt=new java.util.Date();
out.print(str+dt);
%>
source: easywayserver