Common Gateway Interface in GlassFish
Tuesday Jul 15, 2008
Common Gateway Interface (CGI) supports dynamic contents in web environment. CGI programs are executable programs in the server platform with specific output. It can be a Bourne shell script, Perl script or even a C binary executable. It was very popular before the the appearance of Servlet, JSP and PHP. The CGI code in GlassFish workspace is based on Tomcat. In GlassFish v3, CGI will be a supported feature. Let us look at a very simple example.
Create a CGI script
In our example, we have a simple Perl program,
hello, to print a hello message and the timestamp
of the server.
#!/bin/perl
print "Content-type: text/html\n\n";
print "Hello World: ";
print scalar localtime;
print "\n";
Enabling CGI processing and packaging the war file
The CGI processing can be enabled in a war file by adding
CGIServlet et al in web.xml as follows:
<web-app>
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
</web-app>
In this case, one need to package the hello
under the default cgiPathPrefix which is
WEB-INF/cgi.
For security, it is highly recommended that the contents / binaries of
CGI programs should be prohibited from direct viewing or download.
Alternatively, one can enable CGI by uncommenting the corresponding sections
in default-web.xml.
In our case, the CGI program can be invoked through http://localhost:8080/YOUR_CONTEXT/cgi-bin/hello .
Configuration of CGIServlet
One can configure CGIServlet by specifying the init-param as follows:
| init-param | Type | Description | Default |
|---|---|---|---|
cgiPathPrefix | String | subdirectory containing the cgi programs | WEB-INF/cgi
|
debug | int | debug level | 0 (no debug)
|
executable | String | executable for running the CGI script | perl
|
parameterEncoding | String | encoding use for parameter | System.getProperty("file.encoding", "UTF-8")
|
passShellEnvironment | boolean | whether to pass environment properties to CGI program | false
|
CGI with native executables
GlassFish v3 CGI can work with native executables as follows:
- set the
init-paramwith nameexecutableto be the empty String inweb.xml - has exploded directory structure for the war in a directory, say
/export/cgitest - make sure those executables has the executable bits set correctly
- deploy the "directory" (not the "war"), for instance
asadmin deploy /export/cgitest










