AJAX (prototype / java) receiving partial status update at runtime

This partially mimics AJAX (prototype / php) receiving partial status updates during script execution , however I work with JSP pages and servlets. What I want to do is start the action when the user clicks the button and then presents updates on the progress of this action. The action can take from 1 to 10 minutes, so I don’t want the user to just sit on the screen waiting for a response, but rather display a status bar or something indicating in which part of the action the transaction is included.

thanks

+4
source share
2 answers

If you want to start and manage a long-term process, it is better to let it run in its own Thread instead of a Thread request. Keep a link to this Thread in the session area so that the client can use adiaxic requests (using the same session!), To request the server side for the current progress (and also automatically keep the session active, t timeout).

Here is 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 displayed as follows:

 <servlet> <servlet-name>runLongProcess</servlet-name> <servlet-class>mypackage.RunLongProcessServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>runLongProcess</servlet-name> <url-pattern>/runLongProcess</url-pattern> </servlet-mapping> 

And here is the main example of JSP (with a small margin of jQuery , ajaxical JS framework, which I highly recommend by the way):

 <!doctype html> <html lang="en"> <head> <title>Show progress of long running process with help of Thread and Ajax.</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(init); function init() { if (${not empty longProcess}) { $.progress = 0; checkProgress(); } } function checkProgress() { $.getJSON('runLongProcess', function(progress) { $('#progress').text(progress); $.progress = parseInt(progress); }); if ($.progress < 100) { setTimeout(checkProgress, 1000); } } </script> </head> <body> <form action="runLongProcess" method="post"> <p>Run long process: <input type="submit"></p> <p>Current status: <span id="progress">0</span>%</p> </form> </body> </html> 

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

If this is really a very lengthy process, you can improve the "efficiency" by increasing the ajax request intervals in setTimeout() to 5 seconds (5000 ms) or so that the server does not feel DDOS'ed;)

Hope this helps.

+5
source

You may like DWR . Using DWR, you can make asynchronous requests to the server to get information about the progress of a specific task.

+1
source

All Articles