Active Server Pages - PowerPoint PPT Presentation

About This Presentation
Title:

Active Server Pages

Description:

Common Gateway Interface (CGI) permits web browsers to request execution of ... TR TD Reggie Jackson /TD TD eagle /TD /TR TR TD Don Mattingly /TD ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 43
Provided by: johnavi
Learn more at: http://www.cs.albany.edu
Category:

less

Transcript and Presenter's Notes

Title: Active Server Pages


1
Active Server Pages
2
Static Internet
  • In the early days of the Internet, all
    information sent to clients browser was static.
  • Little interactivity.
  • Little functionality beyond simple hyperlinking.

3
Dynamic Internet, Part 1 (CGI)
  • Common Gateway Interface (CGI) permits web
    browsers to request execution of application on
    the web server.
  • The output of application is HTML code, which is
    sent to requesting browser.
  • Separate CGI application loaded for each request.
  • Development of client-side scripting languages
    especially JavaScript and VBScript.

4
Dynamic Internet, Part 2 (ISAPI)
  • An alternative to CGI introduced by Microsoft.
  • Uses dynamic link libraries (DLL). Once loaded,
    DLL stays in memory and answers user requests
    until explicitly released from memory.
  • Normally faster than an equivalent CGI
    application.

5
ISAPI filter
  • Custom DLL in same memory space as the web
    server, called by web server in response to every
    HTTP request.
  • The filter tells the web server how to handle the
    request.
  • The ASAPI filter allows you to customize the web
    servers response to specific types of user
    requests.

6
Examples of ASAPI filters
  • Security layer between client and web server.
    Screens client request.
  • Custom filter to map clients request to
    different location on the server.
  • Custom filter to interpret information from
    server, and present the information in a
    different format. For example, ASP.DLL, which
    interprets server code and gives the client
    customized information

7
Active Server Pages
  • In ASP.DLL.
  • Whenever user requests a file with .asp
    extension, this ASAPI filter handles the
    interpretation.
  • Loads scripting language interpreter DLLs.
  • Executes server-side code.
  • Passes resulting HTML to the web server, which
    forwards it to the browser.

8
Simple example
  • ltHTMLgt
  • ltBODYgt
  • Hello.ltBRgt
  • It is now approximately lt Time() gt at the
    server.
  • ltPgt
  • lt For i 1 to 5 gt
  • ltFONT SIZE lt i gt gt
  • Hello Size lt i gt lt/FONTgt ltBRgt
  • lt Next gt
  • lt/BODYgt
  • lt/HTMLgt

9
Output of simple example
10
ASP Object Model
  • ASP encapsulates the properties and methods of 6
    objects
  • Application
  • ASPError
  • Request
  • Response
  • Server
  • Session

11
Application Object
  • Represents the ASP application itself
  • One Application object for all users.
  • Two methods
  • Application_OnStart (triggered by first user)
  • Application_OnEnd (triggered by administrator
    explicitly closing application.

12
ASPError Object
  • To access properties of last error in currently
    executing script.
  • Accessible only through Servers GetLastError
    method.

13
Request Object
  • Access HTML form-based data and parameters sent
    over the address line.
  • Receive HTTP cookie information.
  • Receive client certificate information
  • Get access to information in HTTP request header
    (via ServerVariables collection).

14
Response Object
  • Controls how data is sent to client.
  • Sends cookies to client, set expiration date.
  • Redirects user to another URL.

15
Server Object
  • Gives access to web server itself.
  • Set timeout variables for scripts
  • CreateObject method lets you create instances of
    server-side components. For example, ActiveX Data
    Objects to handle database access.

16
Session Object
  • Holds information specific to users current
    session on the web server.

17
Server-Side Scripting
  • ASP.DLL interprets code in .asp files thats
    delimited with lt gt.
  • VBScript is the default scripting language.
  • If the first statement is
  • lt _at_LANGUAGEJscript gt
  • you can change the scripting language.

18
Simple example
  • ltHTMLgt
  • ltBODYgt
  • Hello.ltBRgt
  • It is now approximately lt Time() gt at the
    server.
  • ltPgt
  • lt For i 1 to 5 gt
  • ltFONT SIZE lt i gt gt
  • Hello Size lt i gt lt/FONTgt ltBRgt
  • lt Next gt
  • lt/BODYgt
  • lt/HTMLgt

19
Output of simple example
20
An example that uses Form Data
  • lthtmlgt
  • ltbodygt
  • ltform method"POST" action"http//localhost/asp/e
    x2.asp"gt
  • ltH2gtChoose your optionslt/H2gt
  • ltselect name"favoriteBird"gt
  • ltoption value"flamingo"gt Flamingo
  • ltoption value"robin"gt Robin
  • ltoption value"cardinal"gt Cardinal
  • ltoption value"eagle"gt Eagle
  • ltoption value"penguin"gt Penguin
  • ltoption value"bluejay"gt Blue Jay
  • lt/selectgt
  • ltBRgt User Name ltinput typetext name"userName"gt
  • ltBRgt Color ltinput typetext name"favoriteColor"gt
  • ltBRgt ltinput typesubmit value"Send Data"gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

21
The Users View
22
The ASP program
  • ltHTMLgt
  • ltHEADgt
  • ltBODYgt
  • lt username Request("userName") gt
  • lt color Request("favoriteColor") gt
  • lt bird Request("favoriteBird") gt
  • ltFONT COLOR'lt Response.Write color gt' gt
  • Hello, lt Response.Write userName gt. lt/FONTgt
  • ltBRgt Your favorite bird is the lt Response.Write
    bird gt.
  • Here is a picture!
  • ltPgt ltIMG SRClt Response.Write bird gt.jpggt
  • lt/BODYgt
  • lt/HTMLgt

23
What the user sees
24
The generated code
  • ltHTMLgt
  • ltHEADgt
  • ltBODYgt
  • ltFONT COLOR'blue' gt
  • Hello, John Avitabile. lt/FONTgt
  • ltBRgt Your favorite bird is the eagle.
  • Here is a picture!
  • ltPgt ltIMG SRCeagle.jpggt
  • lt/BODYgt
  • lt/HTMLgt

25
An example using file access
  • lthtmlgt
  • ltbodygt
  • ltform method"POST" action"http//localhost/asp/e
    x3.asp"gt
  • ltH2gtChoose your favorite birdlt/H2gt
  • ltselect name"favoriteBird"gt
  • ltoption value"flamingo"gt Flamingo
  • ltoption value"robin"gt Robin
  • ltoption value"cardinal"gt Cardinal
  • ltoption value"eagle"gt Eagle
  • ltoption value"penguin"gt Penguin
  • ltoption value"bluejay"gt Blue Jay
  • lt/selectgt
  • ltPgt ltinput typesubmit value"Send Vote"gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

26
The ASP Program (part 1)
  • lt Set fso CreateObject("Scripting.FileSystemObj
    ect") ' ActiveX
  • voteFile "C\Inetpub\wwwroot\asp\votes.txt"
  • Set tso fso.OpenTextFile(voteFile, 8, True)
    ' TextStreamObject
  • ' 8 is append, 1 is read, 2 is write True
    means create if nec.
  • Call tso.WriteLine(Request("favoriteBird"))
  • Call tso.Close()
  • Set tso fso.OpenTextFile(voteFile, 1, True)
  • Dim vote(6)
  • Do
  • line tso.ReadLine
  • Select Case line
  • Case "flamingo"
  • vote(1) vote(1) 1
  • Case "robin"
  • vote(2) vote(2) 1
  • Case "cardinal"
  • vote(3) vote(3) 1
  • Case "eagle"
  • vote(4) vote(4) 1

27
The ASP Program (part 2)
  • Call tso.Close() gt
  • ltHTMLgt
  • ltHEADgt
  • ltBODYgt
  • lt bird Request("favoriteBird") gt
  • Your vote for lt Response.Write bird gt has been
    recorded. Here are the
  • totals so far.
  • ltTABLE BORDER2 WIDTH50gt
  • ltTRgt ltTDgt Flamingo lt/TDgt ltTDgt lt Response.Write
    vote(1) gt lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Robin lt/TDgt ltTDgt lt Response.Write
    vote(2) gt lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Cardinal lt/TDgt ltTDgt lt Response.Write
    vote(3) gt lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Eagle lt/TDgt ltTDgt lt Response.Write
    vote(4) gt lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Penguin lt/TDgt ltTDgt lt Response.Write
    vote(5) gt lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Blue Jay lt/TDgt ltTDgt lt Response.Write
    vote(6) gt lt/TDgt lt/TRgt
  • lt/TABLEgt
  • lt/BODYgt
  • lt/HTMLgt

28
What the user sees
29
The generated code
  • ltHTMLgt
  • ltHEADgt
  • ltBODYgt
  • Your vote for eagle has been recorded. Here are
    the
  • totals so far.
  • ltTABLE BORDER2 WIDTH50gt
  • ltTRgt ltTDgt Flamingo lt/TDgt ltTDgt 2 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Robin lt/TDgt ltTDgt 3 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Cardinal lt/TDgt ltTDgt 2 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Eagle lt/TDgt ltTDgt 2 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Penguin lt/TDgt ltTDgt 1 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Blue Jay lt/TDgt ltTDgt 2 lt/TDgt lt/TRgt
  • lt/TABLEgt
  • lt/BODYgt
  • lt/HTMLgt

30
Three Tier Architecture
  • User Interface created using HTML, Dynamic HTML,
    or XML. Possibly including client-side scripts.
  • Web server as middle tier that manipulates data
    from database via SQL queries and communicates
    with client Web browser.
  • Database.

31
Accessing a database
lthtmlgt ltbodygt ltform method"POST"
action"http//localhost/asp/ex4.asp"gt ltPgt ltinput
typesubmit value"See Vote Totals"gt lt/formgt lt/bod
ygt lt/htmlgt
32
The ASP Program (part 1)
  • lt ' Check for existing connection to Database
  • If IsObject( Session("birdVotes_dbConn")) Then
  • Set dbConn Session("birdVotes_dbConn")
  • Else
  • Set dbConn Server.CreateObject(
    "ADODB.Connection" )
  • Call dbConn.Open("birdVotes","","") ' ODBC
    registered
  • Set Session("birdVotes_dbConn") dbConn
  • End If
  • dbQuery "SELECT FROM Votes"
  • ' Create recordset
  • Set votesRS Server.CreateObject(
    "ADODB.Recordset")
  • Call votesRS.Open( dbQuery, dbConn)
  • ' move to first record in recordset
  • Call votesRS.MoveFirst() gt

33
The ASP Program (part 2)
  • ltHTMLgt
  • ltHEADgt
  • ltBODYgt
  • lth1gt Votes Already Cast lt/h1gt
  • ltTABLE BORDER2 WIDTH50gt
  • ltTRgt ltTHgt Voter lt/THgt ltTHgt Bird lt/THgt lt/TRgt
  • lt While Not votesRS.EOF gt
  • ltTRgt ltTDgt lt votesRS("Voter") gt lt/TDgt
  • ltTDgt lt votesRS("Bird") gt lt/TDgt
    lt/TRgt
  • lt Call votesRS.MoveNext()
  • Wend gt
  • lt/TABLEgt
  • lt/BODYgt
  • lt/HTMLgt

34
What the user sees
35
The generated code
  • ltHTMLgt ltHEADgt ltBODYgt
  • lth1gt Votes Already Cast lt/h1gt
  • ltTABLE BORDER2 WIDTH50gt
  • ltTRgt ltTHgt Voter lt/THgt ltTHgt Bird lt/THgt lt/TRgt
  • ltTRgt ltTDgt Babe Ruth lt/TDgt
  • ltTDgt flamingo lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Lou Gehrig lt/TDgt
  • ltTDgt penguin lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Joe DiMaggio lt/TDgt
  • ltTDgt cardinal lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Mickey Mantle lt/TDgt
  • ltTDgt bluejay lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Reggie Jackson lt/TDgt
  • ltTDgt eagle lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Don Mattingly lt/TDgt
  • ltTDgt flamingo lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Mariano Rivera lt/TDgt
  • ltTDgt penguin lt/TDgt lt/TRgt
  • ltTRgt ltTDgt Derek Jeter lt/TDgt

36
Inserting, Updating, Complex queries
  • lthtmlgt
  • ltbodygt
  • ltform method"POST" action"http//localhost/asp/e
    x5.asp"gt
  • ltH2gtChoose your favorite birdlt/H2gt
  • ltselect name"favoriteBird"gt
  • ltoption value"flamingo"gt Flamingo
  • ltoption value"robin"gt Robin
  • ltoption value"cardinal"gt Cardinal
  • ltoption value"eagle"gt Eagle
  • ltoption value"penguin"gt Penguin
  • ltoption value"bluejay"gt Blue Jay
  • lt/selectgt
  • ltPgt Name ltinput typetext nameuserNamegt
  • ltPgt ltinput typesubmit value"Send Vote"gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

37
ASP Program (part 1)
  • lt ' Check for existing connection to Database
  • If IsObject( Session("birdVotes_dbConn")) Then
  • Set dbConn Session("birdVotes_dbConn")
  • Else
  • Set dbConn Server.CreateObject(
    "ADODB.Connection" )
  • Call dbConn.Open("birdVotes","","") ' ODBC
    registered
  • Set Session("birdVotes_dbConn") dbConn
  • End If
  • dbQuery "SELECT FROM Votes"
  • ' Create recordset
  • Set votesRS Server.CreateObject(
    "ADODB.Recordset")
  • Call votesRS.Open( dbQuery, dbConn)
  • ' move to first record in recordset

38
ASP Program (part 2)
  • Call votesRS.MoveFirst()
  • previousVote False
  • message Request("userName") ","
  • While Not votesRS.EOF
  • If Request("userName") votesRS("Voter")
    Then
  • message message " you changed your vote
    from " _
  • votesRS("Bird") " to "
    Request("favoriteBird")
  • sqlString "UPDATE Votes SET Bird '"
    Request("favoriteBird") _
  • "' WHERE Voter '"
    Request("userName") "'"
  • dbConn.Execute(sqlString)
  • previousVote True
  • End If
  • Call votesRS.MoveNext()
  • Wend

39
ASP Program (part 3)
  • If previousVote False Then
  • message message " your vote for "
    Request("favoriteBird") _
  • " has been recorded"
  • sqlString "INSERT INTO Votes VALUES ('"
    Request("userName") _
  • "', '" Request("favoriteBird")
    "')"
  • dbConn.Execute(sqlString)
  • End If
  • gt
  • ltHTMLgt
  • ltHEADgt
  • ltBODYgt
  • lt message gt
  • ltTABLEgt
  • ltTRgt ltTHgt Bird lt/THgt ltTHgt Votes lt/THgt lt/TRgt

40
ASP Program (part 4)
  • lt dbQuery "SELECT Votes.Bird, COUNT() AS
    BirdCount FROM Birds " _
  • "INNER JOIN Votes " _
  • "ON Birds.Bird Votes.Bird GROUP BY
    Votes.Bird"
  • Set birdsRS Server.CreateObject(
    "ADODB.Recordset")
  • Call birdsRS.Open( dbQuery, dbConn)
  • ' move to first record in recordset
  • Call birdsRS.MoveFirst()
  • While Not birdsRS.EOF
  • gt
  • ltTRgt ltTDgt lt birdsRS("Bird") gt lt/TDgt
  • ltTDgt lt birdsRS("BirdCount") gt lt/TDgt
    lt/TRgt
  • lt
  • Call birdsRS.MoveNext()
  • Wend gt
  • lt/TABLEgt
  • lt/BODYgt
  • lt/HTMLgt

41
What the User Sees
42
Code generated
  • ltHTMLgt
  • ltHEADgt
  • ltBODYgt
  • Thurman Munson, you changed your vote from
    robin to penguin
  • ltTABLEgt
  • ltTRgt ltTHgt Bird lt/THgt ltTHgt Votes lt/THgt lt/TRgt
  • ltTRgt ltTDgt bluejay lt/TDgt ltTDgt 2 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt cardinal lt/TDgt ltTDgt 1 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt eagle lt/TDgt ltTDgt 2 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt flamingo lt/TDgt ltTDgt 2 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt penguin lt/TDgt ltTDgt 3 lt/TDgt lt/TRgt
  • ltTRgt ltTDgt robin lt/TDgt ltTDgt 2 lt/TDgt lt/TRgt
  • lt/TABLEgt
  • lt/BODYgt
  • lt/HTMLgt
Write a Comment
User Comments (0)
About PowerShow.com