Web Enabled Business Process - 3
Web Enabled Business Process - 3
Notes
Structure:
3.1 What is ASP?
3.1.1 ASP Compatibility
3.1.2 What is an ASP File?
3.1.3 How Does ASP Differ from HTML?
3.1.4 What Can ASP Do for You?
3.2 IIS – Internet Information Server
3.3 PWS – Personal Web Server
3.3.1 Windows Web Server Versions
3.4 Programming for Database Connectivity
3.4.1 Create a Data Source Name
3.4.2 For a Database Program
3.4.3 For a Database Server
3.5 Troubleshooting
3.6 Store and Retrieve Session Variables
3.6.1 Remove Session Variables
3.7 Summary
3.8 Check Your Progress
3.9 Questions and Exercises
3.10 Key Terms
3.11 Check Your Progress: Answers
3.12 Case Study
3.13 Further Readings
Objectives
After going through this unit, you should be able to know:
Ɣ Dynamically edit, change, or add any content of a Web page
Ɣ Respond to user queries or data submitted from HTML forms
Ɣ Access any data or databases and return the results to a browser
Ɣ Customize a Web page to make it more useful for individual users
Ɣ The advantages of using ASP instead of CGI and Perl, are those of simplicity and
speed
Ɣ Provide security – since ASP code cannot be viewed from the browser
Ɣ Clever ASP programming can minimize the network traffic
Ɣ A Case Study based on this Unit
15. Highlight Active Server Pages and click the Allow button.
Notes
16. ASP is now active!
Example
<!DOCTYPE html>
<html>
<body>
<%
response.write(“Hello World!”)
%>
</body>
</html>
There is also a shorthand method for the response.write command. The following
example also sends the text “Hello World” to the browser:
Example
<!DOCTYPE html>
<html>
<body>
<%
=“Hello World!”
%>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<%
response.write(“My first ASP script!”)
%>
</body>
</html>
Examples
A form with method=“get”
How to interact with the user, with the Request.QueryString command.
A form with method=“post”
How to interact with the user, with the Request.Form command.
A form with radio buttons
How to Interact with the User, through Radio Buttons, with the Request.Form Notes
Command?
User Input
The Request object can be used to retrieve user information from forms.
User input can be retrieved with the Request.QueryString or Request.Form
command.
Request.QueryString
The Request.QueryString command is used to collect values in a form with
method=“get”.
Information sent from a form with the GET method is visible to everyone (it will be
displayed in the browser’s address bar) and has limits on the amount of information to
send.
Form Validation
User input should be validated on the browser whenever possible (by client scripts).
Browser validation is faster and reduces the server load.
You should consider server validation if the user input will be inserted into a
database. A good way to validate a form on the server is to post the form to itself, instead
of jumping to a different page. The user will then get the error messages on the same
page as the form. This makes it easier to discover the error.
Response Object
The ASP Response object is used to send output to the user from the server. Its
collections, properties, and methods are described below:
Collections
Collection Description
Cookies Sets a cookie value. If the cookie does not exist, it will be created, and take
the value that is specified
Properties
Property Description
Buffer Specifies whether to buffer the page output or not
CacheControl Sets whether a proxy server can cache the output generated by ASP or not
Charset Appends the name of a character-set to the content-type header in the
Response object
ContentType Sets the HTTP content type for the Response object
Expires Sets how long (in minutes) a page will be cached on a browser before it
expires
ExpiresAbsolute Sets a date and time when a page cached on a browser will expire
IsClientConnected Indicates if the client has disconnected from the server
Pics Appends a value to the PICS label response header
Status Specifies the value of the status line returned by the server
Methods Notes
Method Description
AddHeader Adds a new HTTP header and a value to the HTTP response
AppendToLog Adds a string to the end of the server log entry
BinaryWrite Writes data directly to the output without any character conversion
Clear Clears any buffered HTML output
End Stops processing a script, and returns the current result
Flush Sends buffered HTML output immediately
Redirect Redirects the user to a different URL
Write Writes a specified string to the output
Request Object
When a browser asks for a page from a server, it is called a request. The Request
object is used to get information from a visitor. Its collections, properties, and methods
are described below:
Collections
Collection Description
ClientCertificate Contains all the field values stored in the client certificate
Cookies Contains all the cookie values sent in a HTTP request
Form Contains all the form (input) values from a form that uses the post method
QueryString Contains all the variable values in a HTTP query string
ServerVariables Contains all the server variable values
Properties
Property Description
TotalBytes Returns the total number of bytes the client sent in the body of the request
Methods
Method Description
BinaryRead Retrieves the data sent to the server from the client as part of a post
request and stores it in a safe array
Application Object
An application on the Web may consists of several ASP files that work together to
perform some purpose. The Application object is used to tie these files together.
The Application object is used to store and access variables from any page, just like
the Session object. The difference is that ALL users share ONE Application object (with
Sessions, there is ONE Session object for EACH user).
Collections
Collection Description
Contents Contains all the items appended to the application through a script
command
StaticObjects Contains all the objects appended to the application with the HTML
<object> tag
Methods
Method Description
Contents.Remove Deletes an item from the Contents collection
Contents.RemoveAll() Deletes all items from the Contents collection
Lock Prevents other users from modifying the variables in the Application
object
Unlock Enables other users to modify the variables in the Application object
(after it has been locked using the Lock method)
Events
Event Description
Application_OnEnd Occurs when all user sessions are over, and the application ends
Application_OnStart Occurs before the first new session is created (when the Application
object is first referenced)
Session Object
When you are working with an application on your computer, you open it, do some
changes and then you close it. This is much like a Session. The computer knows who
you are. It knows when you open the application and when you close it. However, on the
internet, there is one problem: the web server does not know who you are and what you
do, because the HTTP address doesn’t maintain state.
ASP solves this problem by creating a unique cookie for each user. The cookie is
sent to the user’s computer and it contains information that identifies the user. This
interface is called the Session object.
The Session object stores information about, or change settings for a user session.
Variables stored in a Session object hold information about one single user, and are
available to all pages in one application. Common information stored in session variables
are name, ID, and preferences. The server creates a new Session object for each new
user, and destroys the Session object when the session expires.
The Session object’s collections, properties, methods, and events are described
below:
Collections Notes
Collection Description
Contents Contains all the items appended to the session through a script command
StaticObjects Contains all the objects appended to the session with the HTML <object>
tag
Properties
Property Description
CodePage Specifies the character set that will be used when displaying dynamic
content
LCID Sets or returns an integer that specifies a location or region. Contents like
date, time, and currency will be displayed according to that location or
region
SessionID Returns a unique ID for each user. The unique ID is generated by the server
Timeout Sets or returns the timeout period (in minutes) for the Session object in this
application
Methods
Method Description
Abandon Destroys a user session
Contents.Remove Deletes an item from the Contents collection
Contents.RemoveAll() Deletes all items from the Contents collection
Events
Event Description
Session_OnEnd Occurs when a session ends
Session_OnStart Occurs when a session starts
Server Object
The ASP Server object is used to access properties and methods on the server. Its
properties and methods are described below:
Properties
Property Description
ScriptTimeout Sets or returns the maximum number of seconds a script can run before it
is terminated
Methods
Method Description
CreateObject Creates an instance of an object
Execute Executes an ASP file from inside another ASP file
GetLastError() Returns an ASPError object that describes the error condition that occurred
HTMLEncode Applies HTML encoding to a specified string
Example
The following example illustrates how to connect to the Microsoft Access NorthWind
sample database by using an ASP Script.
NOTE: This example assumes the default installation of Windows 2000 on drive C,
as well as the default installation of Microsoft Access along with the NorthWind sample
database.
3.5 Troubleshooting
If you experience difficulty connecting to a database by using ASP Web pages,
verify that you have sufficient permissions to access the database:
z Verify that the DSN is using an account with sufficient permissions to access
the database.
z If you attempt to connect to a SQL Server over a network, ensure that you use
a domain account as your anonymous IIS account.
z If you attempt to connect to a Microsoft Access database, ensure that the IIS
account has Write permissions to the folder in which the database is stored.
This is to enable the creation of a temporary file when users access the
database.
ASP Cookies
A cookie is often used to identify a user.
Examples Notes
Welcome cookie
How to create a Welcome cookie?
What is a Cookie?
A cookie is often used to identify a user. A cookie is a small file that the server
embeds on the user’s computer. Each time the same computer requests a page with a
browser, it will send the cookie too. With ASP, you can both create and retrieve cookie
values.
How to Create a Cookie?
The “Response.Cookies” command is used to create cookies.
Note: The Response.Cookies command must appear BEFORE the <html> tag.
In the example below, we will create a cookie named “firstname” and assign the
value “Alex” to it:
<%
Response.Cookies(“firstname”)=“Alex”
%>
It is also possible to assign properties to a cookie, like setting a date when the
cookie should expire:
<%
Response.Cookies(“firstname”)=“Alex”
Response.Cookies(“firstname”).Expires=#May 10,2012#
%>
How to Retrieve a Cookie Value?
The “Request.Cookies” command is used to retrieve a cookie value.
In the example below, we retrieve the value of the cookie named “firstname” and
display it on a page:
<%
fname=Request.Cookies(“firstname”)
response.write(“Firstname=” & fname)
%>
Output: Firstname=Alex
2. Use a Form
You can use a form. The form passes the user input to “welcome.asp” when the
user clicks on the Submit button:
<form method=“post” action=“welcome.asp”>
First Name: <input type=“text” name=“fname” value=“”>
Last Name: <input type=“text” name=“lname” value=“”>
<input type=“submit” value=“Submit”>
</form>
Retrieve the values in the “welcome.asp” file like this:
<%
fname=Request.form(“fname”)
lname=Request.form(“lname”)
response.write(“<p>Hello ” & fname & “ ” & lname & “!</p>”)
response.write(“<p>Welcome to my Website!</p>”)
%>
The example below specifies a text-only version of the page if the user has a low
Notes
screen resolution:
<%If Session(“screenres”)=“low” Then%>
This is the text version of the page.
<%Else%>
This is the multimedia version of the page.
<%End If%>
3.6.1 Remove Session Variables
The Contents collection contains all session variables.
It is possible to remove a session variable with the Remove method.
The example below removes the session variable “sale” if the value of the session
variable “age” is lower than 18:
<%
If Session.Contents(“age”)<18 then
Session.Contents.Remove(“sale”)
End If
%>
To remove all variables in a session, use the RemoveAll method:
<%
Session.Contents.RemoveAll()
%>
Loop Through the Contents Collection
The Contents collection contains all session variables. You can loop through the
Contents collection, to see what’s stored in it:
<%
Session(“username”)=“Donald Duck”
Session(“age”)=50
dim i
For Each i in Session.Contents
Response.Write(i & “<br>”)
Next
%>
Result:
username
age
If you do not know the number of items in the Contents collection, you can use the
Count property:
<%
dim i
dim j
j=Session.Contents.Count
Response.Write(“Session variables: ” & j)
For i=1 to j
3.7 Summary
An Active Server Page (ASP) is an HTML page that includes one or more scripts
(small embedded programs) that is processed on a Microsoft Web server before the
page is sent to the user. An ASP is somewhat similar to a server-side include or a
common gateway interface (CGI) application in that all involve programs that run on the
server, usually tailoring a page for the user. Typically, the script in the Web page at the
server uses input received as the result of the user’s request for the page to access data
from a database and then builds or customizes the page on the fly before sending it to
the requestor.
ASP is a feature of the Microsoft Internet Information Server (IIS), but, since the
server-side script is just building a regular HTML page, it can be delivered to almost any
browser. You can create an ASP file by including a script written in VBScript or JScript in
an HTML file or by using ActiveX Data Objects (ADOs) program statements in the HTML
file. You name the HTML file with the “.asp” file suffix. Microsoft recommends the use of
the server-side ASP rather than a client-side script, where there is actually a choice,
because the server-side script will result in an easily displayable HTML page. Client-side
scripts (for example, with JavaScript) may not work as intended on older browsers.
Ɣ Virtual directory: Virtual directories are aliases for directory paths on the
Notes
server.
Ɣ Form collection: The Form collection holds the values of the form elements
submitted with the POST method. This is the only way to generate a Form
collection.
Ɣ Variable: Variable is a memory location through which the actual values are
stored/retrieved. Its value can be changed.
Ɣ Client-side script and server-side script: Scripts executed only by the
browser without contacting the server is called client-side script. It is
browser-dependent. The scripting code is visible to the user and hence not
secure. Scripts executed by the web server and processed by the server is
called server-side script.
3.11 Check Your Progress: Answers
I. Fill in the Blanks
1. Network
2. Middle Tier
3. Threads
4. Automatic Teller Machine
5. HTTP
II. True or False
1. False
2. False
3. True
4. True
5. True
III. Multiple Choice Questions
1. (a) persistent HTTP
2. (a) application layer
3. (a) uniform resource identifier
4. (b) transmission control protocol
5. (a) multiple HTTP requests are sent on a single TCP connection without
waiting for the corresponding responses
6. (b) 21
7. (a) transmission control protocol
8. (b) Passive mode
9. (c) client server architecture
10. (d) All of the mentioned