SECTION: 900-Content
TITLE: CGI
QUESTION: Can Jetty do CGI?

The Jetty distribution includes a CGI servlet <A TITLE="View JavaDoc" HREF="/javadoc/org/mortbay/servlet/CGI.html">org.mortbay.servlet.CGI</A>. You configure it in your jetty.xml
configuration file along these lines:
<P>
<PRE>
  &lt;Call name="addContext"&gt;
    &lt;Arg&gt;/cgi-bin/*&lt;/Arg&gt;
    &lt;Set name="ResourceBase"&gt;/home/jules/www/cgi-bin&lt;/Set&gt;
    &lt;Set name="ServingDynamicServlets"&gt;TRUE&lt;/Set&gt;
    &lt;Call name="addServlet"&gt;
      &lt;Arg&gt;Common Gateway Interface&lt;/Arg&gt;
      &lt;Arg&gt;/&lt;/Arg&gt;
      &lt;Arg&gt;com.mortbay.Servlet.CGI&lt;/Arg&gt;
     &lt;Put name="Path"&gt;/usr/local/bin:/usr/ucb:/bin:/usr/bin&lt;/Put&gt;
    &lt;/Call&gt;
  &lt;/Call&gt;
</PRE>
<P>
Note that you can use <code>Put name=</code> statements in the jetty.xml file to configure <code>initParams</code> for the CGI servlet, or you can use <code>initParam</code> statements in the <code>web.xml</code> descriptor.
<P>
Some useful <code>initParams</code> are:
<ul>
 <li><code>commandPrefix</code> -  may be used to set a prefix to all commands passed to exec. This can be used on systems that need assistance to execute a particular file type. For example on windows this can be set to "perl" so that perl scripts are executed.</li>
 <li><code>path</code> - is passed to the exec environment as PATH.
</ul>
<P>
Furthermore, any <code>initParam</code> that starts with <code>ENV_</code> is interpreted by the CGI servlet as being an environment variable. The leading <code>ENV_</code> is stripped and the resulting name and value are passed to the exec environment.
<P>
On Windows boxes, you may find that you need to set the <code>initParam</code> <code>ENV_SystemRoot</code> to make your script work eg:
<p>
  &lt;Put name="ENV_SystemRoot"&gt;C:/Windows&lt;/Put&gt;
