JSerWeb Java Web Server Version 1.0
* Documentation
* Requirements
* Installation
* Download
 
* Configuration
* Main configuration file
* Virtual Hosts
* Dynamic Content with CGI
* Custrom errors
 
* License
 

Dynamic Content with CGI


The CGI (Common Gateway Interface) defines a way for a web server to interact with external content-generating programs. A plain HTML document retrieved by a web server is static, which means, it doesn't change. A CGI on the other hand, is executed in real time, so it generates dynamic information.

The CGI was created to connect a web server with any program that could provide all kind of services.

The web browser send a request to the web server using HTTP. If the url required by the web browser is a CGI program or a CGI script, the server executes it and wait for a response. The CGI programs can connect with database servers, mail servers and other demons, by using its API (Application Program Interface) that generally uses sockets, shared memory or other methods.

Configuring JSerWeb to permit CGI

In order to get your CGI programs or scripts to work properly, you'll need to have JSerWeb configured to permit CGI execution.

ScriptAlias
The ScriptAlias directive tells JSerWeb that a particular directory is set aside por CGI programs. JSerWeb assumes that every file in this directory is a CGI program, and will attempt to execute it, when that particular resource is requested by a client.

ScriptAlias /cgi-bin/ d:/serweb10/httpdoc/cgi-bin/

The example above tells JSerWeb that any request for a resource beginning with /cgi-bin/ should be served from the directory d:/serweb10/httpdoc/cgi-bin/, and should be treated as a CGI program.

Writing a CGI program

There are two main differences between "regular" programming, and CGI programming.

First, all output from your CGI program must be preceded by a MIME-type header. This is HTTP header that tells the client what sort of content it is receiving. Most of the time, this will looks like:

Content-type: test/html

Second, your output needs to be in HTML, or some other format that a browser will be able to display. Most of the time, this will be HTML, but occasionally you might write a CGI program that outputs a gif image, or other non-HTML content.

Apart from those two things, writing a CGI program will look a lot like any other program that you might write.

Your first CGI program
The following is an example CGI program that prints one line to your browser. Type in the following, save it to a file called
first.pl, and put it in your cgi-bin directory.

#!/user/bin/perl
print "Content-type: text/html\n\n";
print "Hello, world.";


The first line tells JSerWeb (or whatever shell you have to be running under) that this program can be executed by feeding the file to the interpreter found at the location /user/bin/perl. The second line prints the content-type declaration we talked about, followed by two carriage-return new line pairs. This puts a blank line after the header, to indicate the end of the HTTP headers, and the beginning of the body. The third line prints the string "Hello, world.". And that's the end of it.


Powered by JSerWeb
Copyright 2006 Diego Barrientos.
GNU License