Transcript lecture11

9
Chapter
Nine
Compiled Web
Server Programs
9
Chapter Objectives
• Learn about Common Gateway Interface
(CGI)
• Create CGI programs that generate dynamic
Web pages using Visual Basic, and learn the
advantages and disadvantages of using CGI
programs to create dynamic Web pages
• Pass parameter values among CGI programs
9
Chapter Objectives
• Learn about ActiveX dynamic-link libraries
(DLLs) and their advantages and
disadvantages
• Create an ActiveX DLL that generates a
Web page
• Call an ActiveX DLL from an Active Server
Page
9
Common Gateway Interface
• Common Gateway Interface (CGI)
– Protocol that specifies how Web servers and
compiled programs for processing user inputs
that run on the Web server communicate with
each other
– Developed as a protocol for programs that
process data entered into HTML forms and use
these inputs to generate dynamic Web pages
9
Common Gateway Interface
Figure 9-1:
CGI
processing
architecture
9
Processing Inputs and Outputs
in CGI Programs
• In the early days of mainframe computing:
– Standard input (STDIN) referred to a memory
location where user input from the keyboard
was stored
– Standard output (STDOUT) referred to a
memory location where output to be displayed
on the monitor was stored
9
Processing Inputs and Outputs
in CGI Programs
• The terms STDIN and STDOUT are no
longer used to reference keyboard input and
monitor output
• STDIN and STDOUT now define a
standard way of communicating the
locations of inputs and outputs between
different programs
9
Writing a CGI Program Using
Visual Basic
• Visual Basic can be used to create CGI
programs
– VB doesn’t directly support reading from
STDIN and STDOUT
– To create CGI programs using VB, use a VB
standard module named cgi.bas
9
Writing a CGI Program Using
Visual Basic
• The cgi.bas module contains declarations of
Windows API (Application Programming
Interface) functions that enable Visual Basic
to read from STDIN and write to STDOUT
– Windows API is a set of code libraries that
developers can incorporate within Windows
applications written in a variety of languages, to
create standard Windows program components
9
Using the cgi.bas Standard
Module
Table 9-1:
cgi.bas
procedures
and
functions
9
Using the cgi.bas Standard
Module
• The InitializeCGI procedure must be called
before using the other procedures in cgi.bas
– The InitializeCGI procedure can be called only
once at the beginning of your program
• The stdOut function receives a string
containing the program outputs as an input
variable, and writes these outputs to STDOUT
9
Using the cgi.bas Standard
Module
• The DisplayAll procedure is used in the first
CGI program that you will write to display the
names and values of all the form inputs and
environment variables
• The GetInput function receives the name of
an HTML form variable as an input parameter,
and retrieves the value of the variable from
STDIN
9
Debugging CGI Programs in
Visual Basic
• The main source of errors in CGI programs
developed in Visual Basic is concatenation errors,
where the SQL or HTML commands are not
correctly formed as text strings
– Commands with incorrectly formed text strings are
usually easy to spot
• The VB editor displays
– Lines with syntax errors in red
– A message box describing the error when you enter the
code
9
Debugging CGI Programs in
Visual Basic
Figure 9-10:
String
concatenation
error
9
Debugging CGI Programs in
Visual Basic
• After eliminating the “red” errors, there still
might be errors from text strings where the
syntax is correct, but the content is not correct
– This usually happens when you concatenate
variable values with text strings to create SQL
queries
9
Debugging CGI Programs in
Visual Basic
• Breakpoint
– A place in a program where execution is paused
while the program is running, allowing the
developer to examine variable values
– When program execution is paused at a
breakpoint, place the mouse pointer on any
reference variable in the program code, and the
current value of the variable will be displayed
in the ToolTip window
9
Debugging CGI Programs in
Visual Basic
FIGURE 9-13: Code window with execution paused at breakpoint
9
Debugging CGI Programs in
Visual Basic
• Placing the mouse pointer on variable values to
determine their current values works well for
variable values that are fairly short
• When a query string is very long, some of the text
might not appear in the ToolTip window
• Immediate window
– Testing area that is displayed when an execution is
paused while a VB program is running in the VB
Integrated Development Environment
9
Debugging CGI Programs in
Visual Basic
Figure 9-14:
Query to
display
variable
value in the
Immediate
window
9
Creating Hyperlinks to Pass
Parameter Values in CGI Programs
• The hyperlink lists the name of the CGI
program, followed by a question mark and
the parameter list variable name/value pairs
• Each individual variable name/value pair is
separated from the next by an ampersand (&)
9
Creating Hyperlinks to Pass
Parameter Values in CGI Programs
Figure 9-16:
Code to
create
hyperlinks
with URL
parameter
values
9
Creating Hyperlinks to Pass
Parameter Values in CGI Programs
Figure 9-17: Item ID hyperlink URL parameter value
9
Sharing Data Values with Other
CGI Programs
• CGI programs, like Active Server Pages, can pass
data values as URL parameters, as form
parameters, or as cookies
• URL parameters are passed to a CGI program
within a form ACTION tag by forming the
ACTION parameter using the name of the CGI
executable file and a question mark, and then
listing the URL parameter variable name/value
pairs, separated by ampersands
9
Sharing Data Values with Other
CGI Programs
• Form parameter
– Passed to the Web server when an HTML form
is submitted to the Web server by the user
– Form parameters are the names and associated
values of form controls are written to STDIN
when a form is submitted to the Web server
9
Sharing Data Values with Other
CGI Programs
• Cookie
– Data file that is written on the user’s
workstation by a program within a Web page
– Available to any Web page in an application,
regardless of the order in which the Web page is
selected and viewed
9
Advantages and Disadvantages
of Using CGI Programs
• CGI programs can be written in any programming
language that allows users to write values directly
to STDIN and STDOUT on a Web server
• A CGI program can be used with most Web
servers and operating systems
• In contrast, Active Server Pages run only on a
Microsoft Web server
9
Advantages and Disadvantages
of Using CGI Programs
• CGI programs are compiled programs
• The program source code is converted into
machine language when you compile the program
• In contrast, script commands must be converted
into machine language each time the script is run
– For this reason, compiled programs execute (run) much
faster than scripts
9
Advantages and Disadvantages
of Using CGI Programs
• The drawback of CGI programs is that they do not
use Web server resources efficiently
– On a busy Web site using CGI programs, all of the Web
server’s main memory could be consumed trying to
service multiple submissions of the same HTML form,
and the Web server would be very slow in sending
responses back to users
– To solve this problem, vendors are developing products
to allow a single CGI program to service multiple
submissions of the same form
9
Using ActiveX DLLs for
Server-side Web Processing
• A dynamic link library (DLL) is not a standalone program, but it contains code that can be
linked to, or used by, many different programs
– The code modules in a DLL are called “libraries”
because their code can be “checked out “ and used
by many different programs
– A DLL is somewhat different from a conventional
library in that the code from a specific DLL can be
used simultaneously by many different programs
9
Using ActiveX DLLs for
Server-side Web Processing
• ActiveX DLL
– Code module that is stored on the Web server
– Runs in the Web server’s memory space
– Can be used only with Microsoft Web servers
9
Using ActiveX DLLs for
Server-side Web Processing
• ActiveX DLLs have a significant advantage
over CGI programs
– A single copy of an ActiveX DLL can service
an unlimited number of user requests without
starting additional copies of the program
9
Differences Between ActiveX
DLLs and CGI Programs
• Unlike a CGI program, an ActiveX DLL:
– Is not a stand-alone program
– Comprises one or more procedures that must be
called from another program
– Must be entered into the Registry of the Web
server where it will be used, so that other
programs will be able to call the procedures
within the DLL
9
Differences Between ActiveX
DLLs and CGI Programs
• When an ActiveX DLL is called from an ASP, you
must explicitly add the code to send the form inputs as
parameters from the ASP to the DLL in the ASP
command that calls the ActiveX DLL procedure
• When an ActiveX DLL is called from an ASP, it
returns outputs to the calling ASP script either as a
value returned by a function, or by changing the
values of variables that were passed to the ActiveX
DLL as input parameters from the calling program
9
Creating an ActiveX DLL Using
Visual Basic
• A VB ActiveX DLL consists of a class
module that contains multiple functions or
procedures that can be called by programs
that link to the DLL
• Class
– Template from which an object is created
9
Registry Changes Made When
an ActiveX DLL is Compiled
• Normally, Registry entries are made by
programs when they are installed or
modified
• Registry stores information such as:
– The location of program files
– User preferences such as the toolbars that are
displayed in a program
9
Registry Changes Made When
an ActiveX DLL is Compiled
• To view Registry entries on a Windows95,
Windows98, Windows NT, or Windows
2000 workstation, use a utility named
REGEDIT
• REGEDIT enables users to view and change
Registry entries
• Normally, you never need to modify the
Registry manually
9
Registry Changes Made When
an ActiveX DLL is Compiled
Figure 9-41: Registry Editor
9
Registry Changes Made When
an ActiveX DLL is Compiled
• Key
– Parent item that can be a folder, or variable that has an
associated value
• Value
– Text string that is associated with a key variable
• A key folder can have a default value
– Value that is associated with the key folder if a query
does not specify the name of a key within the key folder
9
Retrieving Database Data in an
ActiveX DLL
• You can an ActiveX DLL to perform
operations that you cannot perform in an ASP
• You can also link an ActiveX DLL to an ASP
to perform database operations
– While database operations can be performed
directly within the ASP script code, you can
enhance the execution speed of the ASP if you
perform some operations using a DLL, since the
DLL is compiled prior to execution
9
Retrieving Database Data in an
ActiveX DLL
• When an ActiveX DLL is called from another
program, the DLL remains in the Web server’s
memory until the Web server is rebooted
– You cannot recompile the DLL, because the DLL is
loaded into memory, and is marked as being in use by
the operating system
• To recompile a DLL that has been called by a
program, you must reboot your computer and
recompile the DLL
9
Passing Form Input Parameters
to an ActiveX DLL
• ISAPI enables you to create DLLs that can be called
directly by the Web server
• When you call an ActiveX DLL from an ASP, the
ActiveX DLL has no built-in way to directly read
form inputs
• The alternative is to use the Request.Querystring
property in the ASP to retrieve the form inputs in the
ASP, and then send the parameters to the ActiveX
DLL’s procedure as VB procedure parameters
9
Passing Form Input Parameters
to an ActiveX DLL
• When creating ActiveX DLLs in VB to
generate dynamic Web pages
– Create functions within the ActiveX DLL that
return:
• A text string containing all HTML commands
• Formatted texts
• Retrieved data values to be displayed by the user’s
browser
9
Passing Form Input Parameters
to an ActiveX DLL
• When creating ActiveX DLLs in VB to
generate dynamic Web pages (cont.)
– Create an Active Server Page to:
• Call functions within an ActiveX DLL
• Pass input parameter values to the DLL functions
• Display the function return value, which is a text
string that represents a formatted Web page
9
Passing Form Input Parameters
to an ActiveX DLL
• When creating ActiveX DLLs in VB to
generate dynamic Web pages (cont.)
– To retrieve database data in an ActiveX DLL:
• Create an ADODB connection object to reference a
database
• Use the recordset Executive method to manipulate
data using SQL commands
9
Chapter Summary
• Common Gateway Interface (CGI) is a protocol
that specifies how a Web server communicates
with programs running on the Web server
• CGI is usually used to process data that are
entered into HTML forms using STDIN and
STDOUT
• STDIN and STDOUT are memory locations on a
computer that define a standard way for different
programs to share the values of inputs and outputs
9
Chapter Summary
• A CGI program can be written in any language
that can be read from STDN, write data to
STDOUT, and read environment variables
• CGI programs can use URL parameters, form
parameters, or cookies to share data values with
other CGI programs
• CGI programs can run on most Web servers, and
on a variety of operating systems
9
Chapter Summary
• The main drawback of CGI programs is that they do
not use Web server resources efficiently
• Dynamic-link libraries (DLLs) are collections of
compiled code modules that are called from other
programs
• ActiveX DLLs have a significant advantage over CGI
programs, since a single copy of an ActiveX DLL can
service an unlimited number of user requests without
starting additional copies of the program