Title: Active Server Pages ASP
1Active Server Pages (ASP)
?????? Windows 2000Server ??? Professional
IIS ?????? Windows 2000 TCP/IP IE 5.0..
??????????? Chili!ASP (ASP, Netscape ? ??.
Windows NT server) InstantASP ( NT,
Novell, AIX, AS/400, Linux, OS/2, Apache,
Netscape, ..) ASP ? IIS asp.dll ?? ?????????
?????? ? IIS ? Winnt\System32\inetsrv
default web site C\InetPub\WWWRoot\.. ???????
? ???? VBScript Jscript lt_at_LANGUAGE???_????
gt
- ????? Request
- ????? Response
- ????? Application
- ????? Session
- ????? Server
- ????? ASPError
- ????? ObjectContext
???????? ??????
2Active Server Pages (ASP)
3Active Server Pages (ASP)
Application Object
???????? ???????? ??????????. ?? ???????? ??
???. ????? ?????? users ??? ????? ?? ?????????
?? ????????? (??????????? ?????????, ????????)
ASP ?????????? 1. ???????? ?????????
?????????? (Variant), ????????? ??? COM ?????
(Variant), ?????? 2. ???? ?????????? ??
??????????
Collections
4Active Server Pages (ASP)
Methods
Events
????????? ???? ? global.asa, ? ??????????
?????????? ?? ????????????
Request Object
???????? ?? ?? ?????? ?? ???. ????????? ? HTTP
request parameters ????????? ?? HTML form ? POST
method ??? GET method, cookies ??? client
certificates.
5Active Server Pages (ASP)
???????? ?????? ?? ???????? 23/09/02 102222
Sent GET /store/xxx.asp HTTP/1/1 Accept
application/msword, . Accept-Language
en-us Encoding . Refererhttp//www.xxxx Cook
ie VisitCount2LASTDATE.. User-Agent
Mozilla/4.0 Host xxx.eee.www.eee ConnectionKeep-
Alive ???????? ??????? 7/09/02
102233 Received HTTP/1.1 200OK Server
Microsoft IIS/5.0 ConnectionKeep-Alive Date Con
tent-Type text/html Content-Length3333 last-Modi
fied . Cookie VisitCount3 ltHTMLgt. lt\HTMLgt
6Active Server Pages (ASP)
Collections
Properties
7Active Server Pages (ASP)
Methods
Response Object
?? ?????? ?? ???., ????????? ??? ?????????? (
????, ?????????????? ???????, ???????????
??????????, ?????????, ?????? ?? ????????? ??
?????) ???????? ??? browser, ???????????? ???
browser ?? ???? URL ??? ????????? ?? cookie
values
Collections
Properties
8Active Server Pages (ASP)
Methods
Server Object
?? ?????? ?? methods ? properties ? server.
???-????? ????????? ????? ? ?? ?????????
instance ?? COM component (Server.CreateObject).
????? ?????? ?? ?? HTML encoding, map virtual
paths to physical paths, ???????? timeout period
?? ???????.
9Active Server Pages (ASP)
Methods
Session Object
???????? ?? ?? ??????????? ?????????? ?? ???????
??????????/?????. ????????????, ????????? ?
Session object ?? ???????? ??? ???????
????? ???????? ? ????????????. Session methods
???????? ??????????? ???? ?? session ? timeout
period ??? ????????? session. ?????????? ??????
??????, ????? Application object. ????? ??? ????
??? ????????? ?????????
10Active Server Pages (ASP)
Collections
Properties
Methods
Events
Scripts ?? ???? events ?? ???? ???? ? Global.asa
????
11Active Server Pages (ASP)
ObjectContext Object
???????? ?? ?? commit ??? abort ?? ??????????,
???????? ? ASP script. ???? ?????? ?? ??????
???????? ASP ?????????? (????? ?? ???????? ?????
?? ASP). ?????? ? ???????? Set objMyContext
GetObjectContext objMyContext.Response.WriteThis
text is to be written into the page
Events
?????? ?? ???????? ASP ?????? ?? ?????? ?????
? IIS 3.0, ???????? ?????????? ?????????? ?????
?? ???? ?????? ?? ASP ?????????? ?????? ????
IScriptingContext interface. ??????? ?? ????????
??????, ?? ??? ??-????? ???????????
ObjectContext Object . ???? ? ???????????? ???
?? ????????? ?????????? ??? ?????????? ?????? ??
????????????? ??????????. ??? ???, ????????????
?????? ?? Response.Write method ?? ????????
?? Visual Basic.
12Active Server Pages (ASP)
Public Function GetResponse() Dim vntCurrent As
Variant Dim curres As ObjectContext Set
curres GetObjectContext () vntCurrent
curres("Response").Write ("Hello World") End
Function
ASPError Object
?? ?????????? ???????????? ??? ??? ASP error.
???????? ? ???? GetLastError ?? ?????? Server.
Properties
13Active Server Pages (ASP)
?????????? ?? ASP ?????? ??? Visual C
?????????? ?? ????????? ?? Visual C ??????????
???????? ???????????, ???????? ?????? ??
??????????? ?? COM ?? ????????? ?? ?????????
?? ??????????. ???? ? ?????? ?????? ??????? ?
Visual Basic. ????? ??????????? ?????? ?? ASP
????? - Response ?? C .
STDMETHODIMP CHelloWorldGetResponse() //
????????? Object Context CComPtrltIObjectContex
tgt pObjContext HRESULT hr
GetObjectContext(pObjContext) if
(SUCCEEDED(hr)) // ????????? Properties
interface CComPtrltIGetContextPropertiesgt
pProps hrpObjContext-gtQueryInterface(IID_IGetC
ontextProperties, (void) pProps)
if (SUCCEEDED(hr)) // ?????
ASP Response object CComBSTR
bstrResponse("Response") CComVariant
vt hr pProps-gtGetProperty(bstrRespo
nse, vt)
14Active Server Pages (ASP)
if (SUCCEEDED(hr)) //??????.???????? ???
IDispatch ??? ???????? ?? IResponse if
(V_VT(vt) VT_DISPATCH )
CComPtrltIResponsegt piResponse
IDispatch pDispatch V_DISPATCH(vt)
if (pDispatch ! NULL) hrpDispatch-
gtQueryInterface(IID_IResponse, (void)piResp
onse) if (SUCCEEDED(hr)) piR
esponse-gtWrite(CComVariant(OLESTR("Hello,
World!")))
return hr
Visual C ?????????? Active Template Library
(ATL), ??????????? ??????????? ?? COM
??????????.
??????? .asp ? HTML script (VBScript,
JavaScript ...), ????????? ? ???????. ??? ???????
?? ????? HTML, DHTML, ActiveX, scripts, Java
applets.
15Active Server Pages (ASP)
?????? ? ????????
ltHTMLgt ltHEADgt ltTITLEgtA Simple ASP
Examplelt/TITLEgt ltMETA HTTP-EQUIV"REFRESH"
CONTENT"60 URLCLOCK.ASP"gt lt/HEADgt ltBODYgt ltFONT
FACEARIAL SIZE 4gtltSTRONGgtSimple ASP
ExampleltSTRONGgt lt/FONTgtltPgt ltTABLE BORDER"6"gt
ltTRgt ltTD BGCOLOR"000000"gt ltFONT
FACEArial COLOR"00FF00" SIZE4gt lt
Time() gt //???????? lt call
Response.Write( Time()) gt lt/FONTgt
lt/TDgt lt/TRgt lt/TABLEgt lt/BODYgt lt/HTMLgt
16Active Server Pages (ASP)
- ????????? ?????? Scripting Runtime ????. ?????
? ???????????? ?? IIS/ASP. - ??????? ?? ? scrrun.dll
- ????? Dictionary ????????? ? ????????? ?????
(????/????????) - Dim obj
- Set obj Server.CreateObject(Scripting.Dictiona
ry) - ????? FileSystemObject. ?? ?????? ??
????????? ??????? ?? ??????? - (???? ? ?? ???????)
- Dim objFSO
- Set objFSO Server.CreateObject(Scripting.FileS
ystemObject) - ????? TextStream. ??????? /???? ? ???????
(??????????????) ???? ?? ????. - ?????? ???? ? FSO
- ???????? ??????????
- ? ????????? dll. ? ???????????? ?? IIS/ASP
????? - Ad Rotator ?? ?????????? ?? ???????????
(???????) ? ?????????? - Brouser Capabilities ???? ???? UserAgent
???????? ?? brouser. ????????? - ????????????? ?? ????????.
- Counters ????????? ??????, ????? ????? ?? ??
????????? ? ??????? ??. - Logging Unility ???? ?????? ?? log ?????????
?? ???????. - Page Counter ????? ???? ????????? ??
????????. - Permission Checker ????????? ????? ??? ????
?? ?????? ?? ?????? ?? - ??????????.
- ...
17Active Server Pages (ASP)
File System Objects (FSO) ?????????? ?????/
?????? ?? ?????, ?????? ? ???????, ??????????,
???????? ? ???????. ???????? ??
FileSystemObject, File, Folder, Drive,
TextStream. ????? ??? ????? 20 ??????. ?????????
Set fso CreateObject( "Scripting.FileSystemObje
ct" ) ???????? ????? ? ???????????? guestBook
"c\webshare\wwwroot\deitel_iw3htp\"
guestbook.txt" ' ????????? ???? ??????
??????????, ??? ?? ?? ??????? If
fso.FileExists( guestbook ) ltgt True Then
Call fso.CreateTextFile( guestBook ) End
If Set tso fso.OpenTextFile( guestbook, 8, True
)
?????????? ? ????????? ?? ???????? ?????????? ???
??????? ?? ????? ? guestbook.txt mailtoUrl
Request( "pubdate" ) " ltA HREF " Chr( 34 )
mailtoUrl mailtoUrl "mailto" Request(
"email" ) mailtoUrl mailtoUrl Chr( 34 )
"gt" Request( "name" ) mailtoUrl
mailtoUrl "lt/Agt " ' Send the
information to guestbook.txt Call
tso.WriteLine( "ltHR COLOR BLUE SIZE 1gt" )
Call tso.WriteLine( mailtoUrl ) Call
tso.WriteLine( Request( "comment" ) ) Call
tso.Close()
18Active Server Pages (ASP)
???????? ?????????? ? .asp ????? ltFORM ACTION
"guestbook.asp?entrytrue" METHOD "POST"gt
- ?????? ?? users ????? ?? ?????????? guestbook.
???????????? ?? - ????????? ???? ?? ?????? ?? ?????????? HTML
- lt
- Dim fso2, tso2
- Set fso2 Server.CreateObject(
"Scripting.FileSystemObject" ) - guestBook "c\webshare\wwwroot\myfolder\"gue
stbook.txt" - If fso2.FileExists( guestBook ) True Then
- ' Open the guestbook, "1" is for reading
- Set tso2 fso2.OpenTextFile( guestbook, 1
) - Call Response.Write( "Guestbook
EntriesltBRgt" ) - Call Response.Write( tso2.ReadAll() )
- Call tso2.Close()
- End If
- gt
- ??????? ?? ??????? ?? ??????? ? cookies
- WEB ???????? ?????????? ??????????? ? ?????????
??????????? ?? ???????? - ?? ????? ?? ???? ????? (????? ? yahoo.com).
- ??? ??? site ?? ???????? ?????? ?? ??????????
??????? ?? ?? ???????? - ??????????? ?? ? ?.?.
19Active Server Pages (ASP)
Session tracking- ? ???????, session ID, ????????
?? ????? Session, cookies ????? ???????,
??????????? ? ???????, ?? ?????????? ?? ??????
???? asp. ???? ??? ???????? ????????? ???????
??????? ???????????? ?????????? (???????? ????? ?
????????? ?? ????? ??. ??? ???????? ????????) ?
?? ??????? ????????????? ?? ???????? ??
??????????. ??????????? ? ??????????? ?? cookies
????? ? ASP ??? , Perl/CGI ? ??. ????? HTTP
???????. ????? ??????? ? ?????? ??????? header,
???????? ???? ?? ???????? (GET, POST) ?
?????????? ?? cookies ??????????? ? ???????.
??????? ????? ??????? ?? ????? ? HTTP ???
??????? ???. ?? cookies, ????? ??????? ???? ?? ??
??????? ? ???????.
?????? ?? ????????? ? ?? ?? ASP ????????
????????? ?????? users ?? ??????. ? ?????????
???????? (????????? ?? login.asp), ?? ???? ?????
???????? ?? ???? ?????? ?? ??????????
instantpage.asp. ?????????? ?? ???????? ????? ?
??????? submitlogin.asp ?? ?????? ???????????? ??
ID ? ??????, ????????? ?? Access ?? ? cookie.
???????? ???????? ? HTML (script ???????? ?
??????? ?SQL ??????) ?ASP? ADO ?? ?? ? ?????
?????? ????????? ?????? PWS, asp ????????? ?
??????. ?? ????????????? ?? root ?? ????, inc.
????????? ? ??????. include ?? ???????. ?? ??
????, ????????????? ?? ????? ?? ???????? (??
?????? .mdb) ???? DSN ?? ???????.
20Active Server Pages (ASP)
login.asp
lt _at_LANGUAGEVBScript gt lt Option Explicit gt
Dim dbConn, dbQuery, loginRS, loginFound If
IsObject( Session( "mothergoose_dbConn" ) ) Then
Set dbConn Session( "mothergoose_dbConn"
) Else Set dbConn Server.CreateObject(
"ADODB.Connection" ) Call dbConn.Open(
"mothergoose", "", "" ) ???? user ID, pasw.
Set Session( "mothergoose_dbConn" ) dbConn
End If dbQuery "SELECT FROM users"
?????????? SQL ?????? Set loginRS
Server.CreateObject( "ADODB.Recordset" ) Call
loginRS.Open( dbQuery, dbConn ) ????????? ??????
?? ? loginRS On Error Resume Next ' ???
?????? ?????????? ? Call loginRS.MoveFirst()
gt lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt ltHTMLgt ltHEADgt ltTITLEgtLogin
Pagelt/TITLEgt lt/HEADgt ltBODYgt lt!-- include
virtual"/includes/mgtheader.inc" --gt
21Active Server Pages (ASP)
lt If Session("loginFailure") True Then gt
??????? ??? ????????? ???? ltFONT SIZE 4 COLOR
"red"gt ?????? ??????, ???????? ??? ltPgtlt/FONTgt
lt End If gt ltFONT FACE "arial" SIZE 2gt //
???????? ? ??? Please select your name and enter
your password to loginltBRgt lt/FONTgt ltFORM NAME
sublogform ACTION "submitlogin.asp" METHOD
POSTgt // ??? ?? ???????? ? ???????
submitlogin.asp ltTABLE BORDER 0gt ltTRgt ltTDgtltFONT
FACE "arial" SIZE 2gtNamelt/FONTgtlt/TDgt ltTDgtltSEL
ECT NAME "LOGINID"gt ltOPTION VALUE
"000"gtSelect your name lt While Not
loginRS.EOF If Session( "loginid" )
loginRS( "loginid" ) Then loginFound
"selected " End If ' ??? ? ?????? login
cookie (??????? ?? ?? submitlogin.asp) ????????
?? If Request.Cookies( "loginid" )
loginRS( "loginid" ) Then loginfound
"selected " End If
22Active Server Pages (ASP)
?? ????? ????? ??????? ??????????? ?????? gt
ltOPTION lt loginFound gt value"lt loginRS(
"loginid" ) gt"gt lt loginRS( "loginid" ) gt
lt loginfound " " gt lt Call
loginRS.MoveNext() Wend gt lt/SELECTgt
lt/TDgt lt/TRgt ltTRgt ltTDgtltFONT FACE "arial"
SIZE "2"gtPasswordlt/FONTgtlt/TDgt ltTDgtltINPUT
TYPE "password" NAME "SUBMIT_LOGIN"gtlt/TDgt
lt/TRgt ltTRgt ltTDgtnbsplt/TDgt ltTD ALIGN
"LEFT"gtltINPUT TYPE "submit" VALUE "Log Me
In ID "login1" NAME "login1"gtlt/TDgt
lt/TRgt lt/TABLEgt lt/FORMgtlt/FONTgt lt!-- include
virtual"/includes/mgtfooter.inc"
--gt lt/BODYgt lt/HTMLgt
23Active Server Pages (ASP)
??? ?????? submitlogin.asp, ????????? ??
login.asp ? ???????? ??????????, ????? ?????????
?? ?????????? ? ??. ??? ?????????? ?? ????????
????? ????? instantpage.asp (?? ? ???????? ).
??? ???????????? ??????? ? login.asp.
lt _at_LANGUAGEVBScript gt lt Option Explicit
gt lt Dim dbConn, dbQuery, loginRS If
IsObject( Session( "mothergoose_dbConn" ) ) Then
Set dbConn Session( "mothergoose_dbConn"
) Else Set dbConn Server.CreateObject(
"ADODB.Connection" ) Call dbConn.Open(
"mothergoose", "", "" ) Set Session(
"mothergoose_dbConn" ) dbConn End If
dbQuery "SELECT FROM users ??????? SQL
???????? ??????? recordset Set loginRS
Server.CreateObject( "ADODB.Recordset" )
??????? recordst ??? ????????? ?????? Call
loginRS.Open( dbQuery, dbConn ) ' ????????
???????? On Error Resume Next
Call loginRS.MoveFirst()
24Active Server Pages (ASP)
If Request( "loginid" ) ltgt "" Then
loginid ? ?? ??????? ?? name ?????? While
Not loginRS.EOF If Request( "loginid" )
loginRS( "loginid" ) AND _ Request(
"submit_login" ) loginRS( "password" ) Then
Session( "loginfailure" ) False
cookie ? ??? loginid ? ???????
Response.Cookies( "loginid" ) Request(
"loginid" ) ??. ? ??? ???????? ????????
Call Response.Redirect( "instantpage.asp" ) ?
????? ????. ?? ???????? ????? ? ?????? ?? ??????
? .mdb ?????? End If Call
loginRS.MoveNext() Wend End If '
??? loginid ? ??????? ???? ??? ???????
Session( "loginFailure" ) True ????? ?? ??
???? ????????? Call Response.Redirect(
"login.asp" ) gt
25Active Server Pages (ASP)
???????? ?? .asp, ?????????? ????????? ???? ADO
??? mothergoose.mdb, ?????????????? ???? DSN ????
lt Dim conn, sql, rs, titleTest, image If
IsObject( Session( "mothergoose_conn" ) ) Then
Set conn Session( "mothergoose_conn" )
Else Set conn Server.CreateObject(
"ADODB.Connection" ) Call conn.Open(
"mothergoose", "", "" ) Set Session(
"mothergoose_conn" ) conn End If '
Prepare the SQL statement sql "SELECT
product.title, language.language " "FROM
language " _ "INNER JOIN (product
INNER JOIN translations ON " _
"product.productID translations.ProductID) ON "
_ "language.languageid
translations.languageID " _ "ORDER BY
translations.ProductID, " " translations.languag
eID" Set rs Server.CreateObject(
"ADODB.Recordset" ) Call rs.Open( sql, conn )
Call rs.MoveFirst() ' Goto first
record gt
26Active Server Pages (ASP)
?????????? ?? ASP ? ADO ? ??
? IIS 5.0 ( Windows 2000 Server), ADO Recordset
objects ????? ?? ?? ?????????? ???/?? ASP
response ??? request ???????? ????????. ????????,
???? ????? ????????? ?? ?? ????????? XML ?? ADO
Recordsets
lt_at_ codepage"65001" gt ltobject id"rstCustomers"
progid"ADODB.Recordset" runat"Server"gtlt/obj
ectgt lt!--metadata name"Microsoft ADO 2.5
Library" type"TypeLib" uuid"00000205-0000-0010
-8000-00AA006D2EA4"--gt lt Dim strSQL,
strConnection Response.ContentType
"text/xml" strSQL "SELECT CustomerID,
CompanyName, ContactName FROM Customers
ORDER By CompanyName" strConnection
"ProviderMicrosoft.Jet.OLEDB.4.0Data
Source Server.MapPath("NWind.mdb") rstCusto
mers.CursorLocation adUseClient
rstCustomers.Open strSQL, strConnection,
adOpenStatic, adLockOptimistic Response.Write
"lt?xml version"1.0" encoding"ISO-8859-1"?gt"
vbCRLF rstCustomers.save response,
adPersistXML rstCustomers.Close gt
27Active Server Pages (ASP)
????? ??, Recordset ???? ???????? ?? ?? ??????? ?
ASP response object, ??? ??????? ??????? I/O
ope?????. ?????? ???? ASP ???????? ?? ?????? ?
user's browser, XML ????????????? ADO Recordset
?? ??????? ? ?????????? ??? ???????/.
????????? ????????? ?? ?????????? ?
?????? Server.CreateObject(ADODB.Connection) ???
ltOBJECT ID obj RUNATSERVER
PROGIDObject ltPARAM Name ?????1 VALUE
????????gt lt/OBJECTgt ?????? ?? ??????????? ??
???????????? ???????? ??? lt?BJECTgt e ?
global.asa ?????? ????? ??? ??????????
(SCOPE) ??????? Server.CreateObject()
??ltOBJECTgt ?????????? ?????? ?????????? ??
???????????? ?? ?????? ?? ????????? lt! --
METADATA TYPE TypeLib FILE???_?????... -- gt