课程3上课日志1
课程3上课日志1一、一般处理程序基本知识(Get和Post的请求方式、Request和Response对象的使用)1、一般处理程序(1)是web项目下才有的文件,扩展名为ashx,实现了IHttpHandler接口,因为不必继承自Page类,所以没有那么多事件需要处理,不必消耗太多资源,所以性能方面要比aspx高。但是每个页面处理逻辑都需要写一个对象的处理程序,项目大的话,处理程序就会显得太多了。(2)创建方法:(3)几个说明:u ProcessRequest()被访问时被调用的方法,可以类似于From_Load理解,也即是一调用一般处理程序,就会执行这个方法,所以我们一般是修改这个方法u HttpContext context:请求上下文对象,包含了请求处理要使用的信息和对象u public bool IsReusable/是否重用,一般不去改 get return false;/这个属性是用来标识当前一般处理程序的实例是否进行重用,确实需要重用设置为true 2、Get和Post请求方式Get和Post是向服务器发送请求的两种方式,其中,Get请求是将需要提交给服务器的数据放在URL地址中,而Post请求则是将请求数据封装到请求报文中进行发送。请求报文由请求行、请求头部、空行和请求数据4部分组成。Gethttp:/127.0.0.1/login.ashx?txtname=xzx&txtpwd=123Get传送的数据量较小,不能大于2KB。Post传送的数据量较大,一般被默认为不受限制。Get安全性非常低,但是执行效率却比Post方法好,Post安全性较高。建议:Get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式;在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式3、Request对象接收数据对象Request对象的作用是获取从客户端向服务器端发出的请求信息,根据请求方式的不同,可以通过三种方式来接收客户端的值。 string name = context.Request.QueryString"txtname " /get请求string name = context.Request.Form"txtpwd " /post请求string name = context.Request"txtpwd " /get和post请求4、Response对象输出数据对象Response对象用于将服务器响应的数据发送到客户端,此对象中包含了有关该响应的信息,并且通过Response对象的方法可以执行一些特定操作,具体示例代码如下所示:context.Response.Write("登录成功");context.Response.Redirect("http:/www.itcast.cn");二、为用户登录案例(实例3-1)创建登录界面与一般处理程序1、登录界面<form method="post" action="login.ashx"> <table> <tr> <td><label for="username">用户名:</label></td> <td colspan="2"><input type="text" name="name" value="" id="username"/></td> </tr> <tr> <td><label for="pwd">密 码:</label></td> <td colspan="2"><input type="password" name="pwd" id="pwd"/></td> </tr> <tr> <td></td> <td><input type="submit" value="登录" /></td> <td><input type="button" value="取消" /></td> </tr> </table> </form>解释:<form method="post" action="login.ashx"> 提问:label的for属性作用?2、创建一般处理程序默认生成很多代码,无需修改,找到ProcessRequest()方法,修改为以下内容。 public void ProcessRequest(HttpContext context) context.Response.ContentType = "text/plain"/告诉浏览器以普通文本显示 string name=context.Request.Form"name"/与前页面的name属性一致,不是id值 string pwd=context.Request.Form"pwd" if (name = "itcast" && pwd = "123456") context.Response.Write("登录成功"); else context.Response.Write("登录失败"); ContentType属性表示响应内容的类型,不同的ContentType值会影响浏览器看到的效果,默认为Text/html(以网页形式展示);text/plain(以普通文本形式显示);application/word(用word应用程序打开)三、改进实例3-1的一般处理程序(登录失败后,显示登录界面,但第1次启动时不显示“登录失败”)1、使得登录失败后,同时显示登录界面public void ProcessRequest(HttpContext context) context.Response.ContentType = "text/html"/告诉浏览器以网页显示 string path = context.Request.MapPath("Login.html"); string html = System.IO.File.ReadAllText(path); string name=context.Request.Form"name"/与前页面的name属性一致,不是id值 string pwd=context.Request.Form"pwd" if (name = "itcast" && pwd = "123456") context.Response.Write("登录成功"); else context.Response.Write("登录失败"); context.Response.Write(html); 注意:这里把Login.html当做模板来使用(以后一般都把html文件当做模板来使用)。启动时先启动Login.html页面,然后修改为Login.ashx2、第1次启动Login.ashx时不显示“登录失败”(1)修改Login.html<form method="post" action="login.ashx"> <input type="hidden" name="_viewstate" value="hidden" /> <table> <tr> <td><label for="username">用户名:</label></td> <td colspan="2"><input type="text" name="name" value="" id="username"/></td> </tr> <tr> <td><label for="pwd">密 码:</label></td> <td colspan="2"><input type="password" name="pwd" id="pwd"/></td> </tr> <tr> <td></td> <td><input type="submit" value="登录" /></td> <td><input type="button" value="取消" /></td> </tr> </table> </form>(2)修改Login.ashxpublic void ProcessRequest(HttpContext context) context.Response.ContentType = "text/html"/告诉浏览器以网页显示 string path = context.Request.MapPath("Login.html"); string html = System.IO.File.ReadAllText(path); string _vs=context.Request.Form"_viewstate"/如果是提交了表单,则_viewstate会获得值 bool ispostback = !string