1. §Æc ®iÓm:
Tªn file: .asp
-
Ph¶i cµi thªm Microsoft Active Server Page ®Ó bæ trî cho Web Server, chuyªn xö lý c¸c script viÕt trong file .asp. Cho phÐp viÕt øng dông theo kiÓu Server Side => c¸c trang web sÏ sÏ ®îc xö lý t¹i Server tríc khi göi xuèng Client.
-
C¸c m· lÖnh ®îc chÌn vµo trong kh¾p trang web vµ ®îc xö lý tuÇn tù. M· lÖnh lµ c¸c Script, hiÖn ASP cho phÐp dïng 2 srcript lµ VBScript hoÆc JavaScript. Lùa chän Script ®îc ®Æt ngay t¹i dßng ®Çu file:
<%@LANGUAGE = “VBSCRIPT”%>
<%@LANGUAGE = “JSCRIPT”%>
NgÇm ®Þnh lµ VBScript.
-
C¸c ®o¹n m· kh«ng ph©n biÖt dµi ng¾n ®îc ®Æt trong dÊu hiÖu: <% %>
-
C¸c biÕn kh«ng cÇn khai b¸o mµ tù khi g¸n gi¸ trÞ cho nã sÏ x¸c ®Þnh biÕn.
-
§Ó in ra c¸c biÕn trong trang web sö dông nh m· sau:
<% =s %>
-
§Ó lµm viÖc víi hÖ thèng file, thao t¸c víi Database... => cÇn ph¶i sö dông c¸c ®èi tîng kiÓu Active X cña ASP hay cµi ®Æt thªm vµo.
-
§Ó lÊy c¸c gi¸ trÞ truyÒn tõ client lªn dïng ph¬ng thøc request.
Ex: s = request(“Company”)
=> LÊy gi¸ trÞ cña biÕn Company.
2. Sö dông c¸c ®èi tîng:
ASP cho phÐp sö dông c¸c ®èi tîng kiÓu Active X. §Ó khai b¸o ta dïng ph¬ng thøc CreateObject cña ®èi tîng Server cã s½n.
Ex: Set f = Server.CreateObject(“Scripting.FileSystemObject”);
Tæng quan: Set param = Server.CreateObject(“PacketName.ClassName”)
ProgID
Sau ®ã cã thÓ sö dông c¸c hµm cña ®èi tîng ®ã.
§Ó gi¶i phãng dïng Set f = nothing
3. Truy nhËp hÖ thèng file:
<%
Dim objFile
Dim objStream
Set objFile = Server.CreateObject(“Scripting.FileSystemObject”);
Set objStream = objFile.OpenTextFile(Server.Mapath(“/ASP”) &_
“\Chapter05\Listing 5-9\tips.txt”)
Randomize Timer
IntLine = Int(Rnd*19)
For i=0 to intLine
ObjStream.SkipLine
Next
StrTxt = objStream.ReadLine
ObjStream.Close
Set objFile= Nothing
Set objStream= Nothing
%>
=> §Ó t¹o file míi dïng ph¬ng thøc CrateTextFile cña objFile.
§Ó ghi mét dßng v¨n b¶n dïng objStream.WriteLine s.
4. Truy nhËp c¬ së d÷ liÖu:
<%
Dim objConnection
Set objConnection = Server.CreateObject(“ADODB.Connection”)
ObjConnection.Open “Publication”, ”sa”, “”
Dim objRecordSet
Set objRecordSet = Server.CreateObject(“ADODB.RecordSet”)
ObjRecordSet.Open “Select pub_name From Publishers”, objConnection
%>
-
§èi tîng kiÓu Connection: t¹o kÕt nèi ADO tíi Database.
-
§èi tîng kiÓu RecordSet: cho phÐp thao t¸c trªn table (select,update ...) cña Connection ®· thiÕt lËp.
<% Do While Not objRecordSet.EOF %>
<% = objRecordSet(“pub_name”)%>
<% objRecordSet.MoveNext
Loop
ObjRecordSet.close
ObjConnection.close
Set objRecordSet = nothing
Set objConnection = nothing %>
-
Chó ý: khi thªm, cËp nhËt d÷ liÖu ngoµi viÖc dïng SQL, cã thÓ thªm theo c¸ch :
With objRecordSet do
.AddNew
.Field(”Authors”) = “Homes”
. Field(”Title”) = “Iliad”
.Update
End With
-
Cã thÓ kÕt nèi trùc tiÕp kh«ng qua DSN cña ODBC nh sau :
<%
Set objConnection = Server.CreateObject(“ADODB.Connection”)
StrCnn = “driver = {SQL Server}; Server = smas; uid = sa; pwd= ; database= pubs”
objConnection.Open strCnn
%>
5. Ph©n trang RecordSet
Khi select => cho kÕt qu¶ 1 b¶ng gåm nhiÒu hµng. Tuy nhiªn mçi lÇn chØ muèn sö dông mét vµi hµng, vÝ dô: b¶ng 50 hµng, mçi lÇn hiÓn thÞ 10 hµng => sö dông kü thuËt paging. ADO cung cÊp c¸c thuéc tÝnh cña Recordset nh: PageSize, PageCount, AbsolutePage.
PageCount: sè trang, PageSize: sè hµng.
ë ®©y ta dïng biÕn session CurrentPage ®Ó ghi nhËn trang hiÖn hµnh.
<% Select Case Request.QueryString(“Direction”)
Case “” ‘Null
Session(“CurrentPage”) = 1
Case “Next”
Session(“CurrentPage”) = Session(“CurrentPage”) + 1 Case “Previous”
Session(“CurrentPage”) = Session(“CurrentPage”) - 1
End Select %>
-
Chó ý: Request.QueryString(“Direction”) <=> QueryString(“Direction”)
TiÕn hµnh kÕt nèi truy vÊn d÷ liÖu.
<%
const adOpenKeySet = 1
dim objConnection kh«ng cÇn khai b¸o còng ®îc
dim objRexordset
Set objConnection = Server.CreateObject(“ADODB.Connection”)
Set ObjConnection.Open “Biblio”, “”, “”
Dim strSQL
StrSQL = “Select * from Authors”
Set objRecordSet = Server.CreateObject(“ADODB.RecordSet”)
ObjRecordSet.PageSize = 10
ObjRecordSet.Open strSQL, objConnection, adOpenKeyset
ObjRecordSet.AbsolutePage = CLng(Session(“CurrentPage”))
%>
Page = <%=Session(“CurrentPage”) %> of <%=ObjRecordSet.PageCount>
<% Dim i
For i = 1 to ObjRecordSet.PageSize %>
<% = ObjRecordSet(“Authors”) %>
...
<% ObjRecordSet.MoveNext
Next %>
<% if CLng(Session(“CurrentPage”)) < ObjRecordSet.PageCount Then %>
Next Page
<%Enf if%>
<% If CLng(Session(“CurrentPage”)) > 1 then %>
Previous Page
<%Enf if%>
<% ObjRecordSet.close
ObjConnection.close
Set objRecordSet = nothing
Set objConnection = nothing %>
6. §èi tîng Application:
§èi tîng nµy cho phÐp t¹o c¸c biÕn øng dông. §ã lµ c¸c biÕn cho tÊt c¶ c¸c user cña mét øng dông web. TÊt c¶ c¸c user mµ yªu cÇu c¸c trang web tõ mét th môc web cã thÓ chia xÎ víi nhau c¸c biÕn ®Þnh nghÜa trong c¸c trang ®ã.
<% Application.Lock
Application(“Company”) = “VASC”
Application.Unlock %>
ë ®©y ®Þnh nghÜa 1 biÕn lµ Company, cã gi¸ trÞ lµ VASC. V× r»ng biÕn cã thÓ bÞ tranh chÊp nªn ph¶i ®Æt gi÷a Lock vµ Unlock.
<% Application.Lock
Application(“Time”) = Now
Application.Unlock %>
7. §èi tîng Session:
Cho phÐp t¹o ra c¸c biÕn øng víi tõng ngêi dïng. Ch¼ng h¹n biÕn cã tªn lµ UserName, øng víi mçi user cã mét gi¸ trÞ cô thÓ kh¸c nhau, ph©n biÖt. Nhng khi sö dông th× tæng qu¸t cho c¸c user. Thùc ra mçi biÕn sÏ ®îc server ph¸t sinh vµ g¸n cho nã mét gi¸ trÞ ®Þnh danh GUID (Globally Unique Identifier, 128 bit) vµ göi tíi Browser. Browser sÏ lu tr÷ GUID ®ã vµ sö dông ®Ó yªu cÇu d÷ liÖu tõ biÕn cã GUID t¬ng øng trªn Server.
<% Session(“Company”) = “NewTech”
Session(“Email”) = “leha@hotmail.com” %>
My Company : <% Session(“Company”) %>
Email : <% Session(“Email”) %>
C¸c biÕn Session nµy sÏ bÞ huû bá sau mét kho¶ng thêi gian (kho¶ng 20’) ngêi dïng th«i truy xuÊt c¸c trang web trong th môc web.
-
Chó ý lµ trong file global.asa => cho ph¸p ®Æt c¸c biÕn, code vµo c¸c thñ tôc Session_OuStart vµ Session OuEnd. VÝ dô:
8. §èi tîng Request:
§Ó göi d÷ liÖu lªn Server, ta sö dông Form trong trang Web. Tªn cña c¸c ®åi tîng web nh Text, Button n»m trong form sÏ ®îc dïng nh tªn biÕn khi ph©n tÝch, lÊy d÷ liÖu trªn Server. Trong form cã button kiÓu Submit ®Ó ngêi dïng ®Èy d÷ liÖu lªn Server.
Form cã hai thuéc tÝnh lµ Action vµ Method. Action quy ®Þnh file (.asp) mµ sÏ ®ãn nhËn vµ xö lý d÷ liÖu ®Èy lªn. method quy ®Þnh c¸ch thøc göi d÷ liÖu; nÕu lµ POST th× tÊt c¶ d÷ liÖu trong form sÏ ®ãng gãi vµ ®Èy lªn Server; NÕu lµ GET th× d÷ liÖu ®îc göi ®i nh mét phÇn cña URL, thêng lµ sau dÊu “?”.
|