0% found this document useful (0 votes)
40 views2 pages

Common Gateway Interface: Servlets V/s CGI

Uploaded by

nareshvarshney29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

Common Gateway Interface: Servlets V/s CGI

Uploaded by

nareshvarshney29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Common Gateway Interface

Common Gateway Interface

Common Gateway Interface (CGI) is a standard method used to generate dynamic content on Web
pages and Web applications. CGI, when implemented on a Web server, provides an interface
between the Web server and programs that generate the Web content. These programs are known as
CGI scripts or simply CGIs; they are usually written in a scripting language, but can be written in
any programming language.

Each Web server runs HTTP server software, which responds to requests from Web browsers.
Generally the HTTP server has a directory (folder), which is designated as a document collection —
files that can be sent to Web browsers connected to this server. For example, if the Web server has
the domain name example.com, and its document collection is stored at /usr/local/apache/htdocs in
the local file system, then the Web server will respond to a request for
http://example.com/index.html by sending out the (pre-written) file at
/usr/local/apache/htdocs/index.html.
If a Web browser requests a URL that points to a file within the CGI directory (e.g.,
http://example.com/cgi-bin/printenv.pl), then, instead of simply sending that file
(/usr/local/apache/htdocs/cgi-bin/printenv.pl) to the Web browser, the HTTP server runs the
specified script and passes the output of the script to the Web browser.

The CGI system also allows the Web browser to send information to the script via the URL or an
HTTP POST request. If a slash and additional directory name(s) are appended to the URL
immediately after the name of the script, then that path is stored in the PATH_INFO environment
variable before the script is called. If parameters are passed to the script via an HTTP POST or GET
request (e.g., a question mark appended to the URL, followed by param=value pairs), then those
parameters are stored in the QUERY_STRING environment variable before the script is called. The
script can then read these environment variables and adapt to the Web browser's request.

Servlets V/s CGI:

The first advantage of servlets over CGI is in its platform independence. Servlets can run on any
operating system just as long as a JVM is installed, which means that you would not be having any
problem even if you choose to switch operating systems. With CGI, switching operating system is a
difficult and laborious process as you would need to recompile the programs in the new operating
system.

Since you are running independent programs in CGI, they create their own process when they are
executed, something that does not happen with servlets as they just share in the memory space of
the JVM. This can lead to problems relating to overhead, especially when you increase the number

1|Page
Common Gateway Interface

of users exponentially. It also creates vulnerability issues as the program is not controlled in any
way once it is run on the server.

The more common method when using CGI is via scripts. This reduces the time needed in creating
programs and are generally more secure. With CGI, you can run scripts right away, while servlets,
you would need to translate the script into Java and compile it into a servlet which adds a little bit to
the loading time.

2|Page

You might also like