Bài giảng lập trình Web - Ts.Vũ Đức Lung - Chương 5

45 32 0
Bài giảng lập trình Web - Ts.Vũ Đức Lung - Chương 5

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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.

Chương 5: Xây dựng quản lý Website ‰ ASP.NET OBJECTS thông g dụng ụ g -Response Object -Request Object -Page P Obj Object -Session Object -HttpCookie p Object j -HttpApplication ‰Tập tin quản lý cấu hình ứng dụng ‰T Tạo mẫu ẫ ttrang có ó khả ă dù dùng llạii - Pagelets P l t ASP.NET ASP.NET OBJECTS thông dụng -Response Object -Request Object -Page Object O j -Session Object -HttpCookie Object -HttpApplication ASP.NET Response Object Cho phép Server đáp ứng, trả lời hay thông tin với Client Phương g pháp (Method) ( ) Write Response.Write("Using Write method of Response object") Response Instance HttpResponse Buffering Pages ASP.NET 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 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 g = Server.MapPath(" /download/tailieu.rar") p ( ) Response.AddHeader("Content-Disposition", "attachment; filename=tailieu.rar") Response.WriteFile(DuongDan) Response.End() End Sub ASP.NET Request Object Dùng để thơng tin Server Client browser ¾R ¾Request.Querystring tQ ti VD: http://www.asp.net?username=vdlung&password=cheery ‘Để Để tìm lại tồn 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 ý: Thường Querystring dùng tối ố đa 255 characters 'Tìm lại tồn giá trị form R Request.Form tF 'Tìm lại giá trị value “vdlung" Request.Form(username) Dùng Request để thu thập thông tin ServerVariables Cookies ASP.NET Page g Object j Page object có xuất sứ từ Page class NET Framework Load: khởi động trang web I P B k kiểm IsPostBack: kiể tra load l d lầ lần đầ đầu? ? Databind: Kết nối với CSDL ASP.NET Đối tượng Session, Application - Giúp p trang aspx liên kết trao đổi liệu cho Web Server Web Site Application Session ASP.NET Session Session Đối tượng Application - Quản lý tất thông tin ứng dụng web - Thông Thô tin ti t Application A li ti cóó thể đ xử lý t trang t aspx suốt chu kỳ sống ứng dụng ¾ Tạo biến Application: Application(“tên biến”) ế = ¾Lấy giá trị từ biến: = Application(“tên pp ( biến”)) VD: Dùng đối tượng để tính số lượng người truy cập vào website Application(“SoLanTruyCap”) Application( SoLanTruyCap ) = 1000 Application(“SoNguoiOnline”) = ASP.NET Khai báo khởi tạo cho biến Application Sub Application_Start(object sender, EventArgs e) ‘ Số người ời sử dụng d truy cập ậ website bi Application("SiteHitCounter“) = 1000 ‘Số người ời sử ddụng đ truy cập ậ website bi Application("CurrentUsers“) = E dS End Sub b ASP.NET 10 Ðáo hạn (Expires) Cookies ‰ Kiểu ngày đáo hạn Response.Cookies("MyASPNETPage").Expires = DateTime.FromString("30/02/2003") S ( 30/02/2003 ) 'hoặc ‰ Kiểu Kiể kh khoảng ả thời gian i đáo đá h hạn Response.Cookies("MyASPNETPage").Expires = DateTime.Now.AddMonths(1) ASP.NET 31 Các thuộc tính quan trọng HttpCookie ¾Domain ¾Path ¾HasKeys ¾Secure - Domain dùng để giới hạn việc sử dụng cookies domain ta định, tỷ www.myserver.com - Path dùng tương tự Domain, Domain giới hạn việc sử dụng cookies path định Server ta - HasKeys báo cho ta biết Cookie dùng kiểu 'đa dạng, đơn giá trị' với cặp key/value - Secure báo cho ASP.NET biết nên chuyển cookie cách an tồn hay khơng thường xảy HTTPS protocol protocol Giá trị mặc định (default) Secure False ASP.NET 32 Ví dụ Cookie sub Page_Load(obj as object, e as eventArgs) g dim strVariable as string 'set up some cookie variables Response.Cookies("MyASPNETPage")("Username") = "Nang Vu" p ( y g )( ) = "TakeMeHome" Response.Cookies("MyASPNETPage")("Password") Response.Cookies("MyASPNETPage")("Preference") = "800x640" Response.Cookies("MyASPNETPage")("UserAgent") = Request.ServerVariables("HTTP_USER_AGENT") for each strVariable in Response.Cookies("MyASPNETPage").Values lblCookies.Text &= "" & strVariable & ": " & _ Request.Cookies("MyASPNETPage")(strVariable) & "" next end sub ASP.NET 33 Ví dụ Cookie ASP.NET 34 Tập tin quản lý cấu hình ứng dụng a) Tập tin Global.asax - Khai báo biến cố => nắm bắt hành động người sử dụng Khai báo khởi tạo giá trị cho biến Application Application, Session Viết xử lý cho kiện hai đối tượng Application Session Tự động sinh biên dịch ứng dụng ASP.NET 35 Cấu trúc tập tin Global.asax Public Class Global Inherits System.Web.HttpApplication Sub S b Application_Start(ByVal A li i S (B V l sender d A As Obj Object, B ByVal V leA As EventArgs) ' Fires when the application is started End Sub Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the session is started End Sub Sub S b Application_BeginRequest(ByVal A li ti B i R t(B V l sender d A As Obj Object, t ByVal e As EventArgs) ' Fires at the beginning of each request End Sub ASP.NET 36 Tập tin quản lý cấu hình ứng dụng (tt) Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs) ' Fires upon attempting to authenticate the use End Sub Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Fires when an error occurs End Sub Sub S b Session_End(ByVal S i E d(B V l sender d A As Obj Object, B ByVal V leA As E EventArgs) A ) ' Fires when the session ends End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the application ends End Sub End Class ASP.NET 37 Khai báo khởi tạo cho biến Application void Application_Start(object sender, EventArgs e) { Aplication.Lock() ‘ Số người sử dụng truy cập website Application("SiteHitCounter“) = 1000 ‘Số người sử dụng truy cập website A li i ("C Application("CurrentUsers“) U “) = Aplication.UnLock() } ASP.NET 38 Khai báo biến cố Session_Start, Session_End void Session_Start(object sender, EventArgs e) { Application("SiteHitCounter“) = Application("SiteHitCounter“) + Application("CurrentUsers“) = Application("CurrentUsers“) + } void Session_End(object sender, EventArgs e) { Application("CurrentUsers“) = Application("CurrentUsers“) - // sessionstate mode = “InProc” } ASP.NET 39 Tập tin quản lý cấu hình ứng dụng (tt) b) Tập tin web.config Lưu trữ thơng tin cấu ấ hình ứng dụng Tạo tự động Viết theo định ị dạng g XML

Ngày đăng: 09/05/2021, 14:54

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan