Sun ONE logo      Previous      Contents      Index      Next     

Sun ONE Web Server 6.1 Getting Started Guide

Chapter 8
Using CGI

You can set up your virtual server to serve dynamic content in two ways: using Java-based web applications and using non-Java applications such as CGIs, ASP, Server-side HTML (SHTML) tags and NSAPI programs. In this chapter, we will look at using CGI programs. In the following chapter, Chapter 9, "Using Sun ONE Active Server Pages," we will use Sun™ ONE Active Server pages to create dynamic content.

For details about using SHTML tags, see the Sun ONE Web Server 6.1 Programmer’s Guide, and for details on using NSAPI, see the Sun ONE Web Server 6.1 NSAPI Programmer’s Guide.

CGI (Common Gateway Interface) programs are external programs that the server calls to process data.

Sun ONE Web Server 6.1 bundles a Perl interpreter, which makes it possible to run CGI programs that are written in the Perl programming language. The Perl interpreter is located at server_root/bin/https/perl/perl. On Windows, file associations need to be in place for CGI programs to run. For example, files with a .pl filename extension need to be associated with the Perl interpreter executable before they can be executed as CGI programs.

For information about writing CGI programs, see the following sources of information:

This section briefly uses a simple exercise to show how you can configure your server to use CGI.

Let’s assume we have a simple Perl program called hello.pl that we want to use CGI on our web server to execute. When executed, this program will display the words “Hello World!” on a browser window.

  1. Use a text editor to create a file called hello.pl and copy the following lines into it (remember to substitute <server_root> with the absolute path to your web server’s server root directory), and save your changes:
  2. #!<server_root>/bin/https/perl/perl

    print "Content-type:text/html\n\n";

    print "Hello World!";

  3. Next, we need to create a directory for this program. By convention, this directory has come to be known as the cgi-bin directory though you can name it whatever you like.
  4. mkdir /opt/SUNWwbsvr/cgi-bin

  5. Copy the hello.pl file into the /opt/SUNWwbsvr/cgi-bin directory
  6. Access the Server Manager interface on a browser and click the Programs tab.

  7. Figure showing the CGI Directory page.

  8. Click the CGI Directory link if the server is running on UNIX or on Windows, the WinCGI Directory link. Specify:
    • a URL prefix (to which clients will send requests). In our example, this is set to /cgi-bin
    • the CGI directory. In our example, /opt/SUNWwbsvr/cgi-bin.

    • Caution

      The server must have read and execute permissions to this directory.


  9. Click OK and then Apply to save and apply the changes.

Open a browser window and (if your server is called acme, running on port 2222), type http://acme:2222/cgi-bin/hello.pl.

This executes the “Hello World” script, as shown below:

Figure showing the "Hello World!" script.



Previous      Contents      Index      Next     


Copyright 2004 Sun Microsystems, Inc. All rights reserved.