Hello World CGI Demos

No example is complete without a Hello World demo. Below are some simple examples of printing out "Hello World" from a variety of languages used at the University.

Java CGI Demo

The following program has been created as HelloWorld.java:

/* HelloWorld.java */
import java.util.*;

public class HelloWorld{
   public static void main(String[] args){
    System.out.println("Content-type: text/html\n\n");
    System.out.println("<head><title>Hello World</title></head>");
    System.out.println("<H1>Hello World</H1>");
    System.out.println("I'm a Java program<br>");
    Date d = new Date();
    DateFormat df = DateFormat.getDateInstance();

A script file is needed to run the java application, this is called HelloWorld.cgi:

#!/bin/sh /usr/bin/java HelloWorld

There are a few points to note. The student webserver is set up so that any file ending in .cgi is taken to be a cgi script. Normally these scripts are stored in a special directory. cgi scripts are run by the user nobody. You have to tell nobody which shell to run and give the full path, hence /usr/bin/java. If your code uses any additional libraries, the -classpath option of the java command must be used. You have to get the access permissions right as well!

Now click on here to run the program.

C CGI Demo

For the C programmers amongst you, here is the same again, but written in C:

/* HelloWorld.c */
#include <stdio.h>
#include <time.h>

int main(void)
      time_t now;
      printf("Content-type: text/html\n\n");
      printf("<head><title>Hello World</title></head>");
      printf("<H1>Hello World</H1>");
      printf("I'm a C Program<br>");
      printf("It is now %s",ctime(&now));

Now click on here to run this program.

Shell Script

If programming is too much effort, the same can be achieved with a Shell script:


echo Content-type: text/html
echo "<html><head><title>Hello World</title>"
echo "<h1>Hello World</h1>"
echo "I'm a Shell Script<br>"
echo "The time is now"
echo "</body></html>"

Click on here to run this program.

You can decide which you think is the faster to run/write!


If you are a student, the system setup has now changed so that student accounts can no longer run cgi scripts, so the above is for information only.

This document was last modified on: 24th March 2014