Guides/CGI

From J Wiki
Jump to: navigation, search

J can be used for CGI programming. Here are some considerations and examples.

Note: the examples assume Linux, but Windows would be similar - feel free to add Windows-specific references.


J Interpreter

For most purposes, the jconsole should suffice for CGI, but the full J system can also be used if required.

Scripts

You need at least a script with definitions to read the CGI parameters and create the required CGI output. What else is needed depends on your application. This can include the J standard libraries.

A CGI script with a minimal set of required definitions is cgi.ijs in the web subversion repository. This is a standalone script, and can be used for testing.

The script needs to exit at the end, otherwise the J session remains alive. If necessary, wrap commands in try catch, so that after a failure, the catch line does the exit.

Permissions

File permissions should allow the web server to run the J interpreter, and read the scripts.

CGI script

This can be a hash bang script to load the J interpreter and run the script.

Simple Example

Download the cgi.ijs script, and save it as test.ijs. It can be saved anywhere, but ensure that the web server has read access. Edit the script and uncomment the last line, so that the script runs cgitest''.

Create a test.cgi script in your cgi-bin directory, as follows. Use the correct pathnames:

#!/bin/sh
/usr/lib/j/jconsole -jprofile /home/chris/test.ijs

This script should be executable, and can be tested by running it from the command line. The session should show the html generated. For example, run:

/usr/lib/cgi-bin/test.cgi

Next, save the following as file test.html in your web directory:

<html><body>
<form action="/cgi-bin/test.cgi" method="get">
<input type=text value="get me" name=tget><br>
<input type="submit" value="Run">
</form></body></html>

Open this file in your browser, and press the Run button. The resulting display should show the timestamp, and the edit box name and contents:

2006 1 3 13 44 36
+----+------+
|tget|get me|
+----+------+

If this does not work, you should check file permissions to ensure that the web server has access, and also check the server log.

J Environment Variable

No special environment settings were needed for the simple example above, or whenever you are just loading a single script. However, you should define the J environment variable if you need to load the J profile, i.e. if you do not use the -jprofile flag. You can do this either in the test.cgi script, or as part of the web server configuration. For example, the following test.cgi sets the J environment variable, and loads J with the standard profile. Use the correct variable name for your version of J:

#!/bin/sh
export JPATHj601=/usr/lib/j
/usr/lib/j/jconsole /home/chris/test.ijs

Alternatively, for Apache, edit httpd.conf to include:

SetEnv JPATHj601 /usr/lib/j

Examples and References

John Howland's j-web system runs the Trinity University Computer Science Department pages from J cgi scripts.

The J forum search is an example of a page using a J cgi script. For the source, see the web subversion repository.

Oleg Kobchenko's JHP hypertext processor allows J code to be embedded in a web page.

CGI introduction and reference.