[英]I need to change the content of a web page without refreshing where the change happens in the server side
I have a web app with java that take requests from clients, process the request then send the result back to the client. 我有一个带有java的Web应用程序,它接收来自客户端的请求,处理请求然后将结果发送回客户端。 I want to add a graphic interface that will show information about the request, the process and the result after the arrival of each request.
我想添加一个图形界面,它将在每个请求到达后显示有关请求,进程和结果的信息。 I want to do this without having the page reloading, the server can receive a lot of requests in a short time so reloading the page is not a good solution.
我希望在没有重新加载页面的情况下执行此操作,服务器可以在短时间内收到大量请求,因此重新加载页面不是一个好的解决方案。 I've tried doing this with AJAX but the change is after an event provoked at the client side, for example a click on a button.
我尝试过使用AJAX,但是更改是在客户端发生的事件之后,例如点击按钮。 I want the event to be on the server's side my event needs to be the arrival of a new request.
我希望事件在服务器端,我的事件需要是新请求的到来。 tried using setInterval but the result is not what I expect, its like only one request get shown out of 10 this is the setInterval code :
尝试使用setInterval,但结果不是我所期望的,就像只有一个请求显示在10中这是setInterval代码:
setInterval(function() {
$.get("log", function(responseText) {
$("#container").append(responseText + "<br>");
});
}, 10);
log is the URL of the servlet that gives information about the request the doGet methode of the log servlet : log是servlet的URL,它提供有关日志servlet的doGet方法的请求的信息:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(requestInfo);
}
every time a request arrive I start putting information in requestInfo 每次请求到达时,我都会开始在requestInfo中放入信息
If you want to constantly check the status, you have to recall your ajax function. 如果要不断检查状态,则必须调用ajax函数。 If you want to be informed by push, you have to use websockets.
如果您希望通过推送获得通知,则必须使用websockets。
function checkLog(){
$.get("log", function(responseText) {
var $notFinished = true;
console.log('fetch from server')
$("#container").append(responseText + "<br>");
// do some checks to determine server has finished, set notFinished to false if it is your last ajax request
...
if($notFinished){
// recall the function in 10 seconds for status check
setInterval(function() {
checkLog();
}, 10000);
}
});
}
// call the function the first time
checkLog();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.