6.2.10 tools组件
tools组件提供了一些有用的方法,可在页面中检查文件是否存在、处理一个html窗体、以及产生一个随机整数,还有用于macintosh计算机的一些方法,还可以检查是否存在某个服务器插件以及检查用户是否是网站的拥有者。
1. tools组件的成页
tools组件提供了五个方法,其中两个依赖于操作系统,如表6-8所示:
表6-8 tools组件的方法及说明
方 法
说 明
fileexists(relative_url)
如果relative_url指定的文件存在,返回值为true,否则为false。必须给出虚拟相对路径及文件名,并且文件必须存在于发布的web网站目录中。
random()
产生一个位于-32768~32767之间的随机整数。使用abs函数(vbscript)或math.abs(jscript)得到在0~32768之间的正整数。使用mod运算符(vbscript)或%运算符(jscript)得到指定范围内的一个数值。例如:
intrand = (objtools.random mod 76) + 25
得到一个在25~100之间的整数。
processform(output_url,
template_url,[insertion_point])
通过template_url指定的文件处理一个html窗体,并且插入来自窗体中已提交给当前页面的数值。结果写进output_url指定的文件,如果指定了可选项insertion_point(字符串)参数的话,组件可在已存在的输出文件中找到这个字符串,并在该位置插入新的内容。如果insertion_point参数没有指定,任何已存在的output_url文件则被新的输出取代
owner
仅适用于macintosh机,如果当前用户帐户是web网站的拥有者,返回值为true,否则返回值为false
pluginexists(plugin_name)
仅适用于macintosh机,如果指定的服务器plugin_name安装在机器上,返回值为true,否则为false
2. 使用fileexists方法
在允许用户访问之前,可以使用fileexists方法检查某些文件是否存在于服务器中(注意这个方法和filesystemobject.fileexists以同样的方式工作)。
下面的例子中,用户提供了网页的相对url,如果用户想通过在名为txturl的文本框中键入url打开网页,在重新定向之前可以检查其是否存在。
<% // in jscript:
var objtools = server.createobject(mswc.tools);
var strurl = request.form(txturl); // collect the page url they entered
if (objtools.fileexists(strurl)) // see if it exists
server.transfer(strurl) // if it does, transfer to it
else // or if not display a message
response.write(sorry, the page you requested does not exist);
%>
这里提供了一个示例页面(使用vbscript)来演示组件的三种方法(非macintosh),可以从asp installable components主菜单中运行,如图6-16所示:
图6-16 运行tools组件的方法的页面
网页的第一部分允许输入一个文件的相对url,并告诉用户该文件是否存在。示例提供的缺省值是查看网站的根目录中是否有global.asa文件。点击按钮时,说明该文件是否可找到的信息将放在页面的顶部,如图6-17所示:
图6-17 运行fileexists方法的结果
把页面的所有控件放在<form>中,提交回本页面,这已经成为一种规范。在页面的开始,查看点击了哪个按钮。如果是fileexists的按钮,就调用组件的fileexists方法并显示合适的信息。
look for a command sent from the form section buttons
if len(request.form("cmdexists")) then
strfile = request.form("txtfile")
if objtools.fileexists(strfile) then
response.write "the file <b>" & strfile & "</b> does exist.<p>"
else
response.write "the file " & strfile & " <b>does not</b> exist.<p>"
end if
end if
3.使用tools.random方法
在asp页面中,有时需要一个随机数来完成某些任务,例如,把用户重新定位到一个随机网页、选择颜色或显示每日提示。可以使用vbscript中的rnd函数,但要把所得数值转变成指定范围内的整数。tools组件的random方法更易于使用,因为能够直接提供整数值。
random方法的结果是一个在-32768~32767范围中的整数值,为了获得一个指定范围的整数,可以使用脚本语言中的abs函数并对下一个最大的整数取模。例如为了用vbscript语言创建0~20的正整数,可以使用下列语句:
intrandom = abs(objtools.random) mod 21
为了得到在50~100之间的数值,可以用:
intrandom = (abs(objtools.random) mod 51) + 50
示例网页使用这项技术生成随机数时,首先需要检查由用户输入的数值,以保证这些数值既是有效正整数又有正确的相对关系。
if len(request.form("cmdrandom")) then
intmin = -1 preset to illegal values and then
intmax = -1 only set if a valid number is entered
strmin = request.form("txtminimum")
strmax = request.form("txtmaximum")
if isnumeric(strmin) then intmin = cstr(strmin)
if isnumeric(strmax) then intmax = cstr(strmax)
if (intmin >= 0) and (intmax > intmin) then
intrandom = (abs(objtools.random) mod (intmax – intmin + 1)) + intmin
response.write "your random value is: <b>" & intrandom & "</b><p>"
else
response.write "<b>the numbers you entered are not valid.</b><p>"
end if
end if
当页面重新调入时,结果显示在网页的顶部,如图6-18所示:
图6-18 运行random方法的结果
4.使用tools.processform方法
tools组件中最复杂的方法是processform,用来读取存在磁盘上的临时文件,并在其中插入创建的信息(可能来自当前页面的request.form集合的内容),然后把结果作为一个文件输出到磁盘,这个方法的语法是:
processform (output_url, template_url, [insertion_point])
临时文件和输出文件相对于当前页面使用相对url来定义。输出文件可以是asp网页,如果这样,当其在浏览器打开时,将正常处理。临时文件可以包含普通的asp代码,但不运行,仅简单地拷贝输出文件。然而,如果把临时文件中的asp代码放在<%%…%%>限定符中,当临时文件调入时代码将被执行,这允许动态生成的数值(诸如进行处理的时间和日期)插入到输出页面中。
下面是示例文件template.asp(在chapter06目录的tools子目录中):
this file was created by the asp tools component
————————————————
the content of the request was:
output file name: <%% = request.form("txtoutput") %%>
template file name: <%% = request.form("txttemplate") %%>
insertion point text: <%% = request.form("txtinsert") %%>
————————————————
created <%% = now() %%>
示例页面包含着预定使用这个临时文件的控件,这些控件创建一个和临时文件在同一个文件夹中的名为output.asp的输出文件,如图6-19所示:
图6-19 运行tools.processform方法的界面
点击按钮时,将运行一部分asp代码,从文本框中采集数据并调用processform方法:
if len(request.form("cmdprocess")) then
strtemplate = request.form("txttemplate")
stroutput = request.form("txtoutput")
strinsertpoint = request.form("txtinsert")
…
we display the template contents here
…
process the form contents
objtools.processform stroutput, strtemplate, strinsertpoint
…
we display the output file contents here
…
end if
(1) 设置输出文件的访问权限
如果得到一个“mswc.tools error 80004005 couldn’t open output file”错误信息,这意味着iis不允许向指定的目录中写入输出文件。解决这个问题最快捷的方法是,在properties对话框中的security选项卡中,将相应的tools目录以及存放output.asp的目录的everyone组的full control设置成允许,如图6-20所示:
图6-20 设置输出文件访问权限的界面
(2) 查看文件内容
图6-21显示了上面代码用缺省值运行缺省值运行的结果,可以看到原来的临时文件内容,以及使用processform方法插入到输出文件中的内容。
图6-21 输出文件的内容
这里省略了显示来自前面见过的文件内容的程序代码。显示内容的方法与asp installable components主菜单页面中用于显示内容链接列表文件的方法相同。使用filesystemobject和textstream对象的实例把整个文件读入到一个字符串中,然后将其插入网页中(记住要使用server.htmlencode方法,以便把字符“<”和“>”转换成可用于显示的字符)。
quot = chr(34) double-quote character
…
create a filesytemobject object to display the template file contents
set objfso = server.createobject("scripting.filesystemobject")
response.write "the content of the template file <b>" & _
strtemplate & "</b> is:"
set objtstream = objfso.opentextfile(server.mappath(strtemplate), forreading)
strcontent = server.htmlencode(objtstream.readall) read whole file
objtstream.close
response.write "<div class=" & quot & "showcode" & quot & "><pre>" & _
strcontent & "</pre></div>"
…
(3) 关于插入点参数
processform方法可选的insertion_point参数能用来在文件中的特定点插入文本,这个方法是先查找输出文件中特定字符串的第一个实例,然后在该位置插入创建的新内容,在输出文件中通过把星号(*)放在insertion_point字符串前面来放置新内容。如果省略了insertion_point参数,新的内容会取代整个输出文件。
注意insertion_point参数在tools.processform方法的早期版本中不能使用。
