ASP Introduction

An ASP file can contain text, HTML tags and scripts. Scripts in an ASP file are executed on the server

What is ASP?

ASP Compatibility

What is an ASP File?

How Does ASP Differ from HTML?

What can ASP do for you?

Important: Because the scripts are executed on the server, the browser that displays the ASP file does not need to support scripting at all!

ASP Syntax

Write Output to a Browser

response.write("Hello World!")

<%="Hello World!"%>
You can use several scripting languages in ASP. However, the default scripting language is VBScript.
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!")

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.


Simple Variables

dim name
name="Donald Duck"
response.write("My name is: " & name)


Dim famname(3),i
famname(0) = "Jan Egil"
famname(1) = "Tove"
famname(2) = "Hege"
famname(3) = "Stale"

For i = 0 to 3
  response.write(famname(i) & "<br />")

Looping Through Headers

dim i
for i=1 to 6
  response.write("<h" & i & ">Header " & i & "</h" & i & ">")

Time based message (VBScript)

dim h

response.write("<p>" & now())
response.write(" (Norwegian Time) </p>")
If h<12 then
  response.write("Good Morning!")
  response.write("Good day!")
end if
Sample Output:
7/13/2007 8:50:36 AM (Norwegian Time)
Good Morning!

Time based message (JScript)

<%@ language="javascript" %> <html>
var d=new Date()
var h=d.getHours()

Response.Write(d + " (Norwegian Time)")
if (h<12){
  Response.Write("Good Morning!")
  Response.Write("Good day!")
Sample Output:
Fri Jul 13 09:35:10 EDT 2007 (Norwegian Time)
Good Morning!

Lifetime of Variables

A variable declared outside a procedure can be accessed and changed by any script in the ASP file.

A variable declared inside a procedure is created and destroyed every time the procedure is executed. No scripts outside the procedure can access or change the variable.

To declare variables accessible to more than one ASP file, declare them as session variables or application variables.

Session variables are used to store information about ONE single user, and are available to all pages in one application. Typically information stored in session variables are name, id, and preferences.

Application variables are also available to all pages in one application. Application variables are used to store information about ALL users in a specific application.