在返回响应之前获取 servlet 请求的状态

发布于 2024-09-05 19:49:57 字数 288 浏览 2 评论 0原文

晚上好,

我正在编写一个 Java Servlet(Struts 2、Tomcat、JSP 等),它能够进行一些相当复杂的模拟。这些最多可能需要 2 分钟才能完成,并将返回结果图表。计算模拟完成的百分比很简单,因为该过程是通过重复相同的计算数千次来实现的。

我很想知道是否有人尝试过使用客户端技术来提供完成百分比的估计。即查询 servlet 处理以获得整个模拟过程中各个点完成的周期数。然后,它可以在客户端浏览器中显示为一个栏。

任何想法、建议、资源将不胜感激。

谢谢,

亚历克斯

Good evening,

I am in the process of writing a Java Servlet (Struts 2, Tomcat, JSP etc) which is capable of doing some fairly complex simulations. These can take up to 2 minutes to complete on the and will return a graph of the results. It is trivial to calculate the percentage of the simulation completed because the process works by repeating the same calculations 1000s of times.

I would be interested to know if anyone has ever tried to use client side technology to provide any estimate of the percentage complete. I.e query the servlet processing to get the number of cycles completed at various point throughout the simulation. This could then be displayed as a bar in the client browser.

Any thoughts, advice, resources would be much appreciated.

Thanks,

Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福丶如此 2024-09-12 19:49:57

在您的数据库中,有一个表来维护模拟列表及其服务器计算的进度。

在您的 Web 应用程序中,使用 AJAX 每隔几秒查询一次服务器(我会使用 1-20 次,具体取决于负载)并更新图形进度条。大多数 javascript 库都有简单的基于计时器的 AJAX 函数来完成此类事情。

有一些细节需要弄清楚,例如完成的模拟是否保留在数据库中(可能是有用的日志信息),但总的来说,这应该相当简单。

In your database, have a table to maintain a list of simulations along with their server-calculated progress.

In your web-application, use AJAX to query the server every few seconds (1-20 depending on load is what I'd go with) and update a graphical progress bar. Most javascript libraries have simple timer-based AJAX functions to do exactly this sort of thing.

There's a few details to figure out, such as whether or not completed simulations remain in the DB (could be useful logging info), but overall, this should be fairly simple.

素手挽清风 2024-09-12 19:49:57

您可以将您的响应封装在 mime/multipart 消息中,并发送您的更新,直到完成完整的响应。

Bugzilla 在搜索中使用此功能来显示“正在搜索...”屏幕,直到搜索结果完成。

You could encapsulate your response in a mime/multipart message and sends your updates until you have a full response done.

Bugzilla uses this in their search to show "Searching ..."-screen until the searchresult is done.

歌枕肩 2024-09-12 19:49:57

如果你想使用普通的 Struts2,你应该看看 执行并等待拦截器

它通过旧的refresh-with-timeout方法工作。当然,它的酷度比某些 AJAX 东西要低,但它简单且有效(我已经使用过它)。

Struts2 负责(通过使用此拦截器)在单独的线程中执行操作方法(这通常需要很长时间),并返回一个特殊的结果 wait 直到工作完成。您只需编写一个 jsp 代码,为该结果显示一些“等待...”消息,并使其重复刷新到相同的操作,(例如)两到三秒的超时。当然,jsp 可以访问操作属性,因此您可以编写一些 getProgress() 方法来显示进度消息或栏。

If you want to use plain Struts2, you should take a look at the ExecuteAndWait Interceptor.

It works by the old refresh-with-timeout method. Sure, it has lower coolness factor than some AJAX thing, but it's simple and works (I've used it).

Struts2 takes care (by using this interceptor) of executing the action method (which would typically take a long time) in a separate thread, and it returns a special result wait until the work is completed. You just have to code a jsp that shows some "waiting..." message for this result, and make it refresh to the same action, repeatedly, with (say) two or three seconds of timeout. The jsp has access to the action properties, of course, hence you can code some getProgress() method to show a progress message or bar.

今天小雨转甜 2024-09-12 19:49:57

AJAX 就是解决这个问题的方法。

AJAX is the way to go here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文