AJAX (prototype/java) getting partial status updates during execution

后端 未结 2 1005
伪装坚强ぢ
伪装坚强ぢ 2021-02-10 13:53

This partially mimics AJAX (prototype/php) getting partial status updates during script execution, however I\'m working with JSP pages and servlets. What I want to do is start a

2条回答
  •  囚心锁ツ
    2021-02-10 14:48

    If you want to run and control a long-running process, better let it run in its own Thread instead of the request's Thread. Store a reference to this Thread in the session scope so that the client can use ajaxical requests (using the same session!) to request the server side for the current progress (and automagically also to keep the session alive so that it doesn't timeout).

    Here's a basic example of such a servlet:

    package mypackage;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class RunLongProcessServlet extends HttpServlet {
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
        {
            if ("XMLHttpRequest".equals(request.getHeader("x-requested-with"))) {
                LongProcess longProcess = (LongProcess) request.getSession().getAttribute("longProcess");
                response.setContentType("application/json");
                response.getWriter().write(String.valueOf(longProcess.getProgress()));
            } else {
                request.getRequestDispatcher("runLongProcess.jsp").forward(request, response);
            }
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
        {
            LongProcess longProcess = new LongProcess();
            longProcess.setDaemon(true);
            longProcess.start();
            request.getSession().setAttribute("longProcess", longProcess);
            request.getRequestDispatcher("runLongProcess.jsp").forward(request, response);
        }
    
    }
    
    class LongProcess extends Thread {
    
        private int progress;
    
        public void run() {
            while (progress < 100) {
                try { sleep(1000); } catch (InterruptedException ignore) {}
                progress++;
            }
        }
    
        public int getProgress() {
            return progress;
        }
    
    }
    

    ..which is mapped as follows:

    
        runLongProcess
        mypackage.RunLongProcessServlet
    
    
        runLongProcess
        /runLongProcess
    
    

    And here's a basic example of the JSP (with a little shot jQuery, an ajaxical JS framework which I by the way greatly recommend):

    
    
        
            Show progress of long running process with help of Thread and Ajax.
            
            
        
        
            

    Run long process:

    Current status: 0%

    Open it at http://localhost:8080/yourcontext/runLongProcess and click the button.

    If this is a really, really long running process, you may improve "efficiency" by increasing the ajax request intervals in the setTimeout() to 5 seconds (5000 ms) or so, so that the server doesn't feel getting DDOS'ed ;)

    Hope this helps.

提交回复
热议问题