使用ServerVariables集合
当讨论Request对象内容时,要研究的集合之一就是ServerVariables集合。这个集合包含了两种值的结合体,一种是随同页面请求从客户端发送到服务器的HTTP报头中的值,另外一种是由服务器在接收到请求时本身所提供的值。为显示ServerVariables集合中值的使用方式,在Request Object页面(Show_request.asp)中,点击“ServerVariables Examples”链接,打开另外一个页面,如下图所示:
![]() 下图所示窗口显示的是ServerVariables集合中一些非常有用的值的一个子集。 ![]() “自引用”页面 在ServerVariables集合中返回的值包含Web服务器的详细信息和当前页面的路径信息。在任何地方创建一个页面都可使用这些信息。例如创建一个“自引用”页面,此页面能够再次调用自身完成另一项任务,我们可以用以下代码: <FORM ACTION=”<% = Request.ServerVariables(“PATH_INFO”) %>” METHOD=”POST”> 同样的效果可以用HTTP的“SCRIPT_NAME”值获得: <FORM ACTION=”<% = Request.ServerVariables(“SCRIPT_NAME”) %>” METHOD=”POST”> 使用<A>元素打开一个不同页,可以使用: ... <% strFullPath = Request.ServerVariables(“PATH_INFO”) ‘Strip off the file name strPathOnly = Left(strFullPath, InStrRev(strFullPath, “/”)) strNextPage = strPathOnly & “pages/next_page.asp” %> ... <A HREF=”<% = strNextPage %>”>Next Page</A> ... 即使原始页面的名称或位置发生变化,这些实例都能正常工作,因为使用了当前页面的路径信息(当然,第二个例子在分离的目标页的名称发生变化时运行会失败)。 换句话说,如果为搜索引擎的子会话自动建立URL,可以收集ServerVariable的一些值: strFullURL = http:// & Request.ServerVariables(“LOCAL_ADDR”) _ & “:” & Request.ServerVariables(“SERVER_PORT”) _ & Request.ServerVariables(“PATH_INFO”) 这将创建一个完整的URL包括端口号(这种情况下,不是标准值80)。例如,结果可能是: http://194.74.60.254:1768/thispath/thispage.asp 检测浏览器的版本 ServerVariables集合中,另外一个有用的值是用户浏览器的用户代理字符串。在“Detecting the Browser Type”页面(browsertype.asp),使用ServerVariables集合中的“HTTP_USER_AGENT”值来获得用户代理字符串,一些脚本用来解析该信息并寻找生产厂家名称和浏览器版本。 <% strUA = Request.ServerVariables(“HTTP_USER_AGENT”) Response.Write “The User Agent string is <B>” & strUA & “</B> ” If InStr(strUA, “MSIE”) Then Response.Write “To upgrade your browser go to “_ & “<A HREF=” & Chr(34) & http://www.microsoft.com/ie/”_ & Chr(34) & “>http://www.microsoft.com/ie/<A> ” intVersion = Cint(Mid(strUA, InStr(strUA, “MSIE”) + 5, 1)) If intVersion >=4 Then Response.Write “You can use Microsoft Dynamic HTML” End If Else If InStr(strUA, “Mozilla”) Then If InStr(strUA, “compatible;”) = 0 Then Response.Write “Your browser is probably Navigator. You can “_ & “download the latest version of Navigator from “_ & “<A HREF=” & Chr(34) & http://home.netscape.com/”_ & “download/”& Chr(34) & “>http://home.netscape.com”_ & “/download/</A> ” intVersion = Cint(Mid(strUA, InStr(strUA, “/”) +1, 1)) If intVersion >= 4 Then Response.Write “You can probably use Netscape Dynamic HTML” End If Else strVersion = Mid(strUA, InStr(strUA, “compatible;”) + 12) strProduct = Left(strVersion, InStr(strVersion, “ “)) Response.Write “Your browser is Navigator-compatible. You can”_ 上一篇:其他Request和Response技巧 下一篇:使用Form和QueryString集合 更多相关文章
|
推荐文章
精彩文章
|