ASP Compatibility
- ASP is a Microsoft Technology
- To run IIS you must have Windows NT 4.0 or
later
- To run PWS you must have Windows 95 or later
- ChiliASP is a technology that runs ASP without
Windows OS
- InstantASP is another technology that runs
ASP without Windows
What is an ASP File?
- An ASP file is just the same as an HTML file
- An ASP file can contain text, HTML, XML, and
scripts
- Scripts in an ASP file are executed on the
server
- An
ASP file has the file extension ".asp"
How Does ASP Differ from HTML?
- When a browser requests an HTML file, the
server returns the file
- When a browser requests an ASP file, IIS passes
the request to the ASP engine. The ASP engine
reads the ASP file, line by line, and executes
the scripts in the file. Finally, the ASP file
is returned to the browser as plain HTML
- You
cannot view the ASP source code by selecting "View
source" in a browser, you will only see the output
from the ASP file, which is plain HTML. This
is because the scripts are executed on the server
before the result is sent back to the browser.
What can ASP do for you?
- 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
- Provides security since your ASP code can
not be viewed from the browser
- Since ASP files are returned as plain HTML,
they can be viewed in any browser
- Clever ASP programming can minimize the network
traffic
The
Basic Syntax Rule
An ASP file normally contains HTML tags, just like
an HTML file. However,
an ASP file can also contain server scripts, surrounded by the delimiters <% and %>.
Server scripts are executed on the server, and can contain any expressions,
statements, procedures, or operators valid for the scripting language you prefer
to use.
The
Response Object
The Write method of the ASP Response Object is
used to send content to the browser. For example, the following statement sends
the
text "Hello World" to the browser:
<%
response.write("Hello World!")
%>
|
VBScript
You may use different scripting languages in ASP files. However, the default
scripting language is VBScript:
<html>
<body>
<%
response.write("Hello World!")
%>
</body>
</html>
|
The example
above writes "Hello World!" into
the body of the document.
JavaScript
To set JavaScript as the default scripting language for a particular page you
must insert a language specification at the top of the page:
<%@ language="javascript"%>
<html>
<body>
<%
Response.Write("Hello World!")
%>
</body>
</html>
|
Note: Unlike
VBScript - JavaScript is case sensitive. You will have to write your ASP
code with
uppercase
letters and lowercase letters when the
language requires it.
Other
Scripting Languages
ASP is shipped with VBScript and JScript (Microsoft's implementation of JavaScript).
If you want to script in another language, like PERL, REXX, or Python, you will
have to install script engines for them.
Important: Because the scripts are executed on the server, the browser
that displays the ASP file does not need to support scripting at all!
Procedures
The ASP
source code can
contain procedures
and functions:
<html>
<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head>
<body>
<p>Result: <%call vbproc(3,4)%></p>
</body>
</html>
|
Insert
the <%@ language="language" %> line above the <html> tag
to write procedures or functions in another scripting language than default:
<%@ language="javascript" %>
<html>
<head>
<%
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
%>
</head>
<body>
<p>Result: <%jsproc(3,4)%></p>
</body>
</html>
|
Differences Between VBScript and JavaScript
When calling
a VBScript
or a JavaScript
procedure from an ASP file written in
VBScript, you can use the "call" keyword followed by the procedure name. If a
procedure requires parameters, the parameter list must be enclosed in parentheses
when using the "call" keyword. If you omit the "call" keyword, the parameter
list must not be enclosed in parentheses. If the procedure has no parameters,
the parentheses are optional.
When
calling a JavaScript or a VBScript procedure from an ASP file written in
JavaScript,
always use parentheses after the procedure name.
User
Input
The Request object may be used to retrieve user information from forms:
<form method="get" action="simpleform.asp">
First Name: <input type="text" name="fname">
<br />
Last Name: <input type="text" name="lname">
<br /><br />
<input type="submit" value="Submit">
</form>
|
User input
can be retrieved in two ways: With Request.QueryString or Request.Form.
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. If
a user typed "Bill" and "Gates" in the form example above, the URL sent
to the server would look like this:
http://www.w3schools.com/simpleform.asp?fname=Bill&lname=Gates
|
Assume
that the ASP file "simpleform.asp" contains
the following script:
<body>
Welcome
<%
response.write(request.querystring("fname"))
response.write(" " & request.querystring("lname"))
%>
</body>
|
The browser will display the following in the body of the document:
Request.Form
The Request.Form
command
is used
to collect values in a form with method="post".
Information sent from a form with the POST method is invisible to others and
has no limits on the amount of information to send. If
a user typed "Bill" and "Gates" in the form example above, the URL sent
to the server would look like this:
http://www.w3schools.com/simpleform.asp
|
Assume
that the ASP file "simpleform.asp" contains
the following script:
<body>
Welcome
<%
response.write(request.form("fname"))
response.write(" " & request.form("lname"))
%>
</body>
|
The browser will display the following in the body of the document:
Form Validation
User input should be validated on the browser whenever possible (by client scripts).
Browser validation is faster and you reduce the server load.
You should consider using 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.
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 for
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 "Kirk" to it:
<%
Response.Cookies("firstname")="Kirk"
%>
|
It is also possible to assign properties to a cookie, like setting a date
when the cookie should expire:
<%
Response.Cookies("firstname")="Kirk"
Response.Cookies("firstname").Expires=#July 14,2003#
%>
|
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
A Cookie with Keys
If a cookie
contains
a collection
of multiple values, we say that the cookie
has Keys. In
the example below, we will create a cookie collection named "user". The "user" cookie
has Keys that contains information about a user:
<%
Response.Cookies("user")("firstname")="Kirk"
Response.Cookies("user")("lastname")="Arnett"
Response.Cookies("user")("country")="USA"
%>
|
<html>
<body>
<%
dim x,y
for each x in Request.Cookies
response.write("<p>")
if Request.Cookies(x).HasKeys then
for each y in Request.Cookies(x)
response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
response.write("<br />")
next
else
Response.Write(x & "=" & Request.Cookies(x) & "<br />")
end if
response.write "</p>"
next
%>
</body>
</html>
|
The Session
object
When you
are working with an application, 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 start the application and when you end. But 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 client and it contains information that identifies the user.
This interface is alled the Session object.
The Session object is used to store information
about, or change settings for a user session. Variables stored in the 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.
When does a Session Start?
A session
starts when:
- A new user requests an ASP file, and the Global.asa file includes a Session_OnStart
procedure
- A value is stored in a Session variable
- A
user requests an ASP file, and the Global.asa file uses the <object> tag
to instantiate an object with session scope
When does a Session End?
A session
ends if a user has not requested or refreshed a page in the application for a
specified period.
By default,
this is 20 minutes.
If you want to set a timeout interval that is shorter or longer than the default,
you can set the Timeout property.
The example below sets a timeout interval of 5 minutes:
To end a session immediately, you may use the Abandon method:
Note: The
main problem with sessions is WHEN they should end. We do not know if the
user's
last request
was the final one or not. So we do not
know how long we should keep the session "alive". Waiting too long uses up
resources on the server. But if the session is deleted too fast you risk that
the user is coming back and the server has deleted all the information, so
the user has to start all over again. Finding the right timeout interval can
be difficult.
Tip: If
you are using session variables, store SMALL amounts of data in them.
Store
and Retrieve Session Variables
The most important thing about the Session object is that you can store variables
in it.
The example below will set the Session variable username to "Donald
Duck" and the Session variable age to "50":
<%
Session("username")="Donald Duck"
Session("age")=50
%>
|
When the value is stored in a session variable it can be reached from ANY
page in the ASP application:
Welcome <%Response.Write(Session("username"))%>
|
The line
above returns: "Welcome Donald Duck".
You can
also store user preferences in the Session object, and then access that preference
to choose
what page
to return to the user.
The example
below specifies a text-only version of the page if the user has a low 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%>
|
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:
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
Response.Write(Session.Contents(i) & "<br />")
Next
%>
|
Result:
Session variables: 2
Donald Duck
50
|
Loop Through the StaticObjects Collection
You can loop through the StaticObjects collection, to see the values of all objects
stored in the Session object:
<%
dim i
For Each i in Session.StaticObjects
Response.Write(i & "<br />")
Next
%>
|
Application Object
An application on the Web may be a group of ASP files. The ASP files work together
to perform some purpose. The Application object in ASP 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, while with Sessions there is one Session object for EACH user.
The Application object should hold information that will be used by many pages
in the application (like database connection information). This means that
you can access the information from any page. It also means that you can change
the information in one place and the changes will automatically be reflected
on all pages.
Store and Retrieve Application Variables
Application variables can be accessed and changed by any page in the application.
You
can create Application variables in "Global.asa" like this:
<script language="vbscript" runat="server">
Sub Application_OnStart
application("vartime")=""
application("users")=1
End Sub
</script>
|
In the
example above we have created two Application variables: "vartime" and "users".
You can access the value of an Application variable like this:
There are
<%
Response.Write(Application("users"))
%>
active connections.
|
Loop Through the Contents Collection
The Contents collection contains all application variables. You can loop through
the Contents collection, to see what's stored in it:
<%
dim i
For Each i in Application.Contents
Response.Write(i & "<br />")
Next
%>
|
If you do not know the number of items in the Contents collection, you can
use the Count property:
<%
dim i
dim j
j=Application.Contents.Count
For i=1 to j
Response.Write(Application.Contents(i) & "<br />")
Next
%>
|
Loop Through the StaticObjects Collection
You can loop through the StaticObjects collection, to see the values of all objects
stored in the Application object:
<%
dim i
For Each i in Application.StaticObjects
Response.Write(i & "<br />")
Next
%>
|
Lock and Unlock
You can
lock an application
with
the "Lock" method. When an application is locked,
the users cannot change the Application variables (other than the one currently
accessing it). You can unlock an application with the "Unlock" method. This method
removes the lock from the Application variable:
<%
Application.Lock
'do some application object operations
Application.Unlock
%>
|