Đối tượng Response trong web Course
Trang 1Bài 2 1
BÀI 6 ASP NÂNG CAO
Khoa CNTT - ĐHKHTN
Khoa CNTT - ĐHKHTN
Nội dung
1 Đối tượng Response
2 Đối tượng Request
3 Đối tượng Session và Application
4 File GLOBAL.ASA
5 COOKIES
6 Đối tượng SERVER
7 ASPError
8 ADO Nâng cao
Khoa CNTT - ĐHKHTN
1 Đối tượng Response (1)
nChuyển hướng trang web
qResponse.Redirect “URL”
qBrowser chuyển sang thực hiện trang “URL”, bỏ
qua các kết quả xuất ra
Trang 2Bài 6 4 Khoa CNTT - ĐHKHTN
1 Đối tượng Response (2)
<HTML>
<TITLE>Page Redirect</TITLE>
</HEAD>
<BODY>
<%
Site = Request.QueryString(“SiteName”)
Select case Site
case “Microsoft”
Response.Redirect ”http://www.microsoft.com”
case “Intel”
Response.Redirect ”http://www.intel.com”
case “Oracle”
Response.Redirect ”http://www.oracle.com”
End Select
%>
</BODY>
Khoa CNTT - ĐHKHTN
1 Đối tượng Response (3)
n Kiểm soát việc kết xuất dữ liệu ra trình duyệt
q Response.Buffer
n FALSE : không đặt buffer
n TRUE : đặt buffer lưu tạm dữ liệu xuất
q <%Response.Buffer=TRUE/FALSE%> : đặt ở đầu trang
ASP
q Response.Flush : xuất dữ liệu buffer về cho client
q Response.Clear : xóa dữ liệu vùng đệm
q Response.End : kết thúc xử lý
q Response.Expires : đặt thời gian (min) cache (-1:no cache)
q Response.ExpiresAbsolute = #date#
Khoa CNTT - ĐHKHTN
1 Đối tượng Response (4)
n Kiểm tra client có còn nối với máy chủ
q Response.IsClientConnected (TRUE/FALSE)
<% 'Check to see if the client is connected
If Not Response.IsClientConnected Then
'Get the sessionid to send to the shutdown function
Shutdownid = Session.SessionID
Shutdown(Shutdownid) 'Perform shutdown processing
End If
%>
n Response.CharSet = “UTF-8” : đặt bảng mã dữ liệu
trả về
Trang 3Bài 6 7 Khoa CNTT - ĐHKHTN
2 Đối tượng Request (1)
nRequest.Form, Request.QueryString
qTrường hợp control có nhiều giá trị
qRequest.Form(“FieldName”).Count : số giá trị
qRequest.Form(“FieldName”)(i) i=1 count : lấy
từng giá trị
qTương tự QueryString hoặc chỉ có Request
Khoa CNTT - ĐHKHTN
2 Đối tượng Request (1)
<%
For i = 1 to
Request.Form("FavoriteFlavor").count
%>
Request.Form(FavoriteFlavor) =
<%= Request.Form("FavoriteFlavor")(i)%>
<BR>
<% Next %>
Khoa CNTT - ĐHKHTN
2 Đối tượng Request (2)
nCách tạo HTTP QueryString
qDùng Form với method=GET
qGõ trực tiếp vào chuỗi URL
<A HREF=“example.asp?FirstName=Duy&LastName=Le Dinh">
string sample</A>
nRequest.ServerVariables(“Variable”) : Lấy
các thông tin môi trường tại server
Trang 4Bài 6 10 Khoa CNTT - ĐHKHTN
2 Đối tượng Request (3)
n AUTH_PASSWORD : Lấy thông tin password nhập từ
CLIENT (qua đặt quyền Authentication IIS)
n LOCAL_ADDR : Trả về địa chỉ Server chứa trang đang được
request (trường hợp hosting nhiều địa chỉ)
n REMOTE_ADDR : Địa chỉ IP của máy client gọi trang web
n REQUEST_METHOD: phương thức truyền Request
(GET,POST)
n …
Khoa CNTT - ĐHKHTN
3 Đối tượng Application, Session
nMục đích: lưu biến dữ liệu dùng chung cho
nhiều trang
qApplication : chia sẻ thông tin (biến dữ liệu) cho
nhiều người dùng cùng 1 ứng dụng
qSession: chia sẻ thông tin (biến dữ liệu) cho nhiều
trang cùng 1 phiên làm việc của người dùng
Khoa CNTT - ĐHKHTN
3 Sử dụng Application (1)
n Sử dụng biến Application
<%
Application("greeting") = "Welcome to My Web World!"
Application("num") = 25
Set Application("Obj1") = Server.CreateObject("MyComponent")
%>
<%
Set MyLocalObj1 = Application("Obj1")
MyLocalObj1.MyObjMethod
%>
Trang 5Bài 6 13 Khoa CNTT - ĐHKHTN
3 Sử dụng Application (2)
nSử dụng biến Application
<%
Application.Lock
Application("NumVisits") = Application("NumVisits") + 1
Application.Unlock
%>
This application page has been visited
<%= Application("NumVisits") %> times!
Khoa CNTT - ĐHKHTN
3 Sử dụng Session (1)
nMỗi phiên làm việc có thời gian xác định
qSession.Timeout = minutes
qSession.SessionID : ID của phiên làm việc
qSession.Contents : chứa danh sách các biến
collection trong Session
qSession.CodePage: quy định bảng mã xử lý của
cả session (=65001 : Unicode UTF-8)
n Session.Abandon : Hủy phiên làm việc (hủy cả biến
dữ liệu)
Khoa CNTT - ĐHKHTN
3 Sử dụng Session (2)
<%
Session("username") = "Janine"
Session("age") = 24
Set Session("Obj1") = Server.CreateObject("MyComponent.class1")
%>
<%
UserName = Session(“username”)
Set MyLocalObj1 = Session("Obj1")
MyLocalObj1.MyObjMethod
%>
Trang 6Bài 6 16 Khoa CNTT - ĐHKHTN
3 Sử dụng Session (3)
n Xử lý mảng trong biến
Session
<%
MyAr(0)=1
MyAr(1) = 10
Session("MyArray") = MyAr
%>
Không dùng được :
Session("MyArray")(0) hay
Session("MyArray")(1)
Lấy giá trị từ mảng
<%
MyArTemp = Session("MyArray") v0 = MyArTemp(0)
%>
Cập nhật giá trị cho mảng
<%
MyArTemp = Session("MyArray") MyArTemp(0) = 4
Session("MyArray") = MyArTemp
%>
Khoa CNTT - ĐHKHTN
4 File Global.asa (1)
nChứa các lệnh thao tác ngay khi các đối
tượng Application, Session được tạo
qApplication_OnStart
qSession_OnStart
nChứa các lệnh thao tác khi các đối tượng
Application, Session chuẩn bị hủy
qApplication_OnEnd
qSession_OnEnd
Khoa CNTT - ĐHKHTN
4 File Global.asa (2)
n File Global.asa đặt tại thư mục gốc của ứng dụng
n Cấu trúc tập tin global.asa
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
.
End Sub
Sub Application_OnEnd
.
End Sub
Sub Session_OnStart
.
End Sub
Sub Session_OnEnd
.
End Sub
</SCRIPT>
Trang 7Bài 6 19 Khoa CNTT - ĐHKHTN
5 COOKIES (1)
nLà thông tin của 1 ứng dụng được lưu trữ tại
máy client
nCác điều khiển xử lý thông tin (lưu, lấy) do
server thực hiện
nCác thuộc tính
qDomain : xác định tên miền mà cookies được gởi
đi
qExpires : ngày hết hạn (Nếu không đặt thì cookies
sẽ hết hạn khi hết session)
qPath : đường dẫn mà cookies được gởi đi
Khoa CNTT - ĐHKHTN
5 COOKIES (2)
nLệnh ghi cookies
q Response.Cookies(cookie)[(key)|.attribute] = value
qcookie: tên biến
qkey : tham số tùy chọn, có thể đặt nhiều giá trị cho
1 cookie
qattribute: thuộc tính (domain, path,….)
nLấy giá trị cookies
q value = Request.Cookies(cookie)[(key)|.attribute]
Khoa CNTT - ĐHKHTN
5 COOKIES (3)
-cookiesform.asp -<%
Response.Buffer=true
%>
<HTML>
<HEAD>
<TITLE>cookiesform.asp</TITLE>
<BODY bgcolor="#FFFFFF">
<%
ln=Request.Cookies("thatperson")("last
name")
fn=Request.Cookies("thatperson")("first
name")
st=Request.Cookies("thatperson")("stat
e")
%>
<FORM ACTION =
"cookiesformrespond.asp">
Form with Cookies<p>
Please enter your First Name<p>
<INPUT NAME="NameFirst" size ="40"
value=<%=fn%>>
<p>
Please enter your Last Name<p>
<INPUT NAME="NameLast" size ="40"
value=<%=ln%>>
<p>
Please enter your State abbreviation<p>
<INPUT NAME="State" MaxLength="2"
value=<%=st%>>
<INPUT TYPE=submit>
</FORM>
</
Trang 8Bài 6 22 Khoa CNTT - ĐHKHTN
Ví dụ Cookies
nTạo welcome cookie
Khoa CNTT - ĐHKHTN
6 Đối tượng SERVER
n Cung cấp các thuộc tính và phương thức hỗ trợ cho
các chức năng ở mức server.
q <%Server.ScriptTimeOut=100%> : Đặt thời gian thi hành
trang web (phút)
q <%Server.Execute("Welcome.asp")%> : gọi thi hành trang
khác (xem như là 1 phần của trang hiện hành)
q Server.HTMLEncode
q Server.URLEncode
q Server.MapPath
q Server.Transfer (“URL”) :
Khoa CNTT - ĐHKHTN
Phương thức diễn giải
n CreateObjecttạo một thực thể của một đối tượng
n ExecuteThực thi một trang ASP từ trong một trang khác
n GetLastError()Trả về đối tượng ASPError mô tả tình trạng lỗi
Phương thức này chỉ có thể dùng được trước khi file gởi nội
dung đến browser
n HTMLEncodeáp dụng mã hoá HTML thành một chuỗi xác định
n MapPathTạo đường dẫn vật lý
n TransferChuyển tất cả thông tin (biến application/session, item
trong tập hợp request ) đã được tạo trong file ASP đến file ASP
khác
n URLEncodeáp dụng mã hoá HTML thành một chuỗi xác định
Trang 9Bài 6 25 Khoa CNTT - ĐHKHTN
Ví dụ đối tượng Server
nLấy thời điểm cuối cùng chỉnh sửa file
nMở file text để đọc
nHomemade hit counter
Khoa CNTT - ĐHKHTN
Đối tượng FileSystemObject (1)
nCung cấp các thuộc tính và phương thức để
xử lí tập tin, thư mục, ổ đĩa,
nTạo đối tượng
set fso = Server.CreateObject(“Scripting.FileSystemObject”)
nCác thuộc tính
qDriveLetter, DriveName, TotalSize, FreeSpace,
Path, ShareName, VolumeName, GetDriver,…
Khoa CNTT - ĐHKHTN
Đối tượng FileSystemObject (1)
nCác phương thức thao tác thư mục
qCreateFolder, DeleteFolder, CopyFolder,
MoveFolder, GetFolder,…
nCác phương thức thao tác tập tin
qCreateTextFile, OpenTextFile, Write, WriteLine,
WriteBlankLines,
qRead, ReadLine, ReadAll, MoveFile, CopyFile,
DeleteFile,
Trang 10Bài 6 28 Khoa CNTT - ĐHKHTN
Ví dụ đối tượng FileSystemObject
n Does a specified file exist?
Kiểm tra file đã tồn tại chưa?.Sử dụng fs.FileExists("tên file")
n Does a specified folder exist?
Kiểm tra sự tông tại một file, sử dụng phương thức FolderExists
n Does a specified drive exist?
Sử dụng fs.DriveExists (với "Set fs=Server.CreateObject")
n Get the name of a specified drive
Sử dụng fs.GetDriveName
n Get the name of the parent folder of a specified path
Sử dụng fs.GetParentFolderName để tên lấy thư mục cha
n Get the file extension
sử dụng fs.GetExtensionName
n Get the base name of a file or folder
sử dụng fs.GetBaseName
Khoa CNTT - ĐHKHTN
7 Đối tượng ASPError
n Lưu các thông tin lỗi trong quá trình thực thi
n Tạo đối tượng ASPError
q Set objAspErr = Server.GetLastError()
n Các thuộc tính mô tả thông tin lỗi
q ASPCode
q Source
q Category
q File
q Line
q Column
q Description
q ASPDescription
Khoa CNTT - ĐHKHTN
1 Đối tượng ASPError (1)
n Lưu các thông tin lỗi trong quá trình thực thi
n Tạo đối tượng ASPError
q Set objAspErr = Server.GetLastError()
n Các thuộc tính mô tả thông tin lỗi
q ASPCode
q Source
q Category
q File
q Line
q Column
q Description
q ASPDescription
Trang 11Bài 6 31 Khoa CNTT - ĐHKHTN
1 Đối tượng ASPError (2)
<%
Sub DisplayError()
Set objASPError = Server.GetLastError()
blnErrorWritten = False
If (objASPError.Line > 0) AND (objASPError.Column > 0) Then
blnErrorWritten = True
Response.Write ", line " & objASPError.Line
Response.Write ", column " & objASPError.Column
Response.Write Server.HTMLEncode(objASPError.Source)
Response.Write "<b>" & objASPError.File
End If
End Sub
%>
Khoa CNTT - ĐHKHTN
2 ADO Nâng cao (1)
n Xử lý lỗi liên quan đến CSDL
q Sử dụng đối tượng Connection.Errors
<% ‘ Cnn đối tượng Connection kết nối CSDL
IF Cnn.errors.count> 0 then
response.write "Database Errors Occured" & "<P>"
for each error in Cnn.errors
response.write "Error #" & error.number & "<P>"
response.write "Error desc -> " & error.description & "<P>"
next
else
response.write "No Database Errors Occured!" & "<P>"
exit sub
end if
%>
=> Thực hiện kiểm tra sau mỗi lần thao tác đến CSDL
Khoa CNTT - ĐHKHTN
Xem thêm các đối tượng
nASP TextStream
nASP Drive
nASP File
nASP Folder
nASP Dictionary
Trang 12Bài 6 34 Khoa CNTT - ĐHKHTN
Xem thêm các thành phần
nASP AdRotator
nASP BrowserCap
nASP Content Linking
nASP Content Rotator