开发者

stoping doGet/dopost in java servlets?

开发者 https://www.devze.com 2022-12-23 02:08 出处:网络
I\'ve been playing with Java Servlets and Ajax a bit, and I\'ve got a situation on which I would really appreciate advice.

I've been playing with Java Servlets and Ajax a bit, and I've got a situation on which I would really appreciate advice.

Let's say I have HTML page with a start and stop buttons, and as a result of clicking start button,

overridden doGet (or doPost) method on a servlet is invoked which computes something that takes a long time to complete. (e.g. a giant loop, or even Infinite loop, doesn't matter, I'm interested in concepts here).

So, I'm asking you:

1.What would be my options to kill / shut down / halt / exit doGet method whan I hit stop button on a web page? Do I use threading here, or there is simpler way? I take it that using System exit is not a very good idea, right? 开发者_如何学Go;)

2.So, let's say I implement code for stopping doGet method. What would happen If I hit start on one browser(e.g.IE), and while this long computation takes place open new tab or other browser(e.g.Firefox) and open same url and hit stop? Would that stop my original computation? Is there any easy way to avoid this?

I know that questions are a bit off, as I'm just starting with server-side of things. :)

Any suggestions would be greatly appreciated!


your stop handler can set a flag in the session context, which the long-running thread will occasionally check and exit if necessary.

you can avoid the multiple browser issue, by generating a unique task id each time the page is loaded. then you can only start or stop a specific task. this id can be a key in the session.


I think you need some kind of new process started after you submit request and this process should answer responses during runtime (showing progress for example via AJAX). Also it should check if there is new request with stop command. Page should be AJAX one with progress/result/stop button.

0

精彩评论

暂无评论...
验证码 换一张
取 消