Tải bản đầy đủ (.pdf) (5 trang)

Đại Cương Về Thiết Kế Web Và Lập Trình Web- P21 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (181.17 KB, 5 trang )


101
Kết quả:
How now pink cow?
Server.GetLastError
Trả lại đối tợng ASPError, đối tợng ny có 9 thuộc tính chỉ đọc cung cấp
thông tin chi tiết về lỗi.
Ví dụ:
<%
Dim objErrorInfo
Set objErrorInfo = Server.GetLastError
Response.Write("ASPCode = " & objErrorInfo.ASPCode)
Response.Write("ASPDescription = " & objErrorInfo.ASPDescription)
Response.Write("Category = " & objErrorInfo.Category)
Response.Write("Column = " & objErrorInfo.Column)
Response.Write("Description = " & objErrorInfo.Description)
Response.Write("File = " & objErrorInfo.File)
Response.Write("Line = " & objErrorInfo.Line)
Response.Write("Number = " & objErrorInfo.Number)
Response.Write("Source = " & objErrorInfo.Source)
%>

Server.HTMLEncode (String)
Phơng thức này cho phép mã hoá chuỗi thành mã HTML đối với xâu ASCII
bất k. Ví dụ, điều này cho phép hiển thị thẻ HTML mà không xử lý nó nh những
thẻ HTML thực sự.
Ví dụ:
<% Response.Write Server.HTMLEncode("The tag for a table is: <Table>") %>
Kết quả:
The tag for a table is: &lt;Table&gt;
Kết quả ở Browser:


The tag for a table is: <Table>
Server.MapPath (Path)
ánh xạ đờng dẫn ảo, hay tơng đối tới đờng dẫn vật lý. Phơng thức này
không kiểm tra sự tồn tại thực sự của đờng dẫn. Nếu bắt đầu bằng dấu / hoặc \ ->
đờng dẫn ảo. Còn không bắt đầu bằng ký tự đó -> đờng dẫn tơng đối.
Ví dụ:
<HTML>
<BODY>
The path of this file is <% Response.Write
Server.MapPath("test.asp")%>
The path of the file1 is <% Response.Write
Server.MapPath("\test.asp")%>
The path of the file2 is <% Response.Write
Server.MapPath("test\test.asp") %>

102
The path of the file3 is <% Response.Write Server.MapPath("\") %>
</BODY>
</HTML>
Kết quả:
The path of this file is C:\VANBANG2\ASP\Example\test.asp
The path of the file1 is d:\inetpub\wwwroot\test.asp
The path of the file2 is C:\VANBANG2\ASP\Example\test\test.asp
The path of the file3 is d:\inetpub\wwwroot
Server.Transfer (Path)
Chuyển tới trang ASP khác từ một trang ASP. Thông tin trạng thái khởi tạo
trong trang đầu tiên sẽ đợc chuyển tới trang thứ hai.
Ví dụ:
CallingAsp.asp
<%

Application("name") = "Application Maker"
Application("publishdate") = "05/15/01"
Application("author") = "DevGuru"
Set Application("Obj1") =
Server.CreateObject("ADODB.Connection")
Server.Transfer("CalledAsp.asp")
%>

CalledAsp.asp
<%;
Response.Write "Output from CalledAsp.asp"
For Each Item in Application.Contents
If IsObject( Application.Contents(Item)) Then
Response.Write Item & is an object.<BR>
Else
Response.Write Item & = & Application.Contents(Item) &<BR>
End If
Next
%>

Kết quả:
Kết quả từ CalledAsp.asp
name=Application Maker
publishdate=05/15/01
author=DevGuru
OBJ1 is an object.
Server.URLEncode(String)
Chuyển xâu thành dạng mã hoá URL, để đảm bảo hyperlink trong ASP đó đợc
định dạng đúng đắn
.


103
Ví dụ:
<% Response.Write Server.URLEncode("") %>
Kết quả:
http%3A%2F%2Fwww%2Eissi%2Fnet
4.6.5 Đối tợng Application
Một ứng dụng bao gồm các file có thể truy nhập thông qua một th mục ảo
xác định và các th mục con của nó.
Đối tợng Application thể hiện toàn bộ một ứng dụng ASP. Chúng ta có thể
sử dụng ứng dụng này để chia sẻ thông tin cho tất cả các ngời dùng trong một ứng
dụng.
Đối tợng Application đợc bắt đầu khi có một yêu cầu đầu tiên một trang
web bất kỳ từ th mục ảo tại Web server và tồn tại cho đến khi Webserver ngừng
hoạt động.
a. Tập hợp:
Application.Contents(Key)
Chứa danh sách các mục vừa đợc khởi tạo v đa vo đối tợng
Application.
Ví dụ:
<% Application("name") = "Application Maker"
Application("publishdate") = "05/15/01"
Application("author") = "DevGuru"
Set Application("Obj1") = Server.CreateObject("ADODB.Connection")
For Each Item in Application.Contents
If IsObject( Application.Contents(Item)) Then
Response.Write Item & " is an object.<BR>"
Else
Response.Write Item & "=" & Application.Contents(Item) & "<BR>"
End If

Next
%>

Kết quả:
name=Application Maker
publishdate=05/15/01
author=DevGuru
OBJ1 is an object
Phơng thức của tập hợp Contents:
Application.Contents.Remove (Name|Integer)
Loại bỏ mục nào đó trong collection Application.Contents
Name chỉ ra tên mục sẽ xóa, nằm trong cặp dấu nháy (). Integer chỉ ra vị
trí mục trong collection sẽ đợc xóa. Giá trị này bắt đầu từ 1.
Ví dụ:

104
<%
Application("name") = "Application Maker"
Application("publishdate") = "05/15/01"
Application("author") = "DevGuru"
Set Application("Obj1") = Server.CreateObject("ADODB.Connection")
Application.Contents.Remove(1)
Application.Contents.Remove("publishdate")
For Each Item in Application.Contents
If IsObject(Application.Contents(Item)) Then
Response.Write Item & " is an object.<BR>"
Else
Response.Write Item & "=" &Application.Contents(Item) & "<BR>"
End If
Next

%>

KÕt qu¶:
author=DevGuru
Obj1 is an object.
• Application.Contents.RemoveAll
Lo¹i bá tÊt c¶ c¸c môc trong collection Application.Contents .
Thªm cÆp dÊu ngoÆc ()
<%Application.Contents.RemoveAll( )%>
 Application.StaticObjects(Key)
Chøa tÊt c¶ c¸c môc ®ã ®−îc t¹o trong øng dông b»ng thÎ <OBJECT.
Global.asa
<OBJECT RUNAT=Server SCOPE=Application
ID=MyInfo PROGID="MSWC.MyInfo">
</OBJECT>
<OBJECT RUNAT=Server SCOPE=Application
ID=MyConnection PROGID="ADODB.Connection">
</OBJECT>
<OBJECT RUNAT=Server SCOPE=Application
ID=MyADRot PROGID="MSWC.ADRotator">
</OBJECT>

File.asp
<%
For Each Item In Application.StaticObjects
Response.Write Item & "<BR>"
Next
%>

105

Kết quả:
MyInfo
MyConnection
MyADRot
b. Sự kiện:
ứng với hai hoạt động bắt đầu và kết thúc một đối tợng Application ta có hai
sự kiện trong đối tợng Application, đó là:
Application_OnStart (khởi tạo các thông tin phục vụ cho một ứng dụng khi
ứng dụng bắt đầu) và Application_OnEnd (đợc kích hoạt khi ứng dụng kết thúc)
Cú pháp của sự kiện Application_OnStart:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
' Nơi chèn đoạn Script cần thiết cho việc khởi tạo một ứng dụng
End Sub
Sub Application_OnEnd
' Nơi chèn đoạn Scipt cần thiết cho việc kết thúc một ứng dụng
End Sub
</SCRIPT>
Chú ý: Phải khai báo ngôn ngữ script đợc sử dụng trong các đoạn script sự
kiện trong dòng đầu tiên của file global.asa
- Các đối tợng của ASP trong phần này chỉ có SERVER và APPLICATION
Global.asa
<script Language="VBScript" RUNAT=Server>
Sub Application_OnEnd()
End Sub
Sub Application_OnStart()
Application("NumSession") = 0
Application("NumVisited") = 0
Session.Timeout = 10
End Sub

Sub Session_OnEnd()
Application("NumSession") = Application("NumSession") 1
End Sub
Sub Session_OnStart()
Application("NumSession") = Application("NumSession") + 1
Application("NumVisited") = Application("NumVisited") + 1
End Sub
</script>

File1.asp
Response.Write "You are " & Application("NumSession") & " of "
& Application("NumVisited") & " users."

×