Sunteți pe pagina 1din 33

Introduction to ASP

33

Session Objectives

Distributed Computing Environment


Active Server Pages
ASP Object Model
Using ASP Objects
Mantains HTTP State with ASP

33

Standalone environment

In a standalone environment you have


one computer that accepts input,
processes the input, and then returns the
results of that processing to a monitor
attached to it.
The application data, user input, display,
and output all lived on that one machine.

If you had to move the output to another


system you could put it on the network
If your computer was not connected to a
network, you could copy the data to a floppy
disk.

33

Distributed Computing Environment

Distributed computing using low


cost PCs has been around for some
time, since the mid 1980s.
However, its the emergence of the
Internet that has really brought it
into limelight.
But, what is distributed computing ?

33

Client-Server Environment
Client sends a
request for
information to an
IP address
1
4

ISP
ISP

Interne
Interne
tt

Client receives
information from
the server

Server receives a
request for
information

Client sends a
request for
information to an
IP address
3

I have an IP address:
020.006.023.004

Browser asks client computer


to send requests out and
interprets incoming data

33

I have an IP address:
199.056.787.111
Also, I can be called
by my domain name:
5
www.mysite.com

Active Server Pages

asp

How do they differ ?

ASP learning curve

html

They are data rich


JavaScript
HTML
Web Design
Databases

Internet

html
33

Active Server Pages


Development Environment

Internet Information Server


Database Software
Access DBs, SQL Server, Oracle
DBs etc..
Development Tools
Simple:

Notepad, TextPad
Normal: FrontPage, DreamWeaver
Hard: MS VisualInDev 6.0
33

How Does it Work?

The client requests an ASP page.


The server(IIS) recognizes that the
requested page is an ASP page(.asp)
and sends the .asp file to asp.dll for
processing.This includes execution of
all server-side scripting code.
The server sends the resulting HTML
page back to the client in the form of
HTTP response.
33

33

How does ASP work?

ADO
Connectivi
ty

<%
Dim Conn
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open(DSN=eComputerShopping)
SQL = "SELECT * FROM tblDepartment Order by DeptID"
SQL
Dim RS = Conn.Execute(SQL)
%>
<table border="1">
HTML or
<tr><td><b>Dept ID</b></td></tr>
XHTML
<%while(Not RS.EOF)%>
<tr><td>&nbsp<%Response.Write(RS("DeptID"))%></td></tr>
<%
RS.MoveNext
wend
%>
VBScript
</table>

33

10

Embedded scripts in HTML page


HTML code
<% (marks the beginning of ASP script)
...
ASP script code
....
%> (marks the end of ASP script)
More HTML code
<% more ASP %>

33

11

Key Elements of ASP Coding


<% %> tags to
separate ASP from
XHTML

Variable &
VBScript
Command

Language
declaration

<% @language = VBScript%>


<%
dim i
i = 10
%>

33

12

Key Elements of ASP Coding


Block
script
<script language=VBscript" runat="server">
Response.Write("Block Script <BR />")
</script>
<%Response.Write("Inline Script<BR />")%>
<!-- #include file=another.asp" -->

Inline
script

Include file

33

13

ASP Object Model

Response

Session

Server

Request

Application

ObjectContext
ASPError

33

14

Response
Response

sends some information

to the user

Methods
AddHeader
AppendToLog
BinaryWrite
Clear
End
Flush
Redirect
Write

Properties

Collections

Buffer
Cookies
CacheControl
Charset
ContentType
Expires
ExpiresAbsolute
IsClientConnected
Pics
Status
33

15

Response.Redirect

Redirects browser to a different web


page
Response.Redirect("http://mysite.com")
Response.Redirect

Local
page

("nextpage.asp)

remote
page
33

16

Request
Request

gets some information


from the user that is taken in an
HTTP request
Methods

BinaryRead

Properties

Collections
ClientCertificate
Cookies
Form
QueryString
ServerVariables

TotalBytes

33

17

Request
Eg:
RegisterProcess.asp?
txtLastName=QuangAnh&txtTitle=Mr
<%
Dim lastName =
Request.QueryString(txtLastName)
Dim title = Request.QueryString(txtTitle)
%>

33

18

How ASP adds state to web


Request.Form

Web page with form posts to ASP


ASP can read form objects from previous page
Request.Form("form_object")

Note form action


Form
that calls next
page
<form action="add_new_employee.asp"
Note form
method="POST" name="NewEmp">
object
Emp ID:
names
<input type="text" name="EmpID" size="4" maxlength="4">

Last Name:
<input type="text" name="Last" size="30" maxlength="30">
input type="submit" value="Submit" name="Submit">
<input type="reset" value="Reset" name="Reset"></p>
33
19
</form>

Session

Session

Methods
Abandon

Events
OnStart
OnEnd

Collections
Contents
StaticObjects

Eg: UserAccount(UserName, Password)


<%
// Management UserAccount
Session(UserName) = Aptech
Session(Password) = 56NCT;
%>
33

Properties
CodePage
LCID
SessionID
Timeout

20

Application
Application stores info that

persists for the entire lifetime of the


application-this is the whole time
the IIS is running
Methods

Properties

Events
OnStart
OnEnd

Lock
Unlock

33

21

Application
Eg: User Counter
<%
Application.Lock()
Application(UserCount) =
Application(UserCount) + 1
Application.UnLock()
%>

33

22

Server
Server to control the IIS
Methods
ScriptTimeout

Properties
CreateObject
Execute
GetLastError
HTMLEncode
MapPath
Transfer
URLEncode
33

23

Server.MapPath

Physical vs. Virtual Path

Server.MapPath

The virtual path is the part of the URL that


follows the .com, .net, .edu, etc.
The physical path is the physical drive, path,
and filename where file is actually located on
the web server.
Function that returns physical path to a file on
web site
Pass it relative path from the current page to
that file
Useful when connecting to Access database

Server.MapPath(mydata.mdb")
33

24

Statelessness

State is the ability of information to


persist throughout various parts of an
application

Needed for shopping carts and many other


applications

The Internet is inherently stateless

Web servers deliver web pages one at a time


upon request, tracking little information about
what was sent before or to whom
Keeps the structure of the Internet simple.
But makes developing a useful web application
challenging
33

25

How ASP Maintains State

33

26

How ASP adds state to web


Server Variables

Environment information stored on server


Many kinds of information including

SERVER_NAME name of server


REMOTE_HOST IP address of client

<%if(Request.ServerVariables("SERVER_NAME")=="kiosk) then%>
<a href="index.asp>Go Here</a>
<%else%>
<a
href="http://bible.crosswalk.com/OnlineStudyBible/bible.cgi>
Go Here </a>
<%end if%>
33

27

How ASP adds state to web


Request.Querystring

Access to variables passed with ? In URL

www.mysite.com/nextpage.asp?user=Tom&type=
cust
? separates the URL from the querystring values.
Each querystring value is in the format of
variable=value
A number of variables can be specified in a
querystring with each variable=value pair separated
by an ampersand (&)

Request.Querystring("user")

Case sensitive
Cannot pass values with embedded spaces

Passing Multiple Querystring variables


/scripts/directory-lookup.asp?name=fred&age=22
Using
Welcome, <%Response.Write(Request.QueryString("name"))%>.
33
28
Your age is <%Response.Write(Request.QueryString("age"))%>.

How ASP adds state to web


Request.Form

Web page with form posts to ASP


ASP can read form objects from previous page
Request.Form("form_object")

Note form action


Form
that calls next
page
<form action="add_new_employee.asp"
method="POST" name="NewEmp">
Note form
object
Emp ID:
names
<input type="text" name="EmpID" size="4" maxlength="4">

Last Name:
<input type="text" name="Last" size="30" maxlength="30">
input type="submit" value="Submit" name="Submit">
<input type="reset" value="Reset" name="Reset"></p>
</form>
33

29

How ASP adds state to web


Request.Form (cont)

Using Form Variables from Previous Page


On the last form, you selected:
<br>Emp ID: <%Response.Write(request.Form("EmpID"))%>
<br>Last Name: <%Response.Write(request.Form("Last"))%>

33

30

How ASP adds state to web


Session variables, cookies, and SessionID

Session variables exist for the life of a users


session with the web application

Private and unique to user


Stored on client PC
Requires that cookies be enabled
Cookies are small files of information created by a
web server and stored on the users computer

SessionID is a unique identifier created for each


user session

Session.SessionID

Declaring and Assigning


Session("SessQID") = NextQ
Using
Response.Write(Session("SessQID"))
33

31

Using HTML Form Objects Text Box

Used like Text Boxes in VB or Access


Input

Type=text
Name used for ASP reference
Size is length of text box
Maxlength is number of characters allowed
Value is default

' ' around recordset value prevents truncation


after ' or in database record.

<input type="text" name="RO" size=7


maxlength=7 value='<%Response.write(RSEdit("RO"))%>'>
33

32

Using HTML Form Objects (conts)

<input type="hidden" name="PK" value='<


%Response.write(RSEdit("pk"))%>'>

<textarea name="Paint" rows=5 cols=50


<%Response.write(RSEdit("Paint"))%></textarea>

33

33