FastCGI in Jetty

Jetty can serve PHP, Python, or Ruby scripts via FastCGI. FastCGI is a network protocol primarily used by a web server to communicate to a FastCGI server. FastCGI servers are used to serve web content generated by dynamic web languages including PHP, Python, Ruby, Perl and others.

The servlet that processes CGI scripts is called org.eclipse.jetty.servlets.CGI. This servlet is asynchronous, therefore, we have to add the <async-supported>true</async-supported> to the web.xml file.

<!DOCTYPE html>
        Get environment <a href="cgi-bin/">variables</a>

When our web application starts, it shows this simple HTML file. A link refers to a Python CGI script to get environment variables.

import sys
import os
from cgi import escape

print "Content-type: text/html"
print "<!doctype html>"
print "<html>"
print "<body>"
print "<pre>"
for k in sorted(os.environ):
    print "<b>%s: </b>%s" %(escape(k), escape(os.environ[k]))
print "</pre>"
print "</body>"
print "</html>"

Our example will process this Python script. It creates a simple HTML page containing environment variables.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""

This is the application's web.xml file. It maps the CGI servlet to /cgi-bin/* URL pattern. In the welcome file list, we specify the index.html file containing a link to a Python script. The cgibinResourceBaseIsRelative sets the path to the cgi-bin directory to be relative to the web application document root directory. The commandPrefix parameter specifies the path to the Python interpreter.

<?xml version="1.0" encoding="UTF-8"?>

<project name="FastCGIServlet" default="archive">
    <property name="name" value="cgiexample"/>
    <property environment="env"/>
    <property name="src.dir" value="src"/>
    <property name="web.dir" value="${src.dir}/web"/>
    <property name="dist.dir" location="dist"/>
    <property name="deploy.path" location="${env.JETTY_BASE}/webapps"/>
    <target name="init">
        <mkdir dir="${dist.dir}"/>
    <target name="archive" depends="init">
        <war destfile="${dist.dir}/${name}.war" 
            <fileset dir="${web.dir}"/>
        <echo>Archive created</echo>
    <target name="clean" depends="init">
        <delete dir="${dist.dir}"/>
        <echo>Cleaning completed</echo>
    <target name="deploy" depends="archive">
        <copy file="${dist.dir}/${name}.war" overwrite="true" 
        <echo>Archive deployed</echo>

The Ant archive task creates a web archive containing the index.html file, web.xml file and a Python script in the WEB-INF/cgi-bin/ subdirectory. The deploy task copies the WAR file into the deployment directory of the Jetty base.

$ java -jar $JETTY_HOME/start.jar --add-to-start=http,deploy,servlets

Assuming that we have not yet enabled any module in the Jetty base, we enable these three modules. The Jetty CGI servlet is in the servlets module.

$ java -jar $JETTY_HOME/start.jar

We start the Jetty server. Now we can navigate to the http://localhost:8080/cgiexample/ URL.

Output of the Python script
Figure:Output of the Python script

In this chapter of the Jetty tutorial, we have executed a Python script using the Jetty's CGI servlet.