4 ASP
4.1 概述
4.1.1 概述
Active Server Pages(ASP),是微软公司于1997年前后推出的一种Web服务器开发技术,采用脚本语言编程,以ISAPI进行解释执行。ASP本身是一种开发技术,不是编程语言。它主要采用两种脚本语言:VBScript和JScript。
由于是微软公司的产品,该技术只能应用在该公司自己的操作系统和Web服务器上(有第三方产品支持ASP在非微软平台和非微软Web服务器上运行,但均非官方版本)。
最适合使用ASP技术的软件平台,主要是微软Windows系列的服务器版本和Web服务器IIS。具体来说有如下版本:
Windows Server Family: ? Windows NT Server 4.0 ? Windows 2000 Server / Advanced Server ? Windows Server 2003 ? Windows Server 2008
而Web服务器是Internet Information Server(IIS),随上述操作系统免费提供。
4.1.2 安装配置
IIS的安装可通过Windows“添加/删除组件”进行,与ASP有关的服务是“万维网服务”。安装后可通过启动服务或管理工具启动、停止。 以下简述IIS服务器的相关配置。
图 4-1 IIS配置:网站
20
图 4-2 IIS配置:主目录
图 4-3 IIS配置:目录安全性
在“网站”属性页(图 4-1)中,可指定IP地址(多网卡服务器)、连接超时等设置。
21
在“主目录”页面(图 4-2)中,主要设定网站的根目录,而且需要设置权限,至少应设置“读取”。 在“目录安全性”页面(图 4-3)中,需设定访问用户。一般允许匿名访问的网站,应选中“匿名访问”,并使用Windows内置的用户名。
4.2 HTTP
4.2.1 概述
超文本传输协议,HyperText Transfer Protocol(HTTP),是万维网的相关协议,主要由World Wide Web Consortium(W3C)制订,当前协议版本是1.1,定义在RFC 2616中。 HTTP由客户端“请求”(Request)和服务器端“响应”(Response)的一来一往,完成网页的传输。为保证效率,Request和Response都由多行构成,并包括“头”(Header)和“身”(Body),可以一次携带多个参数。
4.2.2 Request / Response
当客户端浏览器软件向Web服务器索取某个网页文件时,该浏览器会构造一个Request发到服务器,以下是一个Request示例:
1. GET http://localhost/MyWeb/index.asp HTTP/1.0
2. Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash,
application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* 3. Accept-Language: zh-cn
4. Cookie: ASPSESSIONIDSSRABDA ... ...
5. User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) 6. Host: localhost
7. Proxy-Connection: Keep-Alive
清单 4-1 HTTP Request
以上就是一个Request的“header”部分(无行号),第一行“GET”是一个命令字(HTTP 1.1共包括OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE、CONNECT等命令),表示“索取”后面的页面文件。以下各行是一些参数,表示了浏览器的一些特征。 服务器接收到上面的Request后,会给出一个Response,如下: 1. HTTP/1.1 200 OK
2. Server: Microsoft-IIS/5.1
3. Date: Mon, 24 Oct 2005 06:47:32 GMT 4. Connection: Keep-Alive 5. Content-Length: 596 6. Content-Type: text/html 7. Cache-control: private 8.
9.
10. 10.
11.
28.
清单 4-2 用户登录页面
清单 4-2给出用户登录页面,该页面含有两个表单,第一个供用户登录,第二个可以列出当前用户,并强制退出用户。两个表单由同一个程序处理,为分辨是哪个表单提交的数据,用了一个“hidden”类型的控件“act”,不同的表单,“act”的数值不同。 1. <%
2. Option Explicit
3. Response.Expires = 0 4.
5. Dim act
6. Dim user, pass, logtype 7. Dim tmp1, tmp2, n, str 8.
9. act = Trim(Request.Form(\10.
11. If act = \12. user = Trim(Request(\13. pass = Trim(Request(\14. logtype = Trim(Request(\15. 16. If user = \17. 18. tmp1 = \19. tmp2 = \20. n = InStr(tmp1, tmp2) 21. 22. If logtype = \23. If n > 0 then 24. Response.Write \该用户已登录!\25. Else 26. Application.Lock 27. If Application(\28. Application(\29. Else 30. Application(\31. End If 32. Application.Unlock 33. End If 34. Else 35. RemoveUser user 36. End If 37. End If 38.
39. If act = \40. user = Trim(Request(\41. If user = \42. 43. RemoveUser user 44. End If 45.
46. Sub RemoveUser(un) 47. tmp1 = \48. tmp2 = \49. n = InStr(tmp1, tmp2) 50. 51. If n = 0 Then Exit Sub 52. 53. Dim str1, str2
24