1 / 29

ASP 企业级应用开发

ASP.net 企业级应用开发. 信息工程系:罗明刚. 第六章. Application 、 Session 和 Cookie 对象. 回顾. 传递Form表单中的控件值用Request.Form 获取值,地址栏中传递的参数用 Request.QueryString获取 通常用HttpResponse 类的属性Buffer、Cache、Cookie 和 Expires来设置站点的一些特性 HttpResponse 类常用方法是 Write()、End() 和 Redirect()

lainey
Télécharger la présentation

ASP 企业级应用开发

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.net 企业级应用开发 信息工程系:罗明刚

  2. 第六章 Application、Session和Cookie对象

  3. 回顾 • 传递Form表单中的控件值用Request.Form 获取值,地址栏中传递的参数用 Request.QueryString获取 • 通常用HttpResponse 类的属性Buffer、Cache、Cookie 和 Expires来设置站点的一些特性 • HttpResponse 类常用方法是 Write()、End() 和 Redirect() • HttpServerUtility 类的Execute()和Transfer()的区别关键在于执行后控制权是否返回原先页面、URLEncode()和HTMLEncode()主要用于对HTML 标签和URL进行编码,对站点或程序的安全具有重要意思, MapPath()获取物理路径时需要注意是绝对路径还是相对路径

  4. 目标 • 运用Global.asax 文件 • 使用 Application 对象 • 创建并读取 Cookie • 使用 Session 对象

  5. ASP.NET中数值传递模型介绍

  6. Global.asax 2-1 “Global.asax” • 包含所有应用程序的配置设置 • 存储所有事件的事件处理程序 • 存储在应用程序的根目录下 • 它的位置定义应用程序的限界

  7. Global.asax 2-2 文件 Global.asax httpApplication类 实例 存储于 配置设置 事件处 理程序 事件

  8. TestingGlobal.aspx示例 4-1 Global.asax 文件 <script language=“C#” runat=“server"> protected void Application_Start(Object sender, EventArgs e) { } protected void Session_Start(Object sender, EventArgs e) { Response.Write( “会话已开始 <br>"); } protected void Application_BeginRequest(Object sender, EventArgs e) { Response.Write(“<h1>应用程序开始</h1>"); Response.Write (“应用程序请求开始<br>"); } 打开代码隐藏类文件 Global.asax.cs。 将代码分别添加到 Global.asax.cs中的各个事件中

  9. TestingGlobal.aspx示例 4-2 protected void Application_EndRequest(Object sender, EventArgs e) { Response.Write (“应用程序请求结束<br>"); } protected void Session_End(Object sender, EventArgs e) { Response.Write(“会话已结束"); } protected void Application_End (Object sender, EventArgs e) { } </script>

  10. TestingGlobal.aspx示例 4-3 private void Page_Load(object sender, System.EventArgs e) { Response.Write(“页面加载事件<br>"); } 将默认 ASP.NET 页面‘WebForm1.aspx’重命名为‘TestingGlobal.aspx’, 并添加给定的代码片段

  11. TestingGlobal.aspx示例 4-4 输出结果 会话尚未结束 刷新

  12. Application 对象 类 HttpApplicationState Application对象存储并维护 应用程序级的数据 Application 状态 Application状态 方法 集合 由它们表示

  13. Application 变量 变量用于在应用程序执行时存储数据 string myname = “张三"; Response.Write (“欢迎 " + myname); 变量 页面级 对象级 应用程序级 会话级

  14. 对象级变量 3-1 Global.asax文件 增加在线人数 减少在线人数 清零 protected void Application_Start(Object sender, EventArgs e) { Application.Lock (); Application["UserNum"] =0; Application.UnLock(); } protected void Session_Start(Object sender, EventArgs e) { Response.Write("调用Session_Start:会话已开始 <br>"); Application.Lock(); Application["UserNum"]=int.Parse(Application["UserNum"]. ToString())+1; Application.UnLock(); } protected void Session_End(Object sender, EventArgs e) { Response.Write("调用Session_End:会话已结束"); Application.Lock(); Application["UserNum"]=int.Parse(Application ["UserNum"].ToString())-1; Application.UnLock(); }

  15. 对象级变量 3-2 AppVariable.aspx private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 Response.Write("你是第"+ Application["UserNum"].ToString() + "位访客<BR>"); }

  16. 对象级变量 3-3

  17. Lock 和 Unlock方法 Lock() UnLock() Lock()用于防止用户更改 Application对象的属性 Unlock()方法用于释放对应用程序变量的锁定 Lock()可用作 Application.Lock() UnLock() 可用作 Application.UnLock()

  18. 向应用程序添加、更新和移除项 Application对象还提供 Get() 和 Set()方法, 可用于按名称或索引获取对象,并相应地 更新对象的值 HttpApplicationState 方法

  19. Cookies 在客户端系统中维护客户的个人信息 Cookie … … … 会话 Cookie 持久性 Cookie

  20. 创建和读取会话 Cookie 创建 Cookie 新建 Cookie 读取 Cookie HttpCookie objHttpCookie = new HttpCookie(“UserName”, “张三"); Response.Cookies.Add(objHttpCookie); Response.Write(Request.Cookies(“UserName").Value); 将新 Cookie添加到 Response对象的 Cookie集合中 Value 属性将 Cookie 的值作为字符串返回

  21. 创建和读取持久性 Cookie 创建 Cookie 读取 Cookie 新建 Cookie HttpCookie objHttpCookie = new HttpCookie(“UserName”,“张三"); objHttpCookie.Expires = DateTime.Now.AddMinutes(2); Response.Cookies.Add(objHttpCookie); Response.Write(Request.Cookies(“UserName").Value); 用 Cookie的 Expires属性将 Cookie 的过期期限设置为两分钟

  22. Session 对象 优点 • Session对象用于存储用户的信息 • 当新用户请求应用程序的网页时,Server对象会检查用户是否有 SessionID • Session对象包含特定于某个用户的信息 • 在用户会话期间可以记录和监视特定于用户的信息 • 当会话过期或终止时,服务器就会清除 Session 对象

  23. Session变量 • Session 变量与 application变量不同 • Session变量仅提供给会话中的特定用户 • Session变量可用于存储在整个用户会话过程中都可以访问的值

  24. private void btnLogin_Click(object sender, System.EventArgs e) { if(this.txtName.Text.Trim()!="" || this.txtPwd.Text.Trim()!="") { if(this.txtName.Text=="张三" && this.txtPwd.Text== "123456") { Session["UserName"]=this.txtName.Text.Trim(); Response.Redirect("SessionVariableWelcome.aspx?pwd=" +this.txtPwd.Text ); } else { Session["UserName"]=""; RegisterStartupScript("Check","<Script language=‘JavaScript’>alert(‘用户名或密码不对,请检查!'); </Script>"); } } else { Session["UserName"]=""; } } 在SessionVariableNew.aspx.CS添加如下代码

  25. 使用示例 3-2 private void Page_Load(object sender, System.EventArgs e) { if(Session["UserName"].ToString().Trim()!="") { Response.Write ("<Script language=JavaScript>alert('欢迎 "+Session["UserName"] + "光临,你的密码是: "+Request["pwd"].ToString()+"')</Script>"); } else { Response.Redirect("SessionVariableNew.aspx"); } } 在SessionVariable Welcome.aspx页面 添加如下代码

  26. 使用示例 3-3 错误登录时 正确登录时

  27. Session事件 • Session_Start 当新用户访问一个应用程序时会激活该事件 • Session_End 用户退出应用程序时,就会触发 Session_End 事件

  28. Session事件 Session 属性 Session_Start Session_End

  29. 总结 • Global.asax 文件包含常用的Application_Start、Application_End、Session_Start、Session_End等事件 • Application 对象是存储于服务器的全局变量 • Cookie 存储信息于客户端 • Session 对象用于在服务器端存储用户的信息,在用户结束会话时被清除 • 新用户访问应用程序时会激活 Session_Start 事件,而用户退出应用程序时会触发 Session_End 事件

More Related