如何在 jsp 页面中显示表示 java servlet 执行的进度条

how to show a progress bar in the jsp page representing the execution of a java servlet

我看到我的帖子回答了这个问题 question.But 我不知道如何实现我的 program.My 要求如下。

我从 jsp 页面调用一个 servlet,使用 ajax 调用:

$(document).on('click' , '#gettopevents' , function(event) {


            var field = $("input[name = 'field']").val(); 

            if(field === "")
            {
                $('#responsemsg').html("enter a field");
            }
            else
            {
                var dataform = { 'field' : field };

                $.ajax({
                        url : 'GetAllLogs',
                        type : 'POST' , 
                        data : dataform , 
                        success : function(response) {
                            $('#displaylist').load('listgeneration.jsp');
                        },
                        error : function(){
                            alert("error");
                        }
                });
            }

            event.preventDefault();
    });

servlet 执行是一个密集的过程,它需要一些时间 time.So 我需要向用户显示一个关于 servlet 执行状态的进度条。 更具体地说,我需要它作为。

@WebServlet("/GetAllLogs")
public class GetAllLogs extends HttpServlet
{
    public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException  
    {
          PrintWriter obj = response.getWriter();
          obj.print(10);
          // at this point i need to set the progress bar value to 10%

          ....
          ....

          obj.print(40);
          // at this point i need to change the progress bar value to 40%
         .....
         .....

          obj.print(100);
           //at this point i neet to change the progress bar value to 100%
    }
}

基本上我需要更新状态栏以获取 servlet.Is 这种方法可能的打印值,我该怎么做。 提前致谢

基本步骤如下:

  1. 第一个 ajax 调用启动 long-running 进程,returns 立即
  2. long-running 进程自己知道需要多长时间,并且可以报告*它走了多远。
  3. 另一个端点提供 long-running 进程的状态。因此,另一个 ajax 调用每 1 秒(或任何合理的时间)调用一次以获取数据以显示在进度条中。

*这可能是一些简单的东西,比如存储在会话中的 AtomicInteger,并随着 long-running 进程的工作而更新。但是,long-running 进程可能在其他 JVM 上,或者提供状态的端点可能在其他 JVM 上。如果是这种情况,启动 long-running 进程的初始调用应提供与该进程关联的唯一令牌。 long-running 进程使用令牌作为密钥更新一些共享存储,例如数据库。令牌被传递到提供状态以查找状态的端点。

编辑以添加额外的上下文

ajax 调用 https://.../api/long/running/process/start

当此 ajax 调用 returns 时,它会调用另一个开始轮询 back-end 的方法。 (基于此other post。)

        $.ajax({
                    url : https://.../api/long/running/process/start,
                    type : 'POST' , 
                    data : dataform , 
                    success : function(response) {
                        pollForLongRunningStatus(response);
                    },
                    error : function(){
                        alert("error");
                    }
            });
        }

        pollForLongRunningStatus(response) {
           $.post('https://.../api/long/running/process/status', function(data) {
           alert(data);  // update the progress bar
           setTimeout(pollForLongRunningStatus, 1000);
          });
        }

假设开始 URL 由下面的 class 处理 - 重要的是这个 class 开始漫长的 运行ning 进程并且 returns 立即开始它需要 运行 作为 asnych 任务。 (如何做到这一点取决于 back-end 框架,如果有的话。)

@WebServlet("/.../api/long/running/process/start")
public class LongRunningProcessStartHandler extends HttpServlet  {
    public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException  {

    AtomicInteger percentComplete = new AtomicInteger(0);
    request.getSession().setAttribute("LONG_RUNNING_PROCESS_STATUS", percentComplete);

    LongRunningProcess lrp = new LongRunningProcess(percentComplete);
    lrp.start();

}

public class LongRunningProcess extends Thread {
   private final AtomicInteger percentComplete;

   public LongRunningProcess(AtomicInteger percentComplete) {
       this.percentComplete = percentComplete;
   }

   public void run() {
       for (int i = 0; i < 100; i++) {
       Thread.sleep(1000);
       percentComplete.incrementAndGet();
   }
}

同时,状态 URL 仅报告完成百分比。假设此 class 处理状态端点:

@WebServlet("/.../api/long/running/process/status")
public class LongRunningProcessStatusHandler extends HttpServlet  {
    public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException  {

    return request.getSession().getAttribute("LONG_RUNNING_PROCESS_STATUS").get();
}

这是一般的想法。 (上面的代码不会编译,需要设置为 null 安全。)当值达到 100 时,然后从会话中删除对象以保持会话干净。