ASP Basics - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

ASP Basics

Description:

ASP code is usually written in VBScript although other language can be used (e.g. ... All examples here use VBScript. What do you need to experiment with ... – PowerPoint PPT presentation

Number of Views:745
Avg rating:3.0/5.0
Slides: 39
Provided by: Dr1004
Category:
Tags: asp | basics | vbscript

less

Transcript and Presenter's Notes

Title: ASP Basics


1
ASP Basics
  • Active Server Pages
  • Thanks to Deitel Deitel for much of this content

2
Introduction
  • A server-side scripting technology from Microsoft
  • An ASP file (extension .asp) can contain a
    mixture of HTML and ASP script code. The ASP
    code is executed at the server end and the
    results sent to the client.
  • Originally designed to run with Microsoft's
    Internet Information Server (IIS)
  • Can be run with some other servers (e.g. Apache,
    Netscape) if appropriate software is installed
    (e.g. Chilli!Soft ASP)
  • Last version is ASP 3.0 for use with IIS 5.0
    upwards
  • The current version is ASP.NET - so ASP is no
    longer a current product.
  • ASP code is usually written in VBScript although
    other language can be used (e.g. JavaScript,
    PerlScript). All examples here use VBScript.
  • What do you need to experiment with creating
    ASPs?
  • IIS or PWS (the code MUST run through a web
    server)
  • An editor (preferably not Notepad, e.g. EditPlus)
    or development tool such as Visual InterDev
  • A web browser

3
One Page Survival Guide
  • Subset of Visual Basic
  • NOT case sensitive
  • No s or s etc
  • ' or rem used for comments
  • Dim used to declare variables
  • option explicit in a script forces all variable
    to be declared before use - good practice
  • statements must fit on one line - use _ to
    continue
  • ltgt instead of !
  • used to test for equality as well as to assign
  • Set used to assign an object reference
  • Syntax of an if
  • if a gt b Then
  • a a 1
  • else
  • a a - 1
  • End if
  • (as well as ) used to concatenate strings

4
Built-in/Core Objects
  • In ASP 3.0 these objects are
  • You don't need to instantiate these in your code
    - they just exist and you can call their methods
    or reference their properties

5
Other Objects
  • objects created from additional server-side
    components - either shipped as part of ASP,
    downloaded separately or written yourself (e.g.
    using VB or C)
  • Some components which come as standard with IIS
    include

6
Object Use
  • To use objects belonging to these components you
    need to create instances of them using the
    CreateObject method of the built-in server object
    e.g.
  • Set fso Server.CreateObject("Scripting.FileSyst
    emObject" )
  • ..
  • fso.CreateTextFile( filePath )
  • How do you know the name of the object (e.g.
    Scripting.FileSystemObject)?

create an instance of the file system object
call one of its methods
7
Request Object
  • Request object
  • Contents includes
  • Information sent from the client (either by GET
    or POST)
  • e.g. Request("message")
  • to retrieve the contents of a form field with a
    NAME of "message
  • Any cookies e.g.
  • Any cookies e.g.
  • Request.cookies("myID")
  • to retrieve the value of the cookie called "myID"
  • Server variables including HTTP headers and other
    server information e.g. the server's name e.g.
  • Request.ServerVariables("SERVER_NAME")

8
Response Object
  • Uses include
  • Output content to the client e.g.
  • Response.Write("ltH2gtsorry can't create page -
    server fulllt/H2gt")
  • Redirect the client to another page e.g.
  • Response.Redirect("CreatePage.htm")
  • Control page caching at the browser end
  • Response.Expires 5
  • The page will expire after 5 minutes and if
    visited after that it will be reloaded from the
    server rather than displayed from cache.

9
Clock.aspsends Web servers date and time to
the client as XHTML markuplt gt scripting
delimeter_at_LANGUAGE processing directive
Option ExplicitFormatDateTimeNow
vbLongDate formatResponse.writeTime
  • 1 lt _at_LANGUAGE VBScript gt
  • 2
  • 3 lt
  • 4 ' Fig. 25.2 clock.asp
  • 5 ' A simple ASP example
  • 6 Option Explicit
  • 7 gt
  • 8
  • 9 lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN"
  • 10 "http//www.w3.org/TR/xhtml1/DTD/xhtml1-tran
    sitional.dtd"gt
  • 11
  • 12 lthtml xmlns "http//www.w3.org/1999/xhtml"gt
  • 13
  • 14 ltheadgt
  • 15 lttitlegtA Simple ASP Examplelt/titlegt
  • 16
  • 17 ltstyle type "text/css"gt
  • 18 td background-color black
  • 19 color yellow

10
Clock.aspTimeProgram Output
  • 36 lttdgt
  • 37 lt Time() gt
  • 38 lt/tdgt
  • 39 lt/trgt
  • 40 lt/tablegt
  • 41 lt/bodygt
  • 42
  • 43 lt/htmlgt

11
Fig. 25.3XHTML generated by clock.asp.
  • 1 lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN"
  • 2 "http//www.w3.org/TR/xhtml1/DTD/xhtml1-tran
    sitional.dtd"gt
  • 3
  • 4 lthtml xmlns "http//www.w3.org/1999/xhtml"gt
  • 5
  • 6 ltheadgt
  • 7 lttitlegtA Simple ASP Examplelt/titlegt
  • 8
  • 9 ltstyle type "text/css"gt
  • 10 td background-color black
  • 11 color yellow
  • 12 strong font-family arial,
    sans-serif
  • 13 font-size 14pt color
    blue
  • 14 p font-size 14pt
  • 15 lt/stylegt
  • 16
  • 17 lt/headgt
  • 18
  • 19 ltbodygt

12
Name.htmlpasses information into an ASP
document using the post method.
  • 1 lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN"
  • 2 "http//www.w3.org/TR/xhtml1/DTD/xhtml1-tran
    sitional.dtd"gt
  • 3
  • 4 lt!-- Fig. 25.4 name.html
    --gt
  • 5 lt!-- XHTML document that request an ASP
    document --gt
  • 6
  • 7 lthtml xmlns "http//www.w3.org/1999/xhtml"gt
  • 8
  • 9 ltheadgt
  • 10 lttitlegtName Requestlt/titlegt
  • 11 lt/headgt
  • 12
  • 13 ltbodygt
  • 14
  • 15 ltp style "font-family arial,
    sans-serif"gt
  • 16 Enter your name
  • 17 lt/pgt
  • 18
  • 19 lt!-- request name.asp when posted --gt

13
Name.html Program Output
14
Name.aspUses the Request method to retrieve the
data posted in Name.html. Places this data within
XHTML tags for output.
  • 1 lt _at_LANGUAGE VBScript gt
  • 2
  • 3 lt
  • 4 ' Fig. 25.5 name.asp
  • 5 ' Another simple ASP example
  • 6 Option Explicit
  • 7 gt
  • 8
  • 9 lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN"
  • 10 "http//www.w3.org/TR/xhtml1/DTD/xhtml1-tran
    sitional.dtd"gt
  • 11
  • 12 lthtml xmlns "http//www.w3.org/1999/xhtml"gt
  • 13
  • 14 ltheadgt
  • 15 lttitlegtName Informationlt/titlegt
  • 16
  • 17 ltstyle type "text/css"gt
  • 18 p font-family arial,
    sans-serif
  • 19 font-size 14pt color
    navy

15

Name.asp Program Output
16
Accessing a Database from anActive Server Page
  • Web applications
  • Communicating with databases
  • Use ActiveX Data Objects (ADO)
  • Provides uniform way for programs to connect with
    databases
  • Three-tier distributed applications
  • User interface
  • Created with XHTML, DHTML or XML
  • Contains ActiveX controls, client-side scripts,
    Java applets
  • Communicate directly with business logic
  • Business logic
  • Database access
  • May reside on separate computers

17
Accessing a Database from anActive Server Page
  • Web applications, cont.
  • Three-tier distributed applications, cont.
  • Web servers build middle tier
  • Provide business logic
  • Manipulates databases
  • Communicates with client Web browsers
  • ASP communications with databases
  • Use SQL-based queries
  • ADO handled specifics
  • Through OLE DB
  • Databases provide data source for dynamic content
  • Allows users who are not familiar with Web
    languages to create Web pages
  • Restrict access
  • Password protection
  • Query Access database

18
Database.aspconnects to, and queries an Access
databaseCreateObjectADODB.Connectioncontains
functionality necessary to connect to
databaseerrorHandlerLog
  • 1 lt _at_LANGUAGE VBScript gt
  • 2
  • 3 lt
  • 4 ' Fig. 25.22 database.asp
  • 5 ' ASP document for interacting with the
    database
  • 6 Option Explicit
  • 7
  • 8 Dim connection, loginData
  • 9
  • 10 ' provide error handling code
  • 11 On Error Resume Next
  • 12 Session( "errorString" ) ""
  • 13
  • 14 Set connection Server.CreateObject(
    "ADODB.Connection" )
  • 15 Call connection.Open( "login" )
  • 16 Call errorHandlerLog()
  • 17
  • 18 ' create the record set
  • 19 Set loginData Server.CreateObject(
    "ADODB.Recordset" )

19
Login.aspIdentifies users by prompting them for
login name and password. Data is stored in
Access database login.mdbsubmitlogin.asp
validates the users login
  • 1 lt _at_LANGUAGE VBScript gt
  • 2
  • 3 lt
  • 4 ' Fig. 25.23 login.asp
  • 5 ' ASP document to login to
    instantpage.asp
  • 6 Option Explicit
  • 7
  • 8 ' create the SQL query
  • 9 Session( "query" ) "SELECT loginID FROM
    Users"
  • 10 Call Server.Execute( "database.asp" )
  • 11 gt
  • 12
  • 13 lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN"
  • 14 "http//www.w3.org/TR/xhtml1/DTD/xhtml1-tran
    sitional.dtd"gt
  • 15
  • 16 lthtml xmlns "http//www.w3.org/1999/xhtml"gt
  • 17
  • 18 ltheadgt
  • 19 lttitlegtLogin Pagelt/titlegt

20
Login.aspPrompts user for login name and
password. Information is stored in Access
database opened in database.asp
  • 36 If Session( "errorString" ) "" Then
  • 37 ' if this is a return after a failed
    attempt,
  • 38 ' print an error
  • 39 If Session( "loginFailure" ) True
    Then gt
  • 40 ltp class "error"gtLogin attempt
    failed,
  • 41 please try
    againlt/pgt
  • 42 lt End If
  • 43
  • 44 ' begin the form gt
  • 45 ltpgtPlease select your name and enter
  • 46 your password to loginlt/pgtltbr /gt
  • 47
  • 48 ltform action "submitlogin.asp"
    method "post"gt
  • 49
  • 50 lt!-- format the form using a table
    --gt
  • 51 lttable border "0"gt
  • 52 lttrgt
  • 53 lttdgtNamelt/tdgt
  • 54

If false, line 89 prints error message to user
If true, login failure message prints to user and
prompts new login
21
Login.asp Prompts user for login name and
password. Information is stored in Access
database opened in database.asp
  • 71 lttrgt
  • 72 lttdgtPasswordlt/tdgt
  • 73 lttdgtltinput type "password"
  • 74 name "password"
    /gtlt/tdgt
  • 75 lt/trgt
  • 76
  • 77 lttrgt
  • 78 lttdgtlt/tdgt
  • 79 lttd align "left"gt
  • 80 ltinput type "submit"
    value "Log Me In" /gt
  • 81 lt/tdgt
  • 82 lt/trgt
  • 83 lt/tablegt
  • 84 lt/formgt
  • 85
  • 86 lt!-- include virtual"/includes/footer
    .shtml" --gt
  • 87 lt
  • 88 Else
  • 89 Call Response.Write( Session(
    "errorString" ) )

22
Login.asp Prompts user for login name and
password. Information is stored in Access
database opened in database.asp
  • 105 found False
  • 106
  • 107 While Not loginData.EOF
  • 108 ' create this record's dropdown
    entry
  • 109 gt ltoption
  • 110 lt ' if we did not write selected for
    any option
  • 111 ' before
  • 112 If ( Not found ) Then
  • 113
  • 114 ' if the current record's
    loginID is equal to
  • 115 ' the loginID cookie, then it is
    the loginID of
  • 116 ' the returning user, and thus
    we need to write
  • 117 ' selected for this option in
    this case we also
  • 118 ' need to signal that we have
    written selected
  • 119 ' for an option by setting found
    to True.
  • 120 If Request.Cookies( "loginID" )
    _
  • 121 loginData( "loginID" )
    Then
  • 122 Call Response.Write(
    "selected " _
  • 123 Chr( 34 ) "selected"
    Chr( 34 ) )

23
Login.aspProgram Output
  • 140 ' pull user names from the record set
    to populate the
  • 141 ' dropdown list
  • 142 While Not loginData.EOF
  • 143 ' create this record's dropdown
    entry
  • 144 gt ltoption value "lt loginData(
    "loginID" ) gt"gt
  • 145 lt loginData( "loginID" )
    gtlt/optiongt
  • 146 lt Call loginData.MoveNext()
  • 147 Wend
  • 148 End Sub
  • 149 gt

24
Program Output
25
Submitlogin.aspTakes values passed by login.asp
and checks values against Users table in
databaseIf match is found, user is redirected
to instantpage.asp. If not, user is redirected to
login.asp.
  • 1 lt _at_LANGUAGE VBScript gt
  • 2
  • 3 lt ' Fig. 25.24 submitlogin.asp
  • 4 ' ASP document to check user's username
    and password
  • 5 Option Explicit
  • 6
  • 7 ' test if a user name and a password were
  • 8 ' entered. If not, transfer back to the
    login page.
  • 9 If Request( "password" ) "" Or _
  • 10 Request( "loginID" )
    "noSelection" Then
  • 11 Session( "loginFailure" ) True
  • 12 Call Server.Transfer( "login.asp" )
  • 13 End If
  • 14
  • 15 Dim connection, loginData
  • 16
  • 17 ' create the SQL query
  • 18 Session( "query" ) _
  • 19 "SELECT FROM Users WHERE loginID
    '" _

If so, variable loginFailure set to true, client
redirected back to login.asp
26
Submitlogin.aspProgram Output
  • 36
  • 37 ' send them to instantpage.asp
  • 38 Call Server.Transfer(
    "instantpage.asp" )
  • 39 Else
  • 40 Session( "loginFailure" ) True
  • 41 Call Server.Transfer( "login.asp" )
  • 42 End If
  • 43 gt

27
Program Output
28
Accessing a Database from anActive Server Page
Fig. 25.25 Cookies folder before and after cookie
creation.
29
Accessing a Database from anActive Server Page
Fig. 25.26 Error messages sent to login.asp by
database.asp.
30
Server-Side ActiveX Components
  • ActiveX controls on the server with no GUI
  • Make features available in ASP
  • AdRotator ActiveX component
  • Rotates advertisements on a Web page
  • Randomly displays one of several advertisements
  • Minimizes space on a Web page committed to
    advertisements
  • Client does not have to support ActiveX
    technologies (on the server)
  • PageCounter ActiveX component
  • Page hit counter

31
Server-Side ActiveX Components
32
Component.aspUses AdRotator ActiveX component
to rotate one of five flag images. When user
clicks flag image, countrys corresponding CIA
Fact Book Web page displays.
  • 1 lt _at_LANGUAGE VBScript gt
  • 2
  • 3 lt
  • 4 ' Fig. 25.28 component.asp
  • 5 ' Demonstrating Server-side ActiveX
    Components
  • 6 Option Explicit
  • 7 gt
  • 8
  • 9 lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN"
  • 10 "http//www.w3.org/TR/xhtml1/DTD/xhtml1-tran
    sitional.dtd"gt
  • 11
  • 12 lthtml xmlns "http//www.w3.org/1999/xhtml"gt
  • 13
  • 14 ltheadgt
  • 15 lttitlegtActiveX Component
    Examplelt/titlegt
  • 16 lt/headgt
  • 17
  • 18 ltbodygt
  • 19

33
Component.asp
  • 35 ' create a BrowserType object
  • 36 Set browser Server.CreateObject(
    "MSWC.BrowserType" )
  • 37
  • 38 If browser.VBScript True Then
  • 39 gt
  • 40 ltscript language "VBScript"gt
  • 41 Call Msgbox( "Client browser
    supports VBScript!" )
  • 42 lt/scriptgt
  • 43 lt
  • 44 End If
  • 45
  • 46 If browser.JavaScript True Then
  • 47 gt
  • 48 ltscript language "JavaScript"gt
  • 49 alert( "Client browser supports
    JavaScript!" )
  • 50 lt/scriptgt
  • 51 lt
  • 52 End If
  • 53

34
Component.asp
  • 69 ' create Page Counter Object
  • 70 Set counter Server.CreateObject(
    "MSWC.PageCounter" )
  • 71 Call counter.PageHit() ' page has been
    "hit"
  • 72 gt
  • 73 lt/pgt
  • 74
  • 75 ltp style "color blue font-size
    12pt"gt
  • 76 This page has been visited lt
    counter.Hits() gt
  • 77 times!lt/pgt
  • 78 lt/bodygt
  • 79 lt/htmlgt

35
Program Output
36
Program Output
37
Config.txtDescribes the advertisements
  • 1 REDIRECT redirect.asp
  • 2 width 54
  • 3 height 36
  • 4 border 1
  • 5
  • 6 /images/us.gif
  • 7 http//www.odci.gov/cia/publications/factbook
    /geos/us.html
  • 8 United States Information
  • 9 20
  • 10 /images/france.gif
  • 11 http//www.odci.gov/cia/publications/factbook
    /geos/fr.html
  • 12 France Information
  • 13 20
  • 14 /images/germany.gif
  • 15 http//www.odci.gov/cia/publications/factbook
    /geos/gm.html
  • 16 Germany Information
  • 17 20
  • 18 /images/italy.gif
  • 19 http//www.odci.gov/cia/publications/factbook
    /geos/it.html

38
Redirect.aspRedirects user to countrys CIA
page when ad is clicked
  • 1 lt _at_LANGUAGE VBScript gt
  • 2
  • 3 lt
  • 4 ' Fig. 25.30 redirect.asp
  • 5 ' Redirection Page for AdRotator
    Component
  • 6 Option Explicit
  • 7
  • 8 Call Response.Redirect( Request( "url" )
    )
  • 9 gt
Write a Comment
User Comments (0)
About PowerShow.com