有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript在一个get请求中,如何启动程序,然后使用另一个get请求停止它?

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

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

@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";
}
}

共 (1) 个答案

  1. # 1 楼答案

    使用sessions而不是全局变量