Home | | Internet Programming | Environment Variables

Chapter: Web or internet Programming : Basic Network and Web Concepts

Environment Variables

In order to pass data from the server to the script, the server uses command line arguments along with environment variables.

Environment Variables

 

       In order to pass data from the server to the script, the server uses command line arguments along with environment variables.

       The Environment Variables are set when the server executes a CGI Script. 

       Environment Variables allow the CGI Script to reference variables that might be wanted for the Script output.

       There are two types of environment variables:

       Non-Request specific variables - those set for every request 

       Request specific variables - those that are dependent on the request being fulfilled by the CGI Script

 

Typical Environment Variables

SERVER_SOFTWARE = Apache/1.3.14

SERVER_NAME = www.ncsi.iisc.ernet.in

GATEWAY_INTERFACE = CGI/1.1

SERVER_PROTOCOL = HTTP/1.0

 

SERVER_PORT = 80

REQUEST_METHOD = GET

 

HTTP_ACCEPT = 'image/gif, image/x-xbitmap, image/jpeg, */*'

SCRIPT_NAME = /cgi-bin/environment-example

 

QUERY_STRING =

REMOTE_HOST = ece.iisc.ernet.in

 

REMOTE_ADDR = 144.16.64.3

 

Features

 

‖ CGI is a standard protocol for interfacing external application software with a Web server. It has been widely used for implementing dynamic generated pages on the Web.

 

                      Advantages

 

       They are language independent .CGI programs can be written in any language that allows one to write normal programs since they are executed in the same way as the normal programs.

 

       it's a very simple interface . It's not necessary to have any special library to create a CGI program, or write programs using a particular API. Instead, CGI programs rely on the standard concepts of standard input, standard output, and environment variables to communicate with the Web server.

 

Disadvantages

 

       CGI programs are slow since they need to fork a new process for every HTTP request and the database connection must be reopened for the next instance of the program, which is quite costly

 

Differences between CGI and Servlets

The basic concept behind the CGI and Servlets is :

 

       CGI is a process based (Heavy weight) and Servlet is a Thread based (Light weight). CGI creates a process for every execution. So this is a time taking process whereas servlet executes by using threading so this is light weight process. Hence Servlets are in more powerful.

 

       A process is created for every client request or call in CGI. it increases memory space.

 

       Servlet classes can be moved from one Servlet compatible webserver to another very easily. CGI programs or scripts on the other hand may be platform dependent need to be recompiled or even web server dependent.

 

Example program:

 

myscript.html

 

<HTML>

<BODY>

 

<FORM METHOD="POST" ACTION="/cgi-bin/myscript.cgi"> <PRE>

 

First Name <INPUT TYPE="text" NAME="fname" MAXLENGTH=15 SIZE=15> Last Name <INPUT TYPE="text" NAME="lname" MAXLENGTH=20 SIZE=20> E-Mail Addr <INPUT TYPE="text" NAME="email" MAXLENGTH=35 SIZE=35>

 

<INPUT TYPE="submit" VALUE="Send Mail!"> <INPUT TYPE="reset" value=" Clear-Form">

</PRE>

</FORM>

</BODY>

</HTML>

 

myscript.cgi

 

#!/usr/local/bin/perl read(STDIN,$temp,$ENV{'CONTENT_LENGTH'}); @pairs=split(/&/,$temp);

foreach $item(@pairs)

 

{

 

($key,$content)=split(/=/,$item,2);

 $content=~tr/+/ /;

$content=~s/%(..)/pack("c",hex($1))/ge;

$fields{$key}=$content;

 

}

 

print "Content-type: text/html\n\n";

print "<HTML>\n";

 

print "<BODY BGCOLOR=#FFFFFF>\n";

print "<CENTER>\n";

print "THANK YOU<BR>\n";

 

print "$fields{fname} $fields{lname}</BR>";

print "I will write<BR>\n";

print "you at<BR>\n";

 

print "$fields{email}<BR>\n";

print "</CENTER>\n";

print "</BODY></HTML>";

 



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Basic Network and Web Concepts : Environment Variables |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.