【问题标题】:Running CGI on Jetty在 Jetty 上运行 CGI
【发布时间】:2012-01-06 20:04:03
【问题描述】:

我从 Java 程序本身内部启动 Jetty。我只是想让它指向我放在包目录(/src/package/cgi-bin)下的一个 cgi 文件夹

问题是每次我启动服务器时,它都会抱怨“没有 CGI bin!”。放置 cgi-bin 并指向它的正确位置在哪里?这就是我所拥有的:

public static void main(String[] args) throws Exception {
    // The core Jetty server running on 8080
    Server server = new org.eclipse.jetty.server.Server(8080);

    // Setup CGI routing
    ServletContextHandler context2 = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context2.setContextPath("/");
    context2.setInitParameter("commandPrefix", "perl");

    // Where does this point to? 
    context2.setInitParameter("cgibinResourceBase", "cgi-bin");
    server.setHandler(context2);

    // Add the CGI Servlet
    context2.addServlet(new ServletHolder(new Cgi()), "/cgi");

    server.start();
    server.join();
}

有关此问题的答案,请参阅 Jetty setInitParameter is NOT initializing any parameter

【问题讨论】:

    标签: java cgi jetty


    【解决方案1】:

    通过Jetty setInitParameter is NOT initializing any parameter

    问题是我在 context 上设置参数,而我应该在 ServletHolder 上设置参数

    【讨论】:

      【解决方案2】:

      looking at the code 中,您的setInitParameter 似乎设置不正确。尽管我们使用 XML 来配置 Jetty,但您似乎在做正确的事情,而且我不熟悉在代码中这样做。

      您是否尝试在设置处理程序之前添加 servlet?会不会是叫错了init

      String tmp = getInitParameter("cgibinResourceBase");
      if (tmp == null) {
          tmp = getInitParameter("resourceBase");
          if (tmp == null)
              tmp = getServletContext().getRealPath("/");
      }
      
      if (tmp == null) {
          Log.warn("CGI: no CGI bin !");
          throw new ServletException();
      }
      

      没有太多帮助,但我会留下这个答案,直到有人发布更好的答案。

      【讨论】:

      • 我想这是我们能做的最好的了,不过还是谢谢。它确实帮助我朝着正确的方向前进,但该方法实际上是在 server.start() 上调用的。我只是不明白为什么 setInitParameter 没有携带信息。
      【解决方案3】:

      您的cgi-bin 似乎没有指向正确的位置。

      // Where does this point to? 
      context2.setInitParameter("cgibinResourceBase", "cgi-bin");
      

      正确的路径应该是相对于您的 WEB-INF 目录的,或者作为故障保护,是您的 file-system 上的绝对路径。

      在这种情况下,您应该使用/src/package/cgi-bin,假设src 是您file-system 的根。如果不是,您可以使用以下内容:

      // The second argument is the absolute path to your cgi-bin
      context2.setInitParameter("cgibinResourceBase", "/usr/local/some-more-path/src/package/cgi-bin");
      

      【讨论】:

        猜你喜欢
        • 2015-03-28
        • 1970-01-01
        • 2016-10-31
        • 2011-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多