My application consists of two JSP pages. One is responsible for the UI, while the other processes the response.
UICounter.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page</title>
<script>
function createRequest() {
var xmlHttpReq = false;
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (exp1) {
try {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (exp2) {
xmlHttpReq = false;
}
}
}
return xmlHttpReq;
}
function submitRequest() {
var xmlHttpRequest = createRequest();
xmlHttpRequest.onreadystatechange = handleReadyState(xmlHttpRequest);
xmlHttpRequest.open("POST", "Control.jsp", true);
xmlHttpRequest.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttpRequest.send(null);
}
function handleReadyState(xmlHttpRequest) {
return function() {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
var count = parseInt(xmlHttpRequest.responseText);
document.getElementById("counterLabel").innerHTML = count;
} else {
alert("HTTP error " + xmlHttpRequest.status + ": " + xmlHttpRequest.statusText);
}
}
};
}
</script>
</head>
<body>
<div id="counterLabel">0 </div>
<script type="text/javascript">submitRequest();</script>
</body>
</html>
Control.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
if(session.getAttribute("counter") != null && !session.getAttribute("counter").toString().equals(""))
{
String stringStatus = session.getAttribute("counter").toString().trim();
int statusCounter = Integer.parseInt(stringStatus);
session.setAttribute("counter",String.valueOf(++statusCounter));
}else{
session.setAttribute("counter",String.valueOf(1));
out.print(session.getAttribute("counter"));
}
%>
</body>
</html>
I am looking to implement a feature where AJAX calls are made until a certain condition is met. I have attempted to call the same function (submitRequest()
) recursively within the AJAX response loop until the result reaches 100. However, this approach does not seem to be functioning correctly. Could someone provide guidance on how to achieve this goal? Any examples or additional clarification would be highly appreciated. Thank you!