【问题标题】:On a get request, how can I start program, and then using another get request stop it?在一个 get 请求上,我怎样才能启动程序,然后使用另一个 get 请求停止它?
【发布时间】:2015-07-01 20:31:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript java html http routes


    【解决方案1】:

    使用sessions 代替全局变量。

    【讨论】:

    • 感谢您的意见,指导我了解我正在努力做得更好。
    猜你喜欢
    • 1970-01-01
    • 2013-11-02
    • 2019-09-02
    • 2019-06-09
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 2021-10-26
    相关资源
    最近更新 更多