简体   繁体   English

我需要更改网页的内容而不刷新服务器端发生更改的位置

[英]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.

相关问题 在不更改UI android的情况下,确切地需要如何以及在何处进行更改以接受来自重音内容的常规字符搜索 - How and where exactly need to change to accept normal character search from the accented content without changing the UI android 当我在doPost中更改servlet内容时,什么也没发生 - When I change servlet content in doPost, nothing happens 动态更改JSP页面内容而无需按钮 - Dynamically change JSP page content without buttons 无需重新部署就可以将部署在在线Web逻辑服务器上的War文件中的Jsp / HTML页面更改? - Change in Jsp/Html page in War file deployed on online web logic server without re-deployment? html5:无需刷新即可将内容写入页面 - html5: Write content to a page without refreshing 我想阅读html的内容,我需要用所需的文本进行更改 - i want to read the content of html and i need to change it with the required text 我需要一个想法,如何在不使用表单的情况下将内容放入会话中,或者导航/刷新页面 - I need an idea of how to put stuff in session without using a form or navigate away/refreshing the page 网页更改检测 - web page change detection 如何从服务器端更改HttpServletRequest中的IP? - How can I change IP in HttpServletRequest from server side? Java:WSDL Web服务wsimport,我是否需要重新运行wsimport才能更改webservice服务器中的@WebService类代码 - Java: WSDL web service wsimport, do I need to re-run wsimport of I change the @WebService class code in the webservice server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM