2012-01-06 77 views
2

我从Java程序本身开始使用Jetty。我只是想让它指向我已经放在软件包目录下的一个cgi文件夹(/ src/package/cgi-bin)在Jetty上运行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

回答

1

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(); 
} 

没有太大的帮助,但我会离开这个答案,直到有人张贴更好的答案。

+0

我想这是我们能做的最好的,但是谢谢。它的确帮助我朝着正确的方向发展,但实际上这个方法是在server.start()上调用的。我简直不明白为什么setInitParameter没有携带这些信息。 – joslinm 2012-01-31 02:02:12

+1

源代码已移动,现在位于:http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-servlets/src/main/java/org/eclipse /jetty/servlets/CGI.java – 2013-06-30 14:41:17

0

看来您并未指向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");