欢迎光临
我们一直在努力

ASP中使用ServerVariables集合详解-ASP教程,ASP技巧

建站超值云服务器,限时71元/月

当讨论request对象内容时,要研究的集合之一就是servervariables集合。这个集合包含了两种值的结合体,一种是随同页面请求从客户端发送到服务器的http报头中的值,另外一种是由服务器在接收到请求时本身所提供的值。

  “自引用”页面

  在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”_
& “search for the manufacturer using a search engine, such as”_
& “<a href=” & chr(34) _
& “http://www.altavista.digital.com/cgi-bin/query?q=”_
& strproduct _
& chr(34) & “>http://www.altavista.com/</a>

end if
end if
end if
%>

  对ie 5.0和navigator 4.61的搜索结果分别不同,对于其他厂家的浏览器,可以得到一个链接在alta vista web站点自动开始搜索厂家的名称。

  注意,netscape在用户代理字符串中不提供厂家的名称,因而无法绝对保证一个浏览器一定是navigator。

  检测浏览器的语言

  servervariables集合中另外一个有用的值是“http_accept_language”,它包含了一个当浏览器安装时指定的,或硬编码进用户的地区版本的语言代码。语言代码的例子有en-us(英国、美国)、de-at(德国、澳大利亚)和es-pe(西班牙、秘鲁)。

  语言代码可以是一般的且省略方言标识:例如,在我们的站点wrox者,大批浏览者都是将en(英语)作为语言代码。

  因此,可以检测语言代码并自动装载一个合适的特定地区或指定语言版本的页面。

strlocale = lcase(left(request.servervariables(“http_accept_language”),2))
select case strlocale
 case “en”: response.redirect “http://uk_site.co.uk/”
 case “de”: response.redirect “http://de_site.co.de/”
 case “fr”: response.redirect “http://fr_site.co.fr/”
 ‘… etc
 case else: response.redirect “http://us_sitel.com/”
end select

  或者根据特定的方言,重定向页面:

strlocale = lcase(request.servervariables(“http_accept_language”))
select case strlocale
 case “en-gb”: response.redirect “http://uk_site.co.uk/”
 case “en-us”: response.redirect “http://us_site.com/”
 case “es-pe”: response.redirect “http://es_site2.co.pe/”
 ‘…
 case else: response.redirect “http://us_site1.com/”
end select

  其他有用的servervariables集合的值

  可以访问和使用servervariables集合中的任何一成员,控制asp页面响应一个请求的方式。可以检查一个浏览者访问站点时使用的是否是缺省端口80或还是另一个。在这个例子里,寻找通过端口443的访问——这个端口提供的是安全套接字层(secure socket layer,ssi)访问(和其他的协议),且将它们重定向到一个相应的页面。

if request.servervariables(“server_port”) = “443”) then
response.redirect “/securesite/default.asp” ‘secure user
else
response.redirect “/normalsite/default.asp” ‘non-secure user
end if 

  假如要求浏览者注册且由服务器验证(而不是允许他们在web服务器的iuser帐号下匿名访问,这个问题将在后面章节中详细讨论),可以查询用户名称,来判定正在与我们打交道的用户是谁,是否装载页面给该用户。例如,下面的这个代码将只向名为administrator的用户显示管理链接。


<a href=”dispcnfg.asp”>change display configuration</a>

<a href=”dispcolr.asp”>change display colors</a>

<a href=”keyboard.asp”>change keyboard configuration</a>

<%
if request.servervariables(“auth_user”) _
= ucase(request.servervariables(“server_name”)) & “\administrator” then
%>
<a href=”allusers.asp”>administer all users</a>

<a href=”usrlogon.asp”>administer logon information</a>
<%
end if
%>

  注意asp不填写servervariables集合直到你访问其中的一个成员。首次访问该集合的一个成员将使iis得到它的全部,应只在需要时才使用servervariables集合。

  其他request和response技巧

  现在,来看一下几个使用request和response对象的有用技巧,包括:

  · 连接、缓冲和页面重定向的管理。

  · http报头、缓存与“到期”页面的操作。

  · 利用客户证书。

  · 创建定制的日志文件消息。

  1. 连接、缓冲和页面重定向的管理

  asp的一个很有用的特点就是使用户能够从一个asp网页转向到另一个网页(asp或html),或另一个源文件(例如一个zip文件或文本文件)。这对用户来说是透明的,实际上是浏览器做这个工作。当使用response.redirect方法来载入一个新的网页时,实际上是发送回一个特殊的http报头到客户。此报头为:

  http/1.1 302 object moved
  location /newpath/newpage.asp

  浏览器读到此报头信息,并按location值的指示载入页面。这在功能上与在web页中使用客户端html<meta>标记相同,例如:

<meta http-equiv=”refresh” content=”0;url=/newpath/newpage.asp”>

  这带来的一个问题是,服务器与用户之间的代理服务器可能会提供它自己的包含与新页面的链接的消息,而不是直接载入新页面。而且浏览器根据厂商和版本可能做同样的工作。这就去除了假定的透明,而且对用户来说一直收到的是错误信息,则对你的站点的访问变得比较麻烦。

  在发送诸如文本或html等任何页面内容后,我们就不能再使用redirect方法。然而,一个看起来能够限制“代理服务器影响”的方法是,先确定没有输出(包括http报头)被发送到客户。在asp 2.0中,必须打开缓冲,然后使用clear方法来清空缓冲区:

response.buffer = true
‘some condition to select the appropriate page:
if request.servervariables(“server_port”) = 1856 then
 strnewpage = “/newpath/this_page.asp”
else
 strnewpage = “/newpath/the_other_page.asp”
end if
response.clear
response.redirect strnewpage

  在asp 3.0中,缓冲缺省为打开,所以第一行可被忽略,但它是无害的,而且能确保我们的网页即使在asp 2.0环境中也仍然能工作。
与其使用这种类型的http报头重定向,不如使用asp 3.0的一个新特性,它允许我们通过server对象的transfer方法转换为执行另一个网页,我们将在以后进一步研究这个问题。

  1) asp页面缓冲区

  正如已看到过的,iis 5.0中asp 3.0页面缓冲是缺省打开的,在早期的版本中是缺省关闭的。微软告诉我们缓冲在iis 5.0中提供了更有效的网页传送,这就是缓冲缺省状态被改变的原因。在大部分情况下,这对我们没有影响。但是,假如有一个非常大的网页,或一个用asp或别的服务器端代码和组件花费一定时间创建的网页,当其各部分完成时,我们能够分批刷新它们到客户:


… code to create first part of the page

response.flush

… code to create next part of page

response.flush

  有时可能希望在页面结束之前的某些点上停止代码的执行,可以通过调用end方法去刷新所有的当前内容到客户并中止任何进一步的处理过程。


… code to create first part of the page
if strusername = “” then response.clear

… code to create a new version of this part of the page

  这里有两上演示缓冲和重定向的实例网页,可以从“response object”主页面(sow_response.asp)下载它们。第一个response.redirect例子网页命名为redirect.asp,它在缓冲的页面中定入一些内容,清除缓冲区,并重定向到另一个网页:

for intloop = 1 to 1000000
 response.write “.”
next
response.clear
response.redirect “show_redirect.asp”
response.end

  目标页show_response.asp,做同样的工作,但重定向则是回到“response object”主页。因为这些网页都在缓冲区内,而且所有的输出在重定向之前必须清除,故在浏览器中没有可见的输出。然而,可以通过观察浏览器的状态看到发生的每一次重定向。如下图所示:

<img src=http://go2.163.com/~davelu/asp14.jpg>

  在“response object”主页中,点击“response.flush”链接将打开第二个示例网页usebuffer.asp,它简单地遍历一个字符串的每一个字符,以一定的延迟将它们刷新到客户,这虽是web服务器和asp极低效率的使用方式,但它演示了缓冲的工作方式。

<img src=http://go2.163.com/~davelu/asp15.jpg>

  下面是所要求的最小化的asp代码,注意我们分别把每个字符刷新到浏览器,因为不这样的话它将被存放在缓冲区中,直至网页完成:

strtext = “this text has been flushed to the browser using “ & _
“<b>response.flush</b>

for intchar =1 to len(strtext)
 for intwrite = 1 to 100000
 next
 response.write mid(strtext,intchar,1)
 response.flush
next

  2) response.isclientconnected属性

  isclientconnected属性在asp 2.0中已经存在了,但却有些不可靠。在其返回一个准确的结果之前必须发送一些输出到客户。这一问题在asp 3.0中已被解决。现在这一属性可被自由使用。

  isclientconnected是观察用户是否仍连到服务器和正在载入asp创建的网页的有用方式。如果用户断开连接或停止下载,我们就不用再浪费服务器的资源创建网页,因为缓冲区内容将被iis丢弃。所以,对那些需要大量时间计算或资源使用较多的网页来说,值得在每一阶段都检查浏览器是否已离线:


… code to create first part of the page

if response.isclientconnected then
response.flush
else
response.end
end if

… code to create next part of page…

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP中使用ServerVariables集合详解-ASP教程,ASP技巧
分享到: 更多 (0)