当前位置: 移动技术网 > IT编程>开发语言>.net > ASP 3.0高级编程(二十七)

ASP 3.0高级编程(二十七)

2019年01月22日  | 移动技术网IT编程  | 我要评论

王巨禄,www.dicj.cc,暗夜第六感好看吗

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          & nbsp;                                     ;           // 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.(在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("cmd

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网