Download - ASP theory
-
7/30/2019 ASP theory
1/51
Minder Chen 1998-2002 ASP - 1
Active Server Pages
Minder Chen, [email protected]
-
7/30/2019 ASP theory
2/51
Minder Chen 1998-2002 ASP - 2
Creating ASP Page
ASP uses the delimiters to enclose script commands.
By default, the primary scripting language is VBScript.
test.asp
This page was last refreshed on .
The VBScript function Now returns the current date and time.
This page was last refreshed on 9/11/98 4:30:00 PM.
This page was last refreshed on
9/11/98 4:30:00 PM.
Interpreted ASP code:
An HTML document.
Rendered
via abrowser
-
7/30/2019 ASP theory
3/51
Minder Chen 1998-2002 ASP - 3
The Active Server Pages Model
Brows
er
Web Server
(IIS)
Active
Server
Page
ASPInterpreter:
Process
ASP
Scripting
Statements
An ASP script begins to run when a browser requests a .asp file from
your Web server.
Your Web server then calls ASP, which reads through the requested
file from top to bottom, executes any ASP statements, and sends an
HTML page to the browser.
.asp files
-
7/30/2019 ASP theory
4/51
Minder Chen 1998-2002 ASP - 4
ASP Scripting
The first line in an .asp file specifies the
scripting language for the page. For example,
the following first line in .asp file specifies thatthe script is VBScript:
Without a language tag, script in the file isprocessed as the default language (VBScript by
default.)
Active Server Pages can provide a scripting
environment for a number of other scriptinglanguages, including Jscript and others.
-
7/30/2019 ASP theory
5/51
Minder Chen 1998-2002 ASP - 5
Hello.asp: Display Information
Hello World Example
The end of
script
section
The start of the
script section
Build-in
Object
Argument
Hello World Example
Hello World!
Returned HTML Document
method
-
7/30/2019 ASP theory
6/51
Minder Chen 1998-2002 ASP - 6
Hello2.asp
HELLO WORLD
Hello World!
http://localhost/asp/hello.asp
HELLO WORLD
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Return HTML Source Code
-
7/30/2019 ASP theory
7/51
Minder Chen 1998-2002 ASP - 7
Hello3.asp
-
7/30/2019 ASP theory
8/51
Minder Chen 1998-2002 ASP - 8
Hello4.htm: Sample JavaScript Code
HELLO WORLD
document.write("
Hello world! -- From Client-Side Scripting!")
Hello World! -- From HTML
-
7/30/2019 ASP theory
9/51
Minder Chen 1998-2002 ASP - 9
Data Types Variables: Simple variables and Array variables
VBScript subsumes all categories of data under one name
called a Variant. At a basic level, Variants contain either string or numeric
data.
String data is used for text, while numeric data contains
only numbers.
Variant data can be further classified into subtypes. For
example, you can have numeric data that represents
currency, or a date or time, and the Variant will interpret
the data accordingly.
You can use the data type conversion function for data
type conversion. For example, CIntfunction to forceconversion of an expression to the Variant of subtype
Integer.
-
7/30/2019 ASP theory
10/51
Minder Chen 1998-2002 ASP - 10
Data Types
Empty Variant is uninitialized. Value is 0 for numeric variables or a zero-
length string ("") for string variables.
Null Variant intentionally contains no valid data.
Boolean Contains either True or False.
Byte Contains integer in the range 0 to 255. Integer
Contains integer in the range -32,768 to 32,767.
Currency
-922,337,203,685,477.5808 to 922,337,203,685,477.5807. Long
Contains integer in the range -2,147,483,648 to 2,147,483,647.
-
7/30/2019 ASP theory
11/51
Minder Chen 1998-2002 ASP - 11
Data Types
Single Contains a single-precision, floating-point number in the range -
3.402823E38 to -1.401298E-45 for negative values and1.401298E-45 to 3.402823E38 for positive values.
Double Contains a double-precision, floating-point number in the range
-1.79769313486232E308 to -4.94065645841247E-324 for negativevalues and 4.94065645841247E-324 to 1.79769313486232E308 for
positive values. Date (Time)
Contains a number that represents a date or time betweenJanuary 1, 100 to December 31, 9999.
String Contains a variable-length string that can be up to
approximately two billion characters in length.
Object Contains an object.
Error Contains an error number.
-
7/30/2019 ASP theory
12/51 Minder Chen 1998-2002 ASP - 12
Declaring Variables VBScript implicitlycreates a variable the first time that it encounters an
unrecognized string of characters that could be a variable name.
The Option Explicit statement informs VBScript to generate an error if itencounters an undeclared variable. The Option Explicit statement shouldbe the first line of code in a script that uses variables.
Dim varname
Dim a
A variable name: Must begin with an alphabetic character.
Cannot contain an embedded period.
Must not exceed 255 characters.
Must be unique.
Is not case-sensitive.
Constants: Const CorpName = "Volcano Coffee Company"
Const HousePayment = 1500
-
7/30/2019 ASP theory
13/51 Minder Chen 1998-2002 ASP - 13
Exercise: Variable.asp
Variables
Have to be the
first line
Declare a variablewith a data type
is not allowed:
Dim x as Integer
-
7/30/2019 ASP theory
14/51 Minder Chen 1998-2002 ASP - 14
Response Object: Write Method
Hello World
Hello World
Message
-
7/30/2019 ASP theory
15/51 Minder Chen 1998-2002 ASP - 15
Math OperationsMath Operations
7 + 3 = 10
7 - 3 = 4
7 * 3 = 21
7 / 3 = 2.33333333333333
-
7/30/2019 ASP theory
16/51 Minder Chen 1998-2002 ASP - 16
Decision Making and Branching To vary the flow of a script, you use conditional statements (also known as
controlstructures) to makes decisions during program execution.
The conditional statements include test expressions that are evaluated as
the program runs and, based upon their results, control the program flow. The two control structures that you will learn include
IfThenElse
Select Case.
C
-
7/30/2019 ASP theory
17/51 Minder Chen 1998-2002 ASP - 17
Control Flow
True Block
False Block
M ki D i i U i S l C
-
7/30/2019 ASP theory
18/51 Minder Chen 1998-2002 ASP - 18
Making Decision Using Select Case A Select Case structure works with a single test expression that is evaluated
once at the top of the structure.
The result of the expression is then compared with the values for each Casein the structure. If there is a match, the block of statements associated withthat Case is executed.
Use the Case Else statement to handle any condition that did not match aspecified case.
E l
-
7/30/2019 ASP theory
19/51 Minder Chen 1998-2002 ASP - 19
Example
Dim Color, MyVar
Sub ChangeBackground (Color)
MyVar = lcase (Color)Select Case MyVar
Case "red" bgColor = "red"
Case "green" bgColor = "green"
Case "blue" bgColor = "blue"
Case Else bgColor = "white"
End Select
End Sub
L
-
7/30/2019 ASP theory
20/51 Minder Chen 1998-2002 ASP - 20
Loops
Loops
1
2
3
4
5
10
8
6
42
0
L St t t
-
7/30/2019 ASP theory
21/51 Minder Chen 1998-2002 ASP - 21
Loops Statements
VBScript includes the following looping
constructions:
Do...Loop: repeats statements while or until a test
condition is met.
For...Next: repeats statements a specified number of
times.
For Each element In group Next: repeats statements for
each occurrence of a specific item in a specified group.
L D Whil L
-
7/30/2019 ASP theory
22/51 Minder Chen 1998-2002ASP - 22
Loop: Do While Loop
Counter = 1
Counter = 2
Check condition at the
beginning of the loop
D L Whil
-
7/30/2019 ASP theory
23/51 Minder Chen 1998-2002ASP - 23
Do Loop While
Dim Counter
Counter = 1
Do
Response.Write "Counter = " & Counter & "
"Counter = Counter + 1
Loop While Counter < 3
Check condition at
the end of the loop
D U til
-
7/30/2019 ASP theory
24/51 Minder Chen 1998-2002ASP - 24
Do Until
To check the test condition at the beginningof the
loop, change the following statement:
Do While Counter=3 Loop
To check the condition at the endof the loop,
change the following statement:
Do Loop While Counter=3
F I t D t P i
-
7/30/2019 ASP theory
25/51 Minder Chen 1998-2002 ASP - 25
Form Input Data Processing
form4.htm
Form Input
Name:
answer04.aspForm Input Processing
E i F I t D t P i
-
7/30/2019 ASP theory
26/51 Minder Chen 1998-2002 ASP - 26
Exercise: Form Input Data Processing
Form htm
-
7/30/2019 ASP theory
27/51 Minder Chen 1998-2002 ASP - 27
Form.htmOrder
Sample Order Form
Please fill out this form, then click Submit:
First Name:
Last Name:
Title: Mr.
Ms.
http://127.0.0.1/vbscriptlab/form.htm
Response asp
-
7/30/2019 ASP theory
28/51 Minder Chen 1998-2002 ASP - 28
Response.asp
Response.asp File
Order Received
Response2 asp
-
7/30/2019 ASP theory
29/51 Minder Chen 1998-2002 ASP - 29
Response2.asp
Response.asp File
Order Received
Mr.
Ms.
Change the action attribute
of form.htm to response2.asp
and save it as form2.htm
Exercise: Calculator
-
7/30/2019 ASP theory
30/51 Minder Chen 1998-2002 ASP - 30
Exercise: Calculator
Operator can be: +, -, *, /
Check divide-by-zero error
A*B=600
Cal.htm
Cal htm
-
7/30/2019 ASP theory
31/51
Minder Chen 1998-2002 ASP - 31
Cal.htmCalculator
First Number:
Operator is:
+
-
*
/
Second Number:
Cal asp
-
7/30/2019 ASP theory
32/51
Minder Chen 1998-2002 ASP - 32
Cal.aspCalculator
-
7/30/2019 ASP theory
33/51
Minder Chen 1998-2002 ASP - 33
Continued...Select Case Request.Form("op")
Case "+"
C=A+B
Response.write "A+B=" & C
Case "-"C=A-B
Response.write "A-B=" & C
Case "*"
C=A*B
Response.write "A*B=" & C
Case "/"If B0 Then
Temp=A/B
C=Round(Temp,2) ' Round it to 2 decimal places
Response.Write "A/B=" & C
Else
Response.write "The second number cannot be zero." & "
"Response.write "Use BACK navigation button to change it!" & "
"End IfCase Else
Response.Write "You need to choose an operator"
End Select
%>
Exercise: sub1 asp
-
7/30/2019 ASP theory
34/51
Minder Chen 1998-2002 ASP - 34
Exercise: sub1.asp
Subroutine
Subroutine
sub2.asp
Scope of a Variable
-
7/30/2019 ASP theory
35/51
Minder Chen 1998-2002 ASP - 35
Scope of a Variable
If a variable is declared outside of a procedure,
it is visible throughout the program.
If a variable is declared inside a procedure, it is
only visible inside that procedure.
The range of a variables visibility is called its
scope.
Functions
-
7/30/2019 ASP theory
36/51
Minder Chen 1998-2002 ASP - 36
Functions
Function fName(arg1, arg2, ...)
...
fName = expression
...
End Function
Create a variable with the same name as the Function.
Assign the return valueto that variable.
Arguments, including constants, variables, and
expressions, are passed to the Function procedure,
which can then use the arguments.
Function arguments are placed inside parentheses. If no
arguments are passed, empty parentheses are required.
Functions: temp2 asp
-
7/30/2019 ASP theory
37/51
Minder Chen 1998-2002 ASP - 37
Functions: temp2.asp
Temperature
-
7/30/2019 ASP theory
38/51
Minder Chen 1998-2002 ASP - 38
Object, Property and Method You can create an instance of an object by using the
Server.CreateObject statement.
An object can contain callable program code called methods and data
called properties.
Dot notation
objectName.propertyName
objectname.methodName
objectname.methodName(arg1, arg2, )
QueryString Collection
-
7/30/2019 ASP theory
39/51
Minder Chen 1998-2002 ASP - 39
QueryString Collection
The QueryString collection retrieves form values
passed to your Web server using HTTP GET methodor retrieves variable-value pairs set as text followed aquestion mark in the request URL.
Example:
Process Input Data from the QueryString
-
7/30/2019 ASP theory
40/51
Minder Chen 1998-2002 ASP - 40
Process Input Data from the QueryString
If the user typed Jeff, Smith, and 30, then the following URLrequest would be sent to the server:
http://scripts/profile.asp?firstname=Jeff&lastname=Smith&age=30&userstatus=new
profile.asp:
Hello, .
You are years old. Hello, Jeff Smith. You are 30 years old. This is your
first visit to this Web site!
Multiple Values of a Variable
-
7/30/2019 ASP theory
41/51
Minder Chen 1998-2002 ASP - 41
Multiple Values of a Variable The QueryString collection has an optional parameter
Counterthat you can to count the number of times that aspecific variable appears.
A request of a form containing a list box with multiple items:
http://scripts/list.asp?food=apples&food=olives&food=bread
Command to count multiple values:
Request.QueryString("food").Count To display the multiple values types, List.asp could contain the
following script:
The preceding script would display:applesolives
bread
Use ADO
-
7/30/2019 ASP theory
42/51
Minder Chen 1998-2002 ASP - 42
Use ADO
The Database Access component uses Active Data
Objects (ADO) to provide easy access to
information stored in a database that complies withthe Open Database Connectivity (ODBC) standard.
You will learn how to extract data using the SQL
SELECT statement and create an HTML table todisplay the results.
Identify the Database
-
7/30/2019 ASP theory
43/51
Minder Chen 1998-2002 ASP - 43
Identify the Database
Before using a database with the Database Access component, you must
identify the database in the ODBC application in Control Panel. In this
example, you will use a Microsoft Access database that is provided with
the ASP sample Web site.
At the computer running your Web server, open Control Panel.
Double-click the ODBC icon, and then click System DSN.
There are two types of data sources: User, which is available only to you,
and System, which is available to anyone using the computer. Datasources for use with the Web server need to be of the System type.
Click Add, choose the Microsoft Access Driver, and then click Finish.
In the Data Source Name box, type ASP Tutorial DB, and then click
Select. Select the c:\Webshare\asp\AdvWorks.mdb file and click OK.
Click OK to close the dialog boxes.
Set Up ODBC Data Source
-
7/30/2019 ASP theory
44/51
Minder Chen 1998-2002 ASP - 44
Set Up ODBC Data Source
Deptlist.asp
-
7/30/2019 ASP theory
45/51
Minder Chen 1998-2002 ASP - 46
Deptlist.aspDepartment ListingDepartment List
:
Using Table
-
7/30/2019 ASP theory
46/51
Minder Chen 1998-2002 ASP - 47
Using Table
OLE DB Connection String
-
7/30/2019 ASP theory
47/51
Minder Chen 1998-2002 ASP - 48
OLE DB Connection String
Microsoft Access
Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=phys ical path to database f i le
Microsoft SQL Server
Provider=SQLOLEDB.1;Data Source=path to
server Oracle
Provider=MSDAORA.1;Data Source=path toserver
Microsoft Indexing Service
Provider=MSIDXS.1;Data Source=path to indexfi le
Insert
-
7/30/2019 ASP theory
48/51
Minder Chen 1998-2002 ASP - 49
Insert
Update and Delete
-
7/30/2019 ASP theory
49/51
Minder Chen 1998-2002 ASP - 50
Update and Delete
ASP and Session Management
-
7/30/2019 ASP theory
50/51
Minder Chen 1998-2002 ASP - 51
ASP and Session Management
Hypertext Transfer Protocol (HTTP) is a stateless protocol. Each
browser request to a Web server is independent, and the server
retains no memory of a browser's past requests.
The Session object, one of the intrinsic objects supported by ASP,provides a developer with a complete Web session management
solution.
The Session object supports a dynamic associative array that a
script can use to store information.
For each ASP page requested by a user, the Session object will
preserve the information stored for the user's session. This
session information is stored in memory on the server. The user is
provided with a unique session ID that ASP uses to match user
requests with the information specific to that user's session.
A session is terminated
when you close the browser.
Err.asp
-
7/30/2019 ASP theory
51/51
Err.aspThe Err Object
Err.Description =Division by zero
Err.Number=11
Err.Source=Microsoft VBScript runtime error
Err.HelpFile=
Err.HelpContext =0