2. What is CGI?
Definition of CGI
CGI (Common Gateway Interface) is a standard
protocol for web servers to execute programs that
execute like console applications running on a
server that generates web pages dynamically.
CGI was one of the first methods used to create dynamic content on
web pages, and though its use has declined in favor of other
technologies, it remains an important tool in web development.
Purpose of CGI
The purpose of CGI is to provide an interface
between the client (typically a web browser) and
the server to generate dynamic web pages and
interact with the user.
Evolution of CGI
3. Understanding CGI
Concept of CGI
CGI stands for Common Gateway
Interface, which is a standard protocol for
web servers to execute programs that
execute like console applications running
on a server.
Understanding how CGI is integrated with Java for
web development and server-side programming.
Functionality of CGI
CGI allows web servers to interact dynamically
with users through web forms and other access
methods.
Integration with Java
5. Setting Up CGI in Java
Configuring the web server to support CGI and Java for hosting applications and processing server-
side scripts.
Setting up the Java environment to ensure compatibility and seamless execution of CGI programs.
Server Configuration
Steps for proper execution of CGI scripts within the Java server environment.
Java Environment
Script Execution
6. Creating a Simple CGI Program in Java
Select a suitable integrated development
environment (IDE) for Java, such as
Eclipse or IntelliJ IDEA, to create CGI
programs.
Create a Java program that follows the
CGI protocol, including processing
HTTP request parameters and
generating the appropriate HTTP
response.
Choosing a Development
Environment
Compile the Java program into a .class file
using a Java compiler like javac, ensuring
that it is executable on the web server's
environment.
Writing the CGI Program Compiling the Program
7. Executing the CGI Program
Trigger the CGI program by making an HTTP request to the
server with the proper parameters, such as through a web
form or URL query.
Upon receiving the request, the server executes the CGI
program and processes the input data to generate the dynamic
content for the client.
The CGI program sends the generated content back to the
server, which then delivers it to the client to be displayed in the
web browser.
Invoking the CGI Program
Processing the Request
Returning the Response
8. BASIS FOR
COMPARISON
CGI SERVLET
Basic Programs are written in the native OS. Programs employed using Java.
Platform dependency Platform dependent Does not rely on the platform
Creation of process Each client request creates its own process. Processes are created depending on the type of the client
request.
Conversion of the script Present in the form of executables (native to the server
OS).
Compiled to Java Bytecode.
Runs on Separate process JVM
Security More vulnerable to attacks. Can resist attacks.
Speed Slower Faster
Processing of script Direct Before running the scripts it is translated and compiled.
Portability Can not be ported Portable
9. Example
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class CGIProgram {
public static void main(String[] args) {
// Read input from CGI request
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input;
StringBuilder requestContent = new StringBuilder();
try {
while ((input = reader.readLine()) != null && input.length() != 0) {
requestContent.append(input).append("n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
10. // Parse input data (assuming it's in query string format)
String[] queryParams = requestContent.toString().split("&");
Map<String, String> params = new HashMap<>();
for (String param : queryParams)
{ S
tring[] keyValue = param.split("=");
if (keyValue.length == 2)
{
params.put(keyValue[0], keyValue[1]);
}
} // Generate HTML response
String name = params.get("name");
String response = "<html><head><title>CGI Program Response</title></head><body>";
if (name != null && !name.isEmpty())
{
response += "<h2>Hello, " + name + "!</h2>";
} else
{ response += "<h2>Hello, Stranger!</h2>";
}
response += "</body></html>";
// Output response
System.out.println("Content-type: text/htmln");
System.out.println(response); } }