我正在开发一个小型网络应用程序,它查询几个数据库来进行数据预测,然后返回一个包含预测结果的对象。
这已经有点工作了,但是 servlet 中的数据处理可能会花费半个小时以上,具体取决于查询过滤器,我通常在提交时设置一个 javascript“加载”div,但这有点悲伤。
所以问题是,我可以在 servlet 处理时以某种方式向 Web 客户端发送消息,这样页面看起来就不会死掉吗? (使用有关流程进行情况的实际信息改进加载 div)
最佳答案
只需在发送请求之前添加预加载器,并在成功后删除该预加载器。
var isBool = false;
function communicateWithServer(){
$("#preloader").show();
var isBool = false;
$.ajax({
url : 'url',
data : {
},
type : 'POST',
dataType : 'text',
success : function(response) {
isBool=true;
$("#preloader").hide();
//Show success
},
error : function(request, textStatus, errorThrown) {
$("#preloader").hide();
//show error
}
});
secondComm();
}
function secondComm(){
$.ajax({
url : 'url2',//send the response from your second servlet to this
data : {
},
type : 'POST',
dataType : 'text',
success : function(response) {
$("#preloader").text(response.value);
if(!isBool){secondComm();}
//
},
error : function(request, textStatus, errorThrown) {
if(!isBool){secondComm();}
//show error
}
});
}
请不要与 jquery
混淆,您也可以在没有 jquery
的情况下完成此操作。
function communicateWithServer() {
document.getElementById("preloader").style.display = 'block';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//show success
} else {
//show error
}
document.getElementById("preloader").style.display = 'none';
};
xhttp.open("GET", url, true);
xhttp.send();
}
关于javascript - 在web中显示java servlet进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51415793/