CGI Programming
CGI Programming
• Whenever the client browser sends a request to the webserver the CGI programs
send the output back to the web server based on the input provided by the
client-server.
Common Gateway Interface
• 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 look like: Content-type: text/html
• Secondly,
• 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.
CGI Programming
• Whenever the client browser sends a request to the webserver the
CGI programs send the output back to the web server based on the
input provided by the client-server.
• CGI is the standard for programs to interface with HTTP servers.
• CGI programming is written dynamically generating webpages that
respond to user input or webpages that interact with software on the
server
CGI Programming
• Working of CGI
When a request is made by the client-server
to the webserver, the CGI uses external
script files to handle such requests.
• These files could be written in any language.
• The main objective of these script files is to
retrieve the data from the database quickly
and more efficiently.
• These scripts convert the retrieved data into
an Html format that sends the data to these
web servers in Html formatted page.
The architecture of CGI is shown below:
Common Gateway Interface
• What happens when a user clicks a hyperlink to browse a particular web-
page or URL (Uniform Resource Locator).
• The steps are:
• Browser contacts the HTTP web server for demanding the URL
• Parsing the URL
• Look for the filename
• If it finds that file, a request is sent back
• Web browser takes a response from the webserver
• As the server response, it either shows the received file or an error message.
• It may become possible to set-up an HTTP server because when a certain directory is
requested, that file is not sent back; instead, it is executed as a program, and that
program's output is displayed back to your browser.
Configuring CGI
• The steps are:
• Find out which user is running the Web-server
• Check for server configuration to see if you can run the scripts in a particular
directory
• Check for file's permission
• Make a clear assurance that scripts you made are readable and executable by
the webserver user
• Make sure the Python-Script's first line refers to a webserver that the
interpreter can run
Input to the Common Gateway Interface
• Input to CGI:
• environment variables,
• accessing from input
• When a CGI program is called, the information that is made available to it
can be roughly broken into three groups:
• Information about the client, server, and user
• Form data that the user supplied
• Additional pathname information
• Most information about the client, server, or user is placed in CGI
environment variables.
• Form data is either incorporated into an environment variable, or is
included in the “body” of the request.
• And extra path information is placed in environment variables.
Input to the Common Gateway Interface
• Programs can access this information as they would any environment
variable (e.g., via the %ENV associative array in Perl).
Environment Variable Description
GATEWAY_INTERFACE The revision of the Common Gateway Interface that the server uses.
SERVER_NAME The server's hostname or IP address.
SERVER_SOFTWARE The name and version of the server software that is answering the client request.
SERVER_PROTOCOL The name and revision of the information protocol the request came in with.
SERVER_PORT The port number of the host on which the server is running.
REQUEST_METHOD The method with which the information request was issued.
PATH_INFO Extra path information passed to a CGI program.
PATH_TRANSLATED The translated version of the path given by the variable PATH_INFO.
SCRIPT_NAME The virtual path (e.g., /cgi-bin/program.pl) of the script being executed.
DOCUMENT_ROOT The directory from which Web documents are served.
QUERY_STRING The query information passed to the program. It is appended to the URL with a “?”.
REMOTE_HOST The remote hostname of the user making the request.
REMOTE_ADDR The remote IP address of the user making the request.
AUTH_TYPE The authentication method used to validate a user.
REMOTE_USER The authenticated name of the user.
The user making the request. This variable will only be set if NCSA IdentityCheck flag is enabled, and
REMOTE_IDENT
the client machine supports the RFC 931 identification scheme (ident daemon).
CONTENT_TYPE The MIME type of the query data, such as “text/html”.
The length of the data (in bytes or the number of characters) passed to the CGI program through
CONTENT_LENGTH
standard input.
HTTP_FROM The email address of the user making the request. Most browsers do not support this variable.
HTTP_ACCEPT A list of the MIME types that the client can accept.
HTTP_USER_AGENT The browser the client is using to issue the request.
HTTP_REFERER The URL of the document that the client points to before accessing the CGI program.
Here's a simple Perl CGI script that uses environment variables to display various information
about the server:
#!/usr/local/bin/perl
<HTML>
<HEAD><TITLE>About this Server</TITLE></HEAD>
<BODY><H1>About this Server</H1>
<HR><PRE>
Server Name: $ENV{'SERVER_NAME'}<BR>
Running on Port: $ENV{'SERVER_PORT'}<BR>
Server Software: $ENV{'SERVER_SOFTWARE'}<BR>
Server Protocol: $ENV{'SERVER_PROTOCOL'}<BR>
CGI Revision: $ENV{'GATEWAY_INTERFACE'}<BR>
<HR></PRE>
</BODY></HTML>
• By now, you should be reasonably comfortable designing CGI programs that create
simple virtual documents, like this one:
• #!/usr/local/bin/perl
• print "Content-type: text/html", "\n\n";
• print "<HTML>", "\n";
• print "<HEAD><TITLE>Simple Virtual HTML Document</TITLE></HEAD>", "\n";
• print "<BODY>", "\n";
• print "<H1>", "Virtual HTML", "</H1>", "<HR>", "\n";
• print "Hey look, I just created a virtual (yep, virtual) HTML document!", "\n";
• print "</BODY></HTML>", "\n";
• exit (0);
<HTML>
<HEAD><TITLE>About this Server</TITLE></HEAD>
<BODY><H1>About this Server</H1>
<HR><PRE>
Server Name: www.whatever.com
Running on Port: 80
Server Software: NCSA/1.4.2
Server Protocol: HTTP/1.0
CGI Revision: CGI/1.1
<HR></PRE>
</BODY></HTML>
Output from CGI : CGI and response headers
HTTP header
• we have taken the line that outputs "Content-type" for granted.
• But this is only one type of header that CGI programs can use.
• "Content-type" is an HTTP header that contains a MIME content type
describing the format of the data that follows.
• Other headers can describe:
• The size of the data
• Another document that the server should return (that is, instead of returning
a virtual document created by the script itself)
• HTTP status codes
• Table provides a quick listing of all the HTTP headers you might find
useful.
HTTP header
Header Description
Content-length The length (in bytes) of the output stream. Implies binary data.
Content-type The MIME content type of the output stream.
Date and time when the document is no longer valid and should be reloaded by
Expires
the browser.
Location Server redirection (cannot be sent as part of a complete header).
Pragma Turns document caching on and off.
Status Status of the request (cannot be sent as part of a complete header).
• https://www.oreilly.com/openbook/cgi/
• https://www.tutorialspoint.com/python/python_cgi_programming.ht
m#:~:text=First%20CGI%20Program,command%20to%20make%20file
%20executable.
Accessing Form Input
• Forms provide a way to get input from users and supply it to a CGI
program
Accessing Form Input: Query Strings
• One way to send form data to a CGI program is by appending the
form information to the URL, after a question mark.
• You may have seen URLs like the following:
http://some.machine/cgi-bin/name.pl?fortune
“?” character is known as a query string
When the server is passed a URL with a query string, it calls the CGI
program identified in the first part of the URL (before the “?”) and then
stores the part after the “?” in the environment variable
QUERY_STRING.
The following is a CGI program called name.pl that uses query
information to execute one of three possible UNIX commands.
Query Strings
The METHOD=GET attribute to the <FORM> tag in part
determines how the data is passed to the server
</body>
</html> Welcome.php
<html>
<body>
</body>
</html>
Form Processing Using PHP : GET Method
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html> Welcome_get.php
<html>
<body>
</body>
</html>
JAVA
The principles for creating Java programming were
"Simple, Robust, Portable, Platform-independent, Secured, High Performance,
Multithreaded, Architecture Neutral, Object-Oriented, Interpreted, and Dynamic".
Java Attributes
• Familiar, Simple, Small
• Compiled and Interpreted
• Platform-Independent and Portable
• Object-Oriented
• Robust and Secure
• Distributed
• Multithreaded and Interactive
• High Performance
• Dynamic and Extensible
Java is Compiled and Interpreted
Hardware and
Programmer
Operating System
54
History of java
originally designed for interactive television,
James Gosling, Patrick Naughton, Chris Warth, Mike Sheridan and Ed
Frank initiated the Java language project in June 1991.
The idea was to develop a language which was platform-independent
and which could create embedded software for consumer electronic
devices,
It took 18 months to develop and had an initial name as Oak,
Renamed to Java in 1995, due to copyright issues.
Java originally developed by James Gosling at Sun Microsystems and
released in 1995.
History of Java
• The small team of sun engineers called Green Team. Firstly, it was called "Greentalk" by
James Gosling, and the file extension was .gt.
• After that, it was called Oak and was developed as a part of the Green project.
• in 1995, Oak was renamed as "Java" because it was already a trademark by
Oak Technologies.
• Java is an island in Indonesia where the first coffee was produced (called Java coffee).
• Java was developed by James Gosling, who is known as the father of Java, in 1995.
James Gosling and his team members started the project in the early '90s.
• Initially it was designed for small, embedded systems in electronic appliances like set-
top boxes.
• However, it was best suited for internet programming. Later, Java technology was
incorporated by Netscape.
History of Java
• Java is just a name, not an acronym.
• Initially developed by James Gosling at Sun Microsystems (which is
now a subsidiary of Oracle Corporation) and released in 1995.
• In 1995, Time magazine called Java one of the Ten Best Products of
1995.
• JDK 1.0 was released on January 23, 1996.
• After the first release of Java, there have been many additional features
added to the language.
• Now Java is being used in Windows applications, Web applications, enterprise
applications, mobile applications, cards, etc.
• Each new version adds new features in Java.
Why Java Programming named "Java"?
• Why had they chose the name Java for Java language? The team
gathered to choose a new name. The suggested words were
"dynamic", "revolutionary", "Silk", "jolt", "DNA", etc. They wanted
something that reflected the essence of the technology:
revolutionary, dynamic, lively, cool, unique, and easy to spell, and fun
to say.
• According to James Gosling, "Java was one of the top choices along
with Silk". Since Java was so unique, most of the team members
preferred Java than other names.
• It is a kind of espresso bean. Java name was chosen by James Gosling
while having a cup of coffee nearby his office.
Versions of java
• 1995 version 1.0:
– The Java development kit was released for free by the sun
– 8-Packages 212-Classes
– Microsoft and other companies licensed Java
• 1997 version 1.1:
– 23 -Packages 504-Classes
– Improvement include better event handling inner classes , improved JVM.
– Microsoft developed its own 1.1 compatible Java Virtual
Machine for Internet Explorer
– Many browsers in use are still compatible only with 1.1
1999 version 1.2:
-It is also called as the Java 2 platform
-59 Packages -1520 Classes
-Code & tools distributed as the SDK
-A Java foundation class based on swings for improved graphics and user interfaces
-Collection API included list sets and hash map
Versions of java…
2000 VERSION 1.3:
- 76 Packages - 1842 Classes
- Java Sound (API for Digital & MIDI Sound)
•2002 VERSION 1.4:
- 135 Packages - 2991 Classes
- Improved XML support etc..,
•2004 VERSION 5.0 (1.5):
- 165 Packages - over 3000 Classes
- Faster startup metadata formatted Output
- Generic to operate on objects of various types
2006 Java SE 6:
– Scripting language support
2011 Java SE 7:
– JVM Support for dynamic language
– String in switch
– Allowing underscores in numeric literals
Versions of java…
2014 Java SE 8:
– for Each() method in Iterable interface.
- default and static methods in Interfaces.
- Functional Interfaces and Lambda Expressions.
2017 Java SE 9:
-Stream API Improvements
-Multi-Resolution Image API
March 2022 Java SE 18
Java Version History
1.Java SE 7 (28th July 2011)
1.JDK Alpha and Beta 2.Java SE 8 (18th Mar 2014)
(1995) 3.Java SE 9 (21st Sep 2017)
4.Java SE 10 (20th Mar 2018)
2.JDK 1.0 (23rd Jan 1996) 5.Java SE 11 (September 2018)
6.Java SE 12 (March 2019)
3.JDK 1.1 (19th Feb 1997) 7.Java SE 13 (September 2019)
8.Java SE 14 (Mar 2020)
4.J2SE 1.2 (8th Dec 1998) 9.Java SE 15 (September 2020)
5.J2SE 1.3 (8th May 2000) 10.Java SE 16 (Mar 2021)
11.Java SE 17 (September 2021)
6.J2SE 1.4 (6th Feb 2002) 12.Java SE 18 (to be released by March 2022)
• JVM generates a .class(Bytecode) file, and that file can be run in any OS, but JVM
• should have in OS because JVM is platform dependent.
Java virtual machine (JVM)
• The main difference between C++ and Java is that C++ is only a compiled language
while Java is both compiled and interpreted.
• The C++ compiler converts the source code into machine code and therefore, it is
platform dependent.
• However, Java source code is converted into bytecode by its compiler and following
that, the Java interpreter executes this bytecode at runtime and produces output.
The fact that Java is interpreted is the reason why it is platform-independent.
Structure of Java Program
Java program structure
• the way to write a java program or general format.
Section Description
Documentation Section You can write a comment in this section. These are optional
Package statement You can create a package with any name. A package is a group of classes that are defined by a name. That is, if
you want to declare many classes within one element, then you can declare it within a package. It is an optional
part of the program, Here, the package is a keyword that tells the compiler that package has been created.It is
declared as:
package package_name;
Import statements This line indicates that if you want to use a class of another package, then you can do this by importing it
directly into your program. Example: import calc.add;
Interface statement Interfaces are like a class that includes a group of method declarations. It's an optional section and can be used
when programmers want to implement multiple inheritances within a program.
Class Definition A Java program may contain several class definitions. Classes are the main and essential elements of any Java
program.
Main Method Class Every Java stand-alone program requires the main method as the starting point of the program. This is an
essential part of a Java program. There may be many classes in a Java program, and only one class defines the
main method. Methods contain data type declaration and executable statements.
Classes and Objects
• The class is the unit of programming
• A Java program is a collection of classes
• Each class definition (usually) in its own .java file
• The file name must match the class name
• A class describes objects (instances)
• Describes their common characteristics: is a blueprint
• Thus all the instances have these same characteristics
• These characteristics are:
• Data fields for each object
• Methods (operations) that do work on the objects
//declaration statement
int number;
//expression statement
number = 412;
//control flow statement
if (number > 10 )
{
//expression statement
System.out.println(number + " is greater than 100");
}
public class Student implements Cloneable { public class Calculattion {
public static void main(String[] args) { public static void main(String[] args) {
int num = 2; // TODO Auto-generated method stub
switch (num){
int sum = 0;
case 0:
System.out.println("number is 0"); for(int j = 1; j<=10; j++) {
break; sum = sum + j;
case 1: }
System.out.println("number is 1"); System.out.println("The sum of first 10 natural numbers is " +
break; sum);
default: }
System.out.println(num); }
}
For Statement
}
} public class Calculation {
SWITCH Case public static void main(String[] args) {
public class Calculation { // TODO Auto-generated method stub
public static void main(String[] args) { String[] names = {"Java","C","C++","Python","JavaScript"};
// TODO Auto-generated method stub
int i = 0; System.out.println("Printing the content of the array names
System.out.println("Printing the list of first 10 even :\n");
numbers \n"); for(String name:names) {
while(i<=10) {
System.out.println(name);
System.out.println(i);
i = i + 2;
}
} } } }
WHILE Statement }
public class Calculation { /A Java Program to demonstrate the use of if-
public static void main(String[] args) { else statement.
// TODO Auto-generated method stub //It is a program of odd and even number.
int i = 0; public class IfElseExample {
System.out.println("Printing the list of first 10 public static void main(String[] args) {
even numbers \n");
do {
//defining a variable
System.out.println(i);
int number=13;
i = i + 2;
//Check if the number is divisible by 2 or not
}while(i<=10);
if(number%2==0){
} System.out.println("even number");
} }else{
DO WHILE Statement System.out.println("odd number");
}
}
}
https://www.javatpoint.com/java-if-else IF ELSE Statement
https://www.javatpoint.com/control-flow-in-java
public class BreakExample {
public class ContinueExample {
public static void main(String[] args) {
public static void main(String[] args) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
for(int i = 0; i<= 10; i++) {
System.out.println(i);
for(int i = 0; i<= 2; i++) {
if(i==6) {
break;
for (int j = i; j<=5; j++) {
}
}
if(j == 4) {
}
continue;
}
}
System.out.println(j);
}
}
}
}
CheckPalindromeNumber.java
import java.util.Scanner;
//imports the Scanner class of the java.util package
public class CheckPalindromeNumber { //class definition
public static void main(String args[]) { //main method
int r, s=0, temp; //variables to be used in program
int x;
//It is the number variable to be checked for palindrome
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to check: ");
//reading a number from the user
x=sc.nextInt();
//logic to check if the number id palindrome or not
temp=x;
while(x>0) {
r=x%10; //finds remainder
s=(s*10)+r;
x=x/10;
}
if(temp==s)
System.out.println("The given number is palindrome.");
else
System.out.println("The given number is not palindrome."); }
}