在获取请求时,如何启动程序,然后使用另一个获取请求停止它?

On a get request, how can I start program, and then using another get request stop it?

我是 运行 使用 java 和 tomcat 的网站。在高层次上,我正在尝试做的是将开始按钮路由到 java 方法,该方法对其他资源进行无限量调用。当我想停止那个程序时,我会按下停止按钮。

我已经找到开始部分,但无法停止,因为我认为获取请求页面的状态没有得到保存。我在那个 class 中有一些全局变量,我认为每次我发出获取请求时它们都会被重置。我怎样才能防止这种情况发生?

@Path("/myresource")
public class MyResource {
continuousMethod sample = new continuousMethod()
boolean collecting = false;
/**
 * Method handling HTTP GET requests. The returned object will be sent
 * to the client as "text/plain" media type.
 *
 * @return String that will be returned as a text/plain response.
 */
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/collect")
public String startCollection() throws URISyntaxException {
    System.out.println("Made it here");
    sample.start();
    collecting = true;
    return "Collecting";
}
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/stopcollect")
public String stopCollection() {

    sample.close();
    collecting = false;
    return "Finished collecting!";

    //return "Not collecting";
}
}

使用 sessions 而不是全局变量。