Comman Gateway interface Java cgi pro.pptx

29 views 11 slides Mar 18, 2024
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Java cgi program


Slide Content

CGI Java Programming

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

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

Difference Between CGI and Servlet    

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

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

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

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

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 ();         }    }

/ / 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/html\n");  System.out.println (response); } }

Html file: <!DOCTYPE html> <html> <head>     <title>CGI Form</title> </head> <body>     <h2>Enter Your Name</h2>     <form action="/cgi-bin/CGIProgram" method="post">         <input type="text" name="name" required>         <input type="submit" value="Submit">     </form> </body> </html>