YOMEDIA
Bài giảng lập trình Web - Ts.Vũ Đức Lung - Chương 5
Chia sẻ: Impossible_1 Impossible_1
| Ngày:
| Loại File: PDF
| Số trang:45
85
lượt xem
11
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
ASP.NET dùng Cookies để lưu trữ Session ID và thông tin
về user. Nếu browser của user không yểm trợ cookies hoặc trong nhiều trường hợp, user không muốn (hay chấp nhận) yểm trợ cookies? ASP.NET dùng Cookie Munging = ASP.NET phải mã hoá và giải mã (encode and decode) Session ID cùng với các thông tin liên hệ và đặt vào các trang Web trước khi gởi đi.
AMBIENT/
Chủ đề:
Nội dung Text: Bài giảng lập trình Web - Ts.Vũ Đức Lung - Chương 5
- Chương 5: Xây dựng và quản lý
Website
ASP.NET OBJECTS thông dụng
g ụ g
-Response Object
-Request Object
-Page Obj
P Object
-Session Object
-HttpCookie Object
p j
-HttpApplication
Tập tin quản lý và cấu hình ứng dụng
T các mẫu t
Tạo á ẫ trang có khả năng dù l i - P l t
ó ă dùng lại Pagelets
ASP.NET 1
- ASP.NET OBJECTS thông dụng
-Response Object
-Request Object
-Page O j
Object
-Session Object
-HttpCookie Object
-HttpApplication
ASP.NET 2
- Response Object
Cho phép Server đáp ứng, trả lời hay thông tin
với Client
Phương pháp (Method) Write
g ( )
Response.Write("Using Write method of Response object")
Response là một Instance của HttpResponse
Buffering Pages
ASP.NET 3
- Response Object
Ðể vận dụng hay xử dụng buffer, ta dùng
phương pháp (method):
Clear
Flush
End
Method Redirect
Response.Redirect("http://www.ASP.net")
ASP.NET 4
- Dùng Response Object cho phép download File
Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles
LinkButton1.Click
Dim DuongDan As String
DuongDan = Server.MapPath("../download/tailieu.rar")
g p ( )
Response.AddHeader("Content-Disposition", "attachment;
filename=tailieu.rar")
Response.WriteFile(DuongDan)
Response.End()
End Sub
ASP.NET 5
- Request Object
Dùng để thông tin giữa Server và Client browser
Request.Querystring
R tQ ti
VD: http://www.asp.net?username=vdlung&password=cheery
‘Để tìm lại toàn bộ thông tin "username=vdlung&password=cheery"
Để username=vdlung&password=cheery
Request.Querystring
‘Để tìm lại giá trị (value) “vdlung"
Request.Querystring("username")
Dưới dạng form: Lưu ý:
'Tìm lại toàn bộ giá trị trong form Thường thì Querystring chỉ dùng
Request.Form
R tF tối đa 255 characters
ố
'Tìm lại giá trị value “vdlung"
Request.Form(username)
Dùng Request để thu thập thông tin về ServerVariables và Cookies
ASP.NET 6
- Page Object
g j
Page object có xuất sứ từ Page class
trong .NET Framework
Load: khởi động trang web
IsPostBack: kiểm
I P B k kiể tra l d lầ đầ ?
load lần đầu?
Databind: Kết nối với CSDL
ASP.NET 7
- Đối tượng Session, Application
- Giúp các
p Web Server
trang aspx
Web Site
liên kết và
trao đổi dữ
Application
liệu cho nhau
Session Session Session
ASP.NET 8
- Đối tượng Application
- Quản lý tất cả các thông tin của một ứng dụng web
- Thô tin trong Application có thể được xử lý trong bất kỳ trang aspx
Thông ti t A li ti ó đ ử t t
nào trong suốt chu kỳ sống của ứng dụng
Tạo biến Application:
Application(“tên biến”) =
ế
Lấy giá trị từ biến:
= Application(“tên biến”)
pp ( )
VD:
Dùng đối tượng này để tính số lượng người truy cập vào website
Application( SoLanTruyCap )
Application(“SoLanTruyCap”) = 1000
Application(“SoNguoiOnline”) = 5
ASP.NET 9
- Khai báo và khởi tạo cho biến Application
Sub Application_Start(object sender, EventArgs e)
‘ Số người sử d
ời ử dụng đã truy cập website
ậ bi
Application("SiteHitCounter“) = 1000
‘Số người sử d
ời ử dụng đ
đang truy cập website
ậ bi
Application("CurrentUsers“) = 0
End Sub
E dS b
ASP.NET 10
- Khai báo biến cố Session_Start, Session_End
Sub Session_Start(object sender, EventArgs e)
Application("SiteHitCounter“) = Application("SiteHitCounter“) + 1
Application("CurrentUsers“) = Application("CurrentUsers“) + 1
End Sub
Sub Session_End(object sender, EventArgs e)
Application("CurrentUsers“) = Application("CurrentUsers“) - 1
// sessionstate mode = “InProc”
End Sub
E dS b
ASP.NET 11
- Khai báo lấy giá trị của đối tượng Application
protected Sub Page_Load(object sender, EventArgs e)
Literal1.Text = "Hit Counter: " &
Application("SiteHitCounter”) .ToString()
Literal2.Text = "Current users: " &
Application("CurrentUsers“).ToString()
End Sub
p p
ASP.NET 12
- Các thuộc tính quan trọng của Application
AllKeys
Sub ViewAllKeys()
Dim app As String
Literal1.Text =
Li l1 T
"View all keys in Application object by AllKeys property"
for each app in Application.AllKeys
Literal1.Text &=app & ""
Next
End Sub
View all keys in Application object by AllKeys property
SiteHitCounter
CurrentUsers
C tU
ASP.NET 13
- Các thuộc tính quan trọng của Application
Keys
Sub ViewKeys()
S b Vi K ()
Dim app As String
Literal2.Text = "View all keys in Application object“ & _
" by Keys property"
for each ( app in Application.Keys)
Literal2.Text &= app & ""
Next
End Sub
ASP.NET 14
- Các thuộc tính quan trọng của Application
Count
Application.Count
Sub GetAllValues()
Dim i As Integer
Literal2.Text = “Lấy giá trị của key trong ứng dụng“ & _
" nhờ thuộc tính Count"
for i = 0 To Application.Count-1
Literal2.Text &= Application.GetKeys(i) & “ = ” & _
Application(i).ToString() & ""
Next
End Sub
Lấy giá trị của key trong ứng dụng nhờ thuộc tính Count
SiteHitCounter = 1002
CurrentUsers = 5
ASP.NET 15
- Các phương thức quan trọng của
Application
Get(“SitHitCounter”)
( )
Lock
Unlock
Remove(“CurrentUsers”)
RemoveAt(1)
RemoveAll()
ASP.NET 16
- Đối tượng session
- Lưu trữ thông tin của người dùng trong một phiên làm việc
- Thông tin trong Session có thể được xử lý trong bất kỳ trang aspx nào
trong suốt phiên làm việc hiện hành
Tạo biến Session:
Session(“tên biến”) =
Lấy giá trị từ biến:
ấ ế
= Session(“tên biến”)
VD:
Khi chưa đă nhập vào hệ thố
h đăng hậ à thống:
Session(“UserID”) = 0
Session(“UserName”) = “”
Sau khi đang nhập thành công, thông tin trong session sẽ được cập nhật
công
lại như:
Session(“UserID”) = 1
Session(“UserName”) = “vdlung”
( ) g
ASP.NET 17
- Session Object
j
sub Submit_Click(obj as object,e as EventArgs)
if tbName.Value ""
Session("Name ")=tbName.Value
Response.Write( Hi
Response.Write("Hi " & Session( Name ")&"!")
Session("Name )& ! )
else
Response.Write("You forgot to enter a name.")
end if
d
end sub
ASP.NET 18
- Session Object
- Thường dùng session để nắm giữ các thông tin như UserName, email
hoặc UserID
- Dựa vào các thông tin này mà phân quyền truy cập cho user
-Ví dụ:
ASP.NET 19
- Session Object
Private Sub btnLogin Click( ) Handles btnLogin Click
btnLogin_Click(…) btnLogin.Click
If (tbUserID.Text "") Then
Session("UserID") = tbUserID.Text
Response.Redirect("TrangCanDen.aspx")
End If
End Sub ASP.NET 20
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
ERROR:connection to 10.20.1.98:9315 failed (errno=111, msg=Connection refused)
ERROR:connection to 10.20.1.98:9315 failed (errno=111, msg=Connection refused)
Đang xử lý...