【问题标题】:Output Stream to webpage输出流到网页
【发布时间】:2013-11-14 00:43:55
【问题描述】:

我正在为一些服务器测试编写一个非常粗糙的 Web 界面。

我的 servlet 代码基本上如下所示:

import Application.*;

@WebServlet("/runtest")
public class RunTestServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println(View.runTestPageHTML());
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Application.runTest();
        doGet(request,response);
    }
}
class View{
    public static String runTestPageHTML(){
        return "<html><body><form><submit value='run'></form></body></html>";
    }
}

这有两个问题。如果重新发送表单,Tomcat 可以并且将会开始一个新工作,并且没有关于工作进度的反馈。

我基本上希望 Application.runTest() 将所有到 http://&lt;server&gt;/runtest 的访问重新路由到 logger.out 并忽略所有帖子,直到作业完成。

【问题讨论】:

    标签: java html http tomcat servlets


    【解决方案1】:

    你的工作是在 Application.runTest() 中,对吧? 如果 runTest() 是静态方法,则可以使用全局变量来控制每个请求的一次执行。

        public class Application{
               private static boolean inExecution = false;
    
               public static void runTest(){
                  if(!inExecution){
                     inExecution = true;
                     //(...) yout job
                     inExecution = false;
                  }
    
               }
         }
    

    【讨论】:

      猜你喜欢
      • 2018-08-06
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      相关资源
      最近更新 更多