/****************************************************************/
/* Document : "Classical" ASP examples */
/* Doc. Version : 2 */
/* File : classical_asp.txt */
/* Purpose : some usefull examples for the Oracle or */
/* SQL Server DBA. This textfiles describes */
/* classical ASP ( not ASP.NET ) */
/* Date : 28-02-2002 */
/* Compiled by : Albert */
/****************************************************************/
================
1. Introduction:
================
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 %>.
The main thing to understand is that "Server scripts" in .asp files
are executed on the server, and not in the client's browser.
These scripts can contain any expressions, statements,
procedures, or operators valid for the scripting language you prefer to use.
Thus, the Web Server creates html content dynamically.
You may use different scripting languages in ASP files.
However, the default scripting language is VBScript.
Most commonly used languages are VBScript and Javascript.
This is how it works. The client request an .asp page.
The Server processes any script in this page and returns
an dynamically constructed html reponse to the client.
Client Server
------------------------------
------- request test.asp | <% if Hour(Now)< 12 then %>|
| |---------------------> | Good Morning. |
|Web | | <% else %> |
|browser| test.htm returned | Good Day. |
| | <------------------ | <% end if %> |
------ | ------------------------------
| |
| | if its before 12.00 o'clock
| V
-----<------ Good Morning
Server side script is contained in either <% %> delimiters or in a
Other simple example:
<%@ LANGUAGE=VBScript %>
<% For i = 3 To 7 %>
>
Hello World!
<% Next %>
ASP also uses some "intrinsic" objects, build right into the ASP architecture.
You can refer to their properties and methods.
These objects are:
Response, Request, Session, Application and Server.
Very important objects are the "Response" and "Request" objects.
Let's look at a few simple examples right now:
- example 1:
------------
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" into the body of the document.
<%
response.write("Hello World!")
%>
- example 2:
------------
- example 3:
------------
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"%>
<%
Response.Write("Hello World!")
%>
- example 4:
------------
<%
response.write("
You can use HTML tags to format the text!
")
%>
<%
response.write("
This text is
styled with the style attribute!
")
%>
=============
2. Variables:
=============
A variable is used to store information.
If the variable is declared outside a procedure it can be changed by any script
in the ASP file. If the variable is declared inside a procedure,
it is created and destroyed every time the procedure is executed.
2.1 Simple examples:
--------------------
- Declare a variable:
---------------------
<%
dim name
name="Donald Duck"
response.write("My name is: " & name)
%>
- Declare an array:
-------------------
<%
Dim famname(5),i
famname(0) = "Jan Egil"
famname(1) = "Tove"
famname(2) = "Hege"
famname(3) = "Stale"
famname(4) = "Kai Jim"
famname(5) = "Borge"
For i = 0 to 5
response.write(famname(i) & " ")
Next
%>
- Example: loop through html headers:
------------------------------------------
<%
dim i
for i=1 to 6
response.write("Header " & i & "")
next
%>
- Example: time based greeting using VBscript:
----------------------------------------------
<%
dim h
h=hour(now())
response.write("
" & now())
response.write(" (Norwegian Time)
")
If h<12 then
response.write("Good Morning!")
else
response.write("Good day!")
end if
%>
- Example: time based greeting using Javascript:
------------------------------------------------
<%@ language="javascript" %>
<%
var d=new Date()
var h=d.getHours()
Respons.Write("
")
if (h<12)
{
Response.Write("Good Morning!")
}
else
{
Response.Write("Good day!")
}
%>
2.2 Procedures in an asp file:
------------------------------
- Call a procedure using VBscript:
----------------------------------
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
You can call a procedure like this:
Result: <%call vbproc(3,4)%>
Or, like this:
Result: <%vbproc 3,4%>
- Call a procedure using Javascript:
------------------------------------
<%@ language="javascript" %>
<%
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
%>
Result: <%jsproc(3,4)%>
- How to use both VBscript and Javascript:
------------------------------------------
<%
sub vbproc(num1,num2)
Response.Write(num1*num2)
end sub
%>
Result: <%call vbproc(3,4)%>
Result: <%call jsproc(3,4)%>
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.
============================
3. ASP Forms and user input:
============================
The Request object may be used to retrieve user information from forms.
Suppose we have the following FORM:
Then we want to capture the values passed through this form.
User input can be retrieved in two ways: With Request.QueryString or Request.Form.
3.1 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:
Welcome
<%
response.write(request.querystring("fname"))
response.write(" " & request.querystring("lname"))
%>
The browser will display the following in the body of the document:
Welcome Bill Gates
3.2 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:
Welcome
<%
response.write(request.form("fname"))
response.write(" " & request.form("lname"))
%>
The browser will display the following in the body of the document:
Welcome Bill Gates
3.3 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.
3.4 Some more examples:
-----------------------
- Example: A Form with method is get:
How to interact with the user, with the Request.QueryString command.
<%
dim fname
fname=Request.QueryString("fname")
If fname<>"" Then
Response.Write("Hello " & fname & "! ")
Response.Write("How are you today?")
End If
%>
- Example: A Form with method is post:
How to interact with the user, with the Request.Form command.
<%
dim fname
fname=Request.Form("fname")
If fname<>"" Then
Response.Write("Hello " & fname & "! ")
Response.Write("How are you today?")
End If
%>
- Example: A Form with radio buttons:
How to interact with the user, through radio buttons, with the Request.Form command.
<%
dim cars
cars=Request.Form("cars")
%>
<%
if cars<>"" then
Response.Write("
Your favorite car is: " & cars & "
")
end if
%>
3.5 The Request and Response object methods:
--------------------------------------------
Request:
--------
Request.querystring
Request.Form
Request.cookies
Request.clientcertificate
Request.servervariables
The first two we have seen above, in retrieving userinput.
As an example of the last one:
Request.Servervariables("SERVER_NAME")
Response:
---------
Response.Clear
Response.End
Response.cookies
Response.Flush
Response.Redirect
Response.write
===============
4. ASP Cookies:
===============
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.
A cookie can be used to maintain state. State is the ability to retain user information
in a Web application.
How to Create a Cookie:
-----------------------
The "Response.Cookies" command is used to create cookies.
Note: The Response.Cookies command must appear BEFORE the 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,2002#
%>
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)
%>
===================================
5. Session and Application objects:
===================================
5.1 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.
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 called 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