Response

coordinatedcapableSoftware and s/w Development

Nov 4, 2013 (4 years and 4 days ago)

91 views

Web programming

Active Server Page (ASP)

Amarsanaa .G

School of Information Technology

National University of Mongolia

ASP

Developed by Microsoft

ASP are server
-
generated pages which
can call other programs to do things like
access database.

ASP is an open, compile
-
free application
environment which you can combine
HTML, script and reusable ActiveX
server components to create dynamic
and powerful Web
-
based business
solutions

What is ASP?

A
ctive
S
erver
P
ages

A series of objects and components that
are executed
on

the web server


Uses a suite of technologies that allows
dynamically
-
generated content

Control of how content is generated
from

the server to the browsers

ASP

ASP pages are actually scripts that are
run, or executed, on the web server

------------

------------

------------

ASP

----------

----------
------

.

------

.

Web Server

-----

-----

-----

-----

HTML

Sample Document

<%@Language=VBScript %>

<HTML>

<HEAD>

<TITLE>foo</TITLE>

</HEAD>

<BODY>

<H1>Hello World</H1>

<UL>

<%

Dim i

FOR i=1 TO 3


Response.Write("<LI>"&i&"</LI>")

NEXT

%>

</UL>

</BODY>

</HTML>


<HTML>

<HEAD>

<TITLE>foo</TITLE>

</HEAD>

<BODY>

<H1>Hello World</H1>

<UL>

<LI>1</LI>

<LI>2</LI>

<LI>3</LI>

</UL>

</BODY>

</HTML>


Browser gets this

Server reads this

HTML Page Execution

You Request an HTML Page


Server finds and downloads the page


Browser displays the page


ASP Page Execution

You Request an ASP Page



Server finds the page



Browser displays the page



Server executes ASP commands, converts ASP
to HTML/XML as needed, processes HTML/XML


ASP Compared to Other
Scripting Languages

ASP and /PHP/Perl/CGI execution are
roughly equivalent in execution sequence

Compared to VBScript, ASP does not
have to worry about browser
incompatibility

Compared to JavaScript, don’t have to
worry about browser versions or
disabling

Compared to Java, don’t have to worry
about whether JRE (or MVM) is installed

When should we use ASP

Whenever you want your pages to be
created dynamically when the browser
requests the page.


Display data on the certain date


Storing the data collected from a form

Often the ASP scripts will query a
database and format the data retrieved
for display in a HTML page.

How to run ASP on your own
PC

To run own PC without external server.


Install Microsoft Personal Web Server
(PWS) or Internet Information Server (IIS)
on your PC

What you need to have


At least windows 98, second Edition


Windows 2000 or later version is recommended

Detail about the installation


www.w3schools.com/asp/asp_install.asp

ASP Syntax

An Active Server Page is an HTML file with some additional code
in it.


<% ASP Code evaluated here %>


Here’s an example page:


<%@ Language = JavaScript%>


<HTML>


<HEAD>


<TITLE> Example</TITLE>


</HEAD>


<BODY bgcolor = Lime aLink= DarkTurquoise>


<p> </p>


<% Response.Write(“Hello, World”)%>


</BODY>


</HTML>

If you save this file as .asp put on an ASP
-
ready server, you will
see the “Hello, World”

ASP in the Microsoft Language
Family

The ASP
commands
...

Visual Basic

VBScript

ASP

But there’s more to ASP than just code...


ASP can also interact with other
Microsoft languages or applications:


Execute SQL commands


Open and close databases


Create and Modify database tables


Control applications or COM objects written
or compiled in other languages (VB, C++,
Java)


Extract content from Excel or Word


Control an XML document

ASP in the Microsoft Language
Family

Application Control Techniques

Application Control Techniques


Application Control Techniques


Application Control Techniques


Application Control Techniques


Application Control Techniques


ASP Object Model


Composed of


5 objects


5 components

ASP Objects

Request

Response

Server

Application

Session

ASP Components


Scripting Objects Component


ADO (ActiveX Data Objects) Component


Ad Rotator Component


Browser Capabilities Component


Content Linking Component

ASP Object Model

Server

Application

Session

Request

Response

Session

Request

Response

Client

Client

Server

Response

Client

Request

Server

Server

Server

Response

Client

Request

ASP Application Development
Tools

Personal Preferences


Microsoft FrontPage


Allaire HomeSite

Other Tools


Microsoft Access


Visual Basic


Microsoft Visual InterDev


Visual Studio .NET


Other Non
-
Microsoft Tools (never used them)

ASP Reference Tools

Microsoft Web Site
(www.microsoft.com)


Knowledge Base for different Microsoft
products and Web developer issues


Search Capability for programming
language keywords and their syntax


Online versions of Reference Books


MSDN (Microsoft Developer Network) Web
site for subscribers and/or owners of
VStudio

MSDN Subscription on CD or DVD

Tools You Need to Create ASP
Applications

If you are working with database
storage/retrieval


Knowledge of Access, SQL Server, etc. (to
create databases and tables)


Knowledge of SQL commands (or at least
how to use Access to generate SQL
statements)


Knowledge of the VB style of data handling

Authoring tool(s)

The basic Syntax Rule

An ASP file normally contains HTML tags

An ASP file can also contain server scripts,
surrounded by the delimiters <%and %>

Server scripts are executed on the server


Any expressions


Statements


Procedures


operators

The Response object

The write method of the ASP response
Object is used to send content to the
browser


Response.Write “Hello, ASP”


Scripting language


You can use following scripting
language for developing ASP pages



VBScript



JavaScript


Reference


MSDN
-
> VBScript Language Reference


MSDN
-
> JavaScript Language Reference

VBScript vs JavaScript


<%@language=VBScript %>

<%

Dim a

a = 5

Response.Write “Value of a: ”&a

%>





<%@language=JavaScript %>


<%

var a

a = 5;

Response.Write(“Value of a: ”+a)

%>



Output is same:

Value of a: 5

Another way to create ASP code

<%@language=VBScript

%>

<html>

<body>

<script language=“VBScript” runat=“server”>

Response.write(“Hello World”)

</script>

</body>

</html>

VBScript

You may use different scripting
language in ASP files

The default scripting language is
VBScript


<html>

<body>

<%

Response.write(“Hello World”)

%>

</body>

</html>

ASP Variables

A variable is used to store information


<html>

<body>

<%


dim name


name = “Ali Hosseini”


response.write(“My name is:” &name)

%>

</body>

</html>

ASP vs. JavaScript

It sounds like ASP is more like a
JavaScript than HTML.

JavaScript is run in the Web browser
itself rather than on the server side.

ASP can interact with server and other
server
-
side technology as databases.

VBScripting Language


Constants (e.g. vbSunday, vbBlack)


Errors


VBScript Run
-
time Errors (13 Type mismatch)


VBScript Syntax Errors (1005 Expected '(' )


Functions (IsNull, Chr, Day)


Keywords (Nothing, Null, True, False, Empty)


Methods (Clear, Execute ...)


Objects and Collections (err, RegExp)


Operators (Arithmetic, Logical, Comparision)


Properties (description, length)


Statements (Dim, If..then..end if, Do..loop)

Arrays

Need to declare with
Dim
:


Dim x(10)


Dim board(7,7)

x is an array with
elements 0 through 10

board is a 2
-
D array
with elements 8 rows
and 8 columns

Array Element Access

Use
()
, not
[ ]

!


Dim i,x(10)


For i=0 to 10


x(i) = i

Next

2
-
D Access

Dim i,j,board(2,2)

For i=0 to 2


For j=0 to 2


board(i,j)=0


Next

Next


ReDim

You can change the
dimension

of an
array:


Dim x

ReDim x(100)

ReDim x(25,25)

Collections

Associative Array


the index can be a
number

or a
string
.

Request
.Form(
0
)
or

Request.Form(
“uid”
)

Session.
Contents
(
"FirstName"
) = "Meng"



Session.
Contents
(2)

Supported by a
Scripting.Dictionary

object


access to the collection is via an object.

Dim c

Set c =
CreateObject("Scripting.Dictionary")

Use
set

whenever you assign an object to a variable!

Dictionary

Objects

Properties & Methods

Count
: number of elements in the
collection.

Items
: returns array of all items
(values)

Keys
: returns array of all keys
(indicies)

Item(key)

: returns an item (looks up
key)

Remove(key)

: removes one
key/value pair.

Dictionary Example

Dim grades

Set grades=CreateObject("Scripting.Dictionary")

grades.Add "HW1",85

grades.Add "HW2",100

grades.Add "Test",91

%>

<H3>Your Grades</H3>

<TABLE>

<TR><TH>HW1</TH><TD><%=grades.Item("HW1")%></TD></TR>

<TR><TH>HW2</TH><TD><%=grades.Item("HW2")%></TD></TR>

<TR><TH>Test</TH><TD><%=grades.Item("Test")%></TD></TR>

</TABLE>

VBScript ASP Objects

Err

Request, Response

Session, Application

FileSystemObject

RegExp, Match, Matches



The
Request

Object

Provides information about the
request

Dim val =

Request
.
cookie
("cookiename")=

Dim val =
Request
.
form
("fieldname")

or just use

Dim val =
Request
("fieldname")

The
Response

Object

Used to modify the HTTP response


Response.Cookie("cname") = value


Response.Write("something")


Response.Redirect("http://foo.com")

The
FileSystem

Object

Access to Drives, Folders and Files (on
the web server machine).


Copy, move, create, delete files and
folders.


Write and Read Files.


The
RegExp

Object

Regular Expressions


Match()


Replace()


Language used to describe patterns is
pretty much the same as in Perl.

OO Interface is a bit harder to use
(requires more code).

VBScript Built
-
in Functions

Lots of functions:


Math


Formatting (currency, numbers, etc.)


Dates


Strings

Random Numbers in VBScript

Functions:
rnd()
and
Randomize()


Randomize()


answer = Int(1000*Rnd()+1)

an integer in the range 1
-
1000 inclusive

Subroutines

Subroutines don't return anything.


Sub
subname
(
param1
,
param2
,…)


…statements…


Exit Sub


…statements…

End Sub


Example

Sub H3(txt)


Response.Write("<H3>")


Response.Write(txt)


Response.Write("</H3>")

End Sub


H3("Hello World")

Call H3("I have been called")

Functions

Functions return something

No
return statement
, instead set the
function name

to a value.

Function
funcname
(
param1
,…)


…statements…


Exit Function


…statements…


funcname

= something

End Function


Function Example

Function
MakeTag
(tag,txt)


Dim s


s = "<" & tag & ">"


s = s & txt


s = s & "</" & tag & ">"


MakeTag

= s

End Function


Response.Write(MakeTag("H4","Hello"))


Practice


Write Countdown time function.