ASP 实例


基础

用ASP写文本

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. response.write("Hello World!")
  7. %>
  8.  
  9. </body>
  10. </html>

向文本添加HTML

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <%
  5. response.write("<h2>You can use HTML tags to format the text!</h2>")
  6. %>
  7.  
  8. <%
  9. response.write("<p style='color:#0000ff'>This text is styled with the style attribute!</p>")
  10. %>
  11. </body>
  12. </html>

变量

声明变量
变量用于存储信息。本例演示如何声明变量,为变量赋值,并在程序中使用这个变量。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim name
  7. name="Donald Duck"
  8. response.write("My name is: " & name)
  9. %>
  10.  
  11. </body>
  12. </html>
  1. My name is: Donald Duck

声明数组
数组用于存储一系列相关的数据项目。本例演示如何声明一个存储名字的数组。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Dim famname(5),i
  7. famname(0) = "Jan Egil"
  8. famname(1) = "Tove"
  9. famname(2) = "Hege"
  10. famname(3) = "Stale"
  11. famname(4) = "Kai Jim"
  12. famname(5) = "Borge"
  13.  
  14. For i = 0 to 5
  15.       response.write(famname(i) & "<br>")
  16. Next
  17. %>
  18.  
  19. </body>
  20. </html>
  1. Jan Egil
  2. Tove
  3. Hege
  4. Stale
  5. Kai Jim
  6. Borge

循环生成 HTML 标题
本例演示如何循环生成 6 个不同的 HTML 标题。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim i 
  7. for i=1 to 6
  8.    response.write("<h" & i & ">Heading " & i & "</h" & i & ">")
  9. next
  10. %>
  11.  
  12. </body>
  13. </html>

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

使用 Vbscript 制作基于时间的问候语
本例演示如何根据服务器时间向用户显示不同的消息。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <%
  5. dim h
  6. h=hour(now())
  7.  
  8. response.write("<p>" & now())
  9. response.write("</p>")
  10. If h<12 then
  11.    response.write("Good Morning!")
  12. else
  13.    response.write("Good day!")
  14. end if
  15. %>
  16. </body>
  17. </html>
  1. 9/20/2013 11:08:35 PM
  2. Good day!

使用 JavaScript 制作基于时间的问候语
本例同上,演示如何根据服务器时间向用户显示不同的消息,只是语法不同而已。

  1. <%@ language="javascript" %>
  2. <!DOCTYPE html>
  3. <html>
  4. <body>
  5. <%
  6. var d=new Date()
  7. var h=d.getHours()
  8.  
  9. Response.Write("<p>")
  10. Response.Write(d)
  11. Response.Write("</p>")
  12. if (h<12)
  13.    {
  14.    Response.Write("Good Morning!")
  15.    }
  16. else
  17.    {
  18.    Response.Write("Good day!")
  19.    }
  20. %>
  21. </body>
  22. </html>
  1. Fri Sep 20 23:08:38 EDT 2013
  2. Good day!


Date/Time 函数(VBScript)

日期和时间

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. Today's date is: <%response.write(date())%>.
  6. <br>
  7. The server's local time is: <%response.write(time())%>.
  8.  
  9. </body>
  10. </html>

获得天的名字

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <p>
  6. VBScripts' function <b>WeekdayName</b> is used to get a weekday:
  7. </p>
  8. <%
  9. response.Write(WeekDayName(1))
  10. response.Write("<br>")
  11. response.Write(WeekDayName(2))
  12. %>
  13.  
  14. <p>Abbreviated name of a weekday:</p>
  15. <%
  16. response.Write(WeekDayName(1,true))
  17. response.Write("<br>")
  18. response.Write(WeekDayName(2,true))
  19. %>
  20.  
  21. <p>Current weekday:</p>
  22. <%
  23. response.Write(WeekdayName(weekday(date)))
  24. response.Write("<br>")
  25. response.Write(WeekdayName(weekday(date), true))
  26. %>
  27.  
  28. </body>
  29. </html>

获得月的名字

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <p>VBScripts' function <b>MonthName</b> is used to get a month:</p>
  6. <%
  7. response.Write(MonthName(1))
  8. response.Write("<br>")
  9. response.Write(MonthName(2))
  10. %>
  11.  
  12. <p>Abbreviated name of a month:</p>
  13. <%
  14. response.Write(MonthName(1,true))
  15. response.Write("<br>")
  16. response.Write(MonthName(2,true))
  17. %>
  18.  
  19. <p>Current month:</p>
  20. <%
  21. response.Write(MonthName(month(date)))
  22. response.Write("<br>")
  23. response.Write(MonthName(month(date), true))
  24. %>
  25.  
  26. </body>
  27. </html>

获得今天的月和天

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. Today it is
  6. <%response.write(WeekdayName(weekday(date)))%>,
  7. <br>
  8. and the month is
  9. <%response.write(MonthName(month(date)))%>
  10.  
  11. </body>
  12. </html>

3000年倒计时

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <p>Countdown to year 3000:</p>
  6.  
  7. <p>
  8. <%millennium=cdate("1/1/3000 00:00:00")%>
  9.  
  10. It is
  11. <%response.write(DateDiff("yyyy", Now(), millennium))%>
  12. years to year 3000!
  13. <br>
  14. It is
  15. <%response.write(DateDiff("m", Now(), millennium))%>
  16. months to year 3000!
  17. <br>
  18. It is
  19. <%response.write(DateDiff("ww", Now(), millennium))%>
  20. weeks to year 3000!
  21. <br>
  22. It is
  23. <%response.write(DateDiff("d", Now(), millennium))%>
  24. days to year 3000!
  25. <br>
  26. It is
  27. <%response.write(DateDiff("h", Now(), millennium))%>
  28. hours to year 3000!
  29. <br>
  30. It is
  31. <%response.write(DateDiff("n", Now(), millennium))%>
  32. minutes to year 3000!
  33. <br>
  34. It is
  35. <%response.write(DateDiff("s", Now(), millennium))%>
  36. seconds to year 3000!
  37. </p>
  38.  
  39. </body>
  40. </html>

从今天起计算N天后

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <%
  5. response.write(DateAdd("d",30,date()))
  6. %>
  7.  
  8. <p>
  9. Syntax for DateAdd: DateAdd(interval,number,date). You can use <b>DateAdd</b> to for example calculate a date 30 days from today.
  10. </p>
  11.  
  12. </body>
  13. </html>

格式化日期和时间

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. response.write(FormatDateTime(date(),vbgeneraldate))
  7. response.write("<br>")
  8. response.write(FormatDateTime(date(),vblongdate))
  9. response.write("<br>")
  10. response.write(FormatDateTime(date(),vbshortdate))
  11. response.write("<br>")
  12. response.write(FormatDateTime(now(),vblongtime))
  13. response.write("<br>")
  14. response.write(FormatDateTime(now(),vbshorttime))
  15. %>
  16.  
  17. <p>
  18. Syntax for FormatDateTime: FormatDateTime(date,namedformat).
  19. </p>
  20.  
  21. </body>
  22. </html>

这是一个日期吗?

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. somedate="10/30/99"
  7. response.write(IsDate(somedate))
  8. %>
  9.  
  10. </body>
  11. </html>

其他函数(VBScript)

大写或小写的字符串

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. name = "Bill Gates"
  7. response.write(ucase(name))
  8. response.write("<br>")
  9. response.write(lcase(name))
  10. %>
  11.  
  12. </body>
  13. </html>

字符串去掉前后空格

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. name = " jianzhan "
  7. response.write("visit" & name & "now<br>")
  8. response.write("visit" & trim(name) & "now<br>")
  9. response.write("visit" & ltrim(name) & "now<br>")
  10. response.write("visit" & rtrim(name) & "now")
  11. %>
  12.  
  13. </body>
  14. </html>

如何转化一个字符串?

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. sometext = "Hello Everyone!"
  7. response.write(strReverse(sometext))
  8. %>
  9.  
  10. </body>
  11. </html>

四舍五入

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. = 48.66776677
  7. = 48.3333333
  8. response.write(Round(i))
  9. response.write("<br>")
  10. response.write(Round(j))
  11. %>
  12.  
  13. </body>
  14. </html>

一个随机数

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. randomize()
  7. response.write(rnd())
  8. %>
  9.  
  10. </body>
  11. </html>

从一个字符串返回特定的字符

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. sometext="Welcome to this Web"
  7. response.write(Left(sometext,5))
  8. response.write("<br>")
  9. response.write(Right(sometext,5))
  10. %>
  11.  
  12. </body>
  13. </html>

替换字符串的特定字符

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. sometext="Welcome to this Web!!"
  7. response.write(Replace(sometext, "Web", "Page"))
  8. %>
  9.  
  10. </body>
  11. </html>

从字符串中返回指定数目的字符

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. sometext="Welcome to this Web!!"
  7. response.write(Mid(sometext, 9, 2))
  8. %>
  9.  
  10. </body>
  11. </html>

程序

调用使用VBScript的子程序

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <%
  5. sub vbproc(num1,num2)
  6. response.write(num1*num2)
  7. end sub
  8. %>
  9. </head>
  10.  
  11. <body>
  12. <p>You can call a procedure like this:</p>
  13. <p>Result: <%call vbproc(3,4)%></p>
  14. <p>Or, like this:</p>
  15. <p>Result: <%vbproc 3,4%></p>
  16. </body>
  17. </html>

调用使用JavaScript的子程序

  1. <%@ language="javascript" %>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <%
  6. function jsproc(num1,num2)
  7. {
  8. Response.Write(num1*num2)
  9. }
  10. %>
  11. </head>
  12.  
  13. <body>
  14. <p>Result: <%jsproc(3,4)%></p>
  15. </body>
  16. </html>

调用使用VBScript和JavaScript的子程序

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <%
  5. sub vbproc(num1,num2)
  6. Response.Write(num1*num2)
  7. end sub
  8. %>
  9. <script  language="javascript" runat="server">
  10. function jsproc(num1,num2)
  11. {
  12. Response.Write(num1*num2)
  13. }
  14. </script>
  15. </head>
  16.  
  17. <body>
  18. <p>Result: <%call vbproc(3,4)%></p>
  19. <p>Result: <%call jsproc(3,4)%></p>
  20. </body>
  21.  
  22. </html>

表单

使用method="get"的表单

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <form action="demo_reqquery.asp" method="get">
  5. Your name: <input type="text" name="fname" size="20" />
  6. <input type="submit" value="Submit" />
  7. </form>
  8. <%
  9. dim fname
  10. fname=Request.QueryString("fname")
  11. If fname<>"" Then
  12.       Response.Write("Hello " & fname & "!<br>")
  13.       Response.Write("How are you today?")
  14. End If
  15. %>
  16. </body>
  17. </html>

使用method="post"的表单

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <form action="demo_simpleform.asp" method="post">
  5. Your name: <input type="text" name="fname" size="20" />
  6. <input type="submit" value="Submit" />
  7. </form>
  8. <%
  9. dim fname
  10. fname=Request.Form("fname")
  11. If fname<>"" Then
  12.       Response.Write("Hello " & fname & "!<br>")
  13.       Response.Write("How are you today?")
  14. End If
  15. %>
  16. </body>
  17. </html>

使用单选按钮的表单

  1. <!DOCTYPE html>
  2. <html>
  3. <%
  4. dim cars
  5. cars=Request.Form("cars")
  6. %>
  7. <body>
  8. <form action="demo_radiob.asp method="post">
  9. <p>Please select your favorite car:</p>
  10.  
  11. <input type="radio" name="cars"
  12. <%if cars="Volvo" then Response.Write("checked")%>
  13. value="Volvo">Volvo</input>
  14. <br>
  15. <input type="radio" name="cars"
  16. <%if cars="Saab" then Response.Write("checked")%>
  17. value="Saab">Saab</input>
  18. <br>
  19. <input type="radio" name="cars"
  20. <%if cars="BMW" then Response.Write("checked")%>
  21. value="BMW">BMW</input>
  22. <br><br>
  23. <input type="submit" value="Submit" />
  24. </form>
  25. <%
  26. if cars<>"" then
  27.    Response.Write("<p>Your favorite car is: " & cars & "</p>")
  28. end if
  29. %>
  30. </body>
  31. </html>

Cookie

Welcome cookie

  1. <%
  2. dim numvisits
  3. response.cookies("NumVisits").Expires=date+365 
  4. numvisits=request.cookies("NumVisits")
  5.  
  6. if numvisits="" then
  7.    response.cookies("NumVisits")=1
  8.    response.write("Welcome! This is the first time you are visiting this Web page.")
  9. else
  10.    response.cookies("NumVisits")=numvisits+1
  11.    response.write("You have visited this ")
  12.    response.write("Web page " & numvisits)
  13.    if numvisits=1 then
  14.      response.write " time before!"
  15.    else
  16.      response.write " times before!"
  17.    end if
  18. end if
  19. %>
  20. <!DOCTYPE html>
  21. <html>
  22. <body>
  23. </body>
  24. </html>

Response 对象

使用 ASP 写文本
本例演示如何使用 ASP 来写文本。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. response.write("Hello World!")
  7. %>
  8.  
  9. </body>
  10. </html>
  1. Hello World!

在 ASP 中使用 HTML 标签格式化文本
本例演示如何使用 ASP 将文本和 HTML 标签结合起来。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <%
  5. response.write("<h2>You can use HTML tags to format the text!</h2>")
  6. %>
  7.  
  8. <%
  9. response.write("<p style='color:#0000ff'>This text is styled with the style attribute!</p>")
  10. %>
  11. </body>
  12. </html>

You can use HTML tags to format the text!

This text is styled with the style attribute!

将用户重定向至一个不同的 URL
本例演示如何将用户重定向至一个不同的 URL。

  1. <%
  2. if Request.Form("select")<>"" then
  3.        Response.Redirect(Request.Form("select"))
  4. end if
  5. %>   
  6.  
  7. <!DOCTYPE html>
  8. <html>
  9. <body>
  10.  
  11. <form action="demo_redirect.asp" method="post">
  12.  
  13. <input type="radio" name="select" 
  14. value="demo_server.asp">
  15. Server Example<br>
  16.  
  17. <input type="radio" name="select" 
  18. value="demo_text.asp">
  19. Text Example<br><br>
  20. <input type="submit" value="Go!">
  21.  
  22. </form>
  23.  
  24. </body>
  25. </html>

显示随机的链接
本例演示如何创建一个随机的链接。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. randomize()
  7. r=rnd()
  8. if r>0.5 then
  9.   response.write("<a href='http://www.shouce.ren'>jianzhan.cc!</a>")
  10. else
  11.   response.write("<a href='http://www.refsnesdata.no'>Refsnesdata.no!</a>")
  12. end if
  13. %>
  14.  
  15. <p>
  16. This example demonstrates a link, each time you load the page, it will display 
  17. one of two links: jianzhan.cc! OR Refsnesdata.no! There is a 50% chance for 
  18. each of them.
  19. </p>
  20.  
  21. </body>
  22. </html>

控制缓冲区
本例演示如何控制缓冲区。

  1. <%
  2. Response.Buffer=true
  3. %>
  4. <!DOCTYPE html>
  5. <html>
  6. <body>
  7. <p>
  8. This text will be sent to your browser when my response buffer is flushed.
  9. </p>
  10. <%
  11. Response.Flush
  12. %>
  13. </body>
  14. </html>

清空缓冲区
本例演示如何清空缓冲区。

  1. <%
  2. Response.Buffer=true
  3. %>
  4. <!DOCTYPE html>
  5. <html>
  6. <body>
  7. <p>This is some text I want to send to the user.</p>
  8. <p>No, I changed my mind. I want to clear the text.</p>
  9. <%
  10. Response.Clear
  11. %>
  12. </body>
  13. </html>

在处理过程中终止脚本并返回结果
本例演示如何在处理过程中终止脚本。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>I am writing some text. This text will never be<br>
  5. <%
  6. Response.End
  7. %>
  8. finished! It's too late to write more!</p>
  9. </body>
  10. </html>

设置页面在失效前在浏览器中缓存时间
本例演示如何规定页面在失效前在浏览器中的缓存时间。

  1. <%Response.Expires=-1%>
  2. <!DOCTYPE html>
  3. <html>
  4. <body>
  5. <p>This page will be refreshed with each access!</p>
  6. </body>
  7. </html>

设置页面缓存在浏览器中的失效日期或时间
本例演示如何规定页面缓存在浏览器中的失效时间日期或时间。

  1. <%Response.Expires=-1%>
  2. <!DOCTYPE html>
  3. <html>
  4. <body>
  5. <p>This page will be refreshed with each access!</p>
  6. </body>
  7. </html>

检查用户是否仍然与服务器连接
本例演示如何检查用户是否已与服务器断开。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. If Response.IsClientConnected=true then
  7. Response.Write("The user is still connected!")
  8. else
  9. Response.Write("The user is not connected!")
  10. end if
  11. %>
  12.  
  13. </body>
  14. </html>

设置内容类型
本例演示如何规定内容的类型。

  1. <%
  2. Response.ContentType="text/html"
  3. %>
  4. <!DOCTYPE html>
  5. <html>
  6. <body>
  7.  
  8. <p>This is some text</p>
  9.  
  10. </body>
  11. </html>

设置字符集名称
本例演示如何规定字符集的名称。

  1. <%
  2. Response.Charset="ISO8859-1"
  3. %>
  4. <!DOCTYPE html>
  5. <html>
  6. <body>
  7.  
  8. <p>This is some text</p>
  9.  
  10. </body>
  11. </html>

Request 对象

当用户点击链接时发送查询信息
本例演示如何在链接中向页面发送查询信息,并在目标页面中取回这些信息(在本例中是同一页面)。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <a href="demo_simplequerystring.asp?color=green">Example</a>
  6.  
  7. <%
  8. Response.Write(Request.QueryString)
  9. %>
  10.  
  11. </body>
  12. </html>

QueryString 集合的简单应用
本例演示如何使用 QueryString 集合从表单取回值。(此表单使用 GET 方法,这意味着所发送的信息对用户来说是可见的。)

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form action="demo_simplereqquery.asp" method="get">
  6. First name: <input type="text" name="fname"><br>
  7. Last name: <input type="text" name="lname"><br>
  8. <input type="submit" value="Submit">
  9. </form>
  10.  
  11. <%
  12. Response.Write(Request.QueryString)
  13. %>
  14.  
  15. </body>
  16. </html>

如何使用来自表单的信息
本例演示如何使用从表单取回的值。此表单使用 GET 方法。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form action="demo_simplereqquery.asp" method="get">
  6. First name: <input type="text" name="fname"><br>
  7. Last name: <input type="text" name="lname"><br>
  8. <input type="submit" value="Submit">
  9. </form>
  10.  
  11. <%
  12. Response.Write(Request.QueryString)
  13. %>
  14.  
  15. </body>
  16. </html>

来自表单的更多信息
本例演示如果输入字段包含若干相同的名称,QueryString 集合会包含什么内容。它将展示如何使用 Count 关键词来对 "name" 属性进行计数。此表单使用 GET 方法。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. If Request.QueryString<>"" Then
  7.       If Request.QueryString("name")<>", " Then
  8.            name1=Request.QueryString("name")(1)
  9.            name2=Request.QueryString("name")(2)
  10.       end if
  11. end if
  12. %>
  13.  
  14. <form action="demo_reqquery2.asp" method="get">
  15. First name:
  16. <input type="text" name="name" value="<%=name1%>" />
  17. <br>
  18. Last name:
  19. <input type="text" name="name" value="<%=name2%>" />
  20. <br>
  21. <input type="submit" value="Submit" />
  22. </form>
  23. <hr>
  24. <%
  25. If Request.QueryString<>"" Then
  26.       Response.Write("<p>")
  27.       Response.Write("The information received from the form was:")
  28.       Response.Write("</p><p>")
  29.       Response.Write("name=" & Request.QueryString("name"))
  30.       Response.Write("</p><p>")
  31.       Response.Write("The name property's count is: ")
  32.       Response.Write(Request.QueryString("name").Count)
  33.       Response.Write("</p><p>")
  34.       Response.Write("First name=" & name1)
  35.       Response.Write("</p><p>")
  36.       Response.Write("Last name=" & name2)
  37.       Response.Write("</p>")
  38. end if
  39. %>
  40. </body>
  41. </html>

在线实例

Form 集合的简单应用
本例演示如何使用 Form 集合从表单取回值。(此表单使用 POST 方法,这意味着所发送的信息对用户来说是不可见的。)

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form action="demo_simpleform1.asp" method="post">
  6. First name:
  7. <input type="text" name="fname" value="Donald" />
  8. <br>
  9. Last name:
  10. <input type="text" name="lname" value="Duck" />
  11. <br>
  12. <input type="submit" value="Submit" />
  13. </form>
  14.  
  15. <%
  16. Response.Write(Request.Form)
  17. %>
  18.  
  19. </body>
  20. </html>

如何使用来自表单的信息
本例演示如何使用从表单取回的值。此表单使用了 POST 方法。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <form action="demo_simpleform.asp" method="post">
  5. Your name: <input type="text" name="fname" size="20" />
  6. <input type="submit" value="Submit" />
  7. </form>
  8. <%
  9. dim fname
  10. fname=Request.Form("fname")
  11. If fname<>"" Then
  12.       Response.Write("Hello " & fname & "!<br>")
  13.       Response.Write("How are you today?")
  14. End If
  15. %>
  16. </body>
  17. </html>

来自表单的更多信息
本例演示如果输入字段包含若干相同的名称,Form 集合会包含什么内容。它将展示如何使用 Count 关键词来对 "name" 属性进行计数。此表单使用了 POST 方法。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form action="demo_form2.asp" method="post">
  6. First name:
  7. <input type="text" name="name" value="Donald" />
  8. <br>
  9. Last name:
  10. <input type="text" name="name" value="Duck" />
  11. <br>
  12. <input type="submit" value="Submit" />
  13. </form>
  14. <hr>
  15.  
  16. <p>The information received from the form above was:</p>
  17. <%
  18. If Request.Form("name")<>"" Then
  19.       Response.Write("<p>")
  20.       Response.Write("name=" & Request.Form("name"))
  21.       Response.Write("</p><p>")
  22.       Response.Write("The name property's count is: ")
  23.       Response.Write(Request.Form("name").Count)
  24.       Response.Write("</p><p>")
  25.       Response.Write("First name=" & Request.Form("name")(1))
  26.       Response.Write("</p><p>") 
  27.       Response.Write("Last name=" & Request.Form("name")(2))
  28.       Response.Write("</p>") 
  29. End if
  30. %>
  31.  
  32. </body>
  33. </html>

带有单选按钮的表单
本例演示如何使用 Form 集合通过单选按钮与用户进行交互。此表单使用 POST 方法。

  1. <!DOCTYPE html>
  2. <html>
  3. <%
  4. dim cars
  5. cars=Request.Form("cars")
  6. %>
  7. <body>
  8. <form action="demo_radiob.asp method="post">
  9. <p>Please select your favorite car:</p>
  10.  
  11. <input type="radio" name="cars"
  12. <%if cars="Volvo" then Response.Write("checked")%>
  13. value="Volvo">Volvo</input>
  14. <br>
  15. <input type="radio" name="cars"
  16. <%if cars="Saab" then Response.Write("checked")%>
  17. value="Saab">Saab</input>
  18. <br>
  19. <input type="radio" name="cars"
  20. <%if cars="BMW" then Response.Write("checked")%>
  21. value="BMW">BMW</input>
  22. <br><br>
  23. <input type="submit" value="Submit" />
  24. </form>
  25. <%
  26. if cars<>"" then
  27.    Response.Write("<p>Your favorite car is: " & cars & "</p>")
  28. end if
  29. %>
  30. </body>
  31. </html>

带有复选框的表单
本例演示如何使用 Form 集合通过复选框与用户进行交互。此表单使用 POST 方法。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <%
  5. fruits=Request.Form("fruits")
  6. %>
  7.  
  8. <form action="demo_checkboxes.asp" method="post">
  9. <p>Which of these fruits do you prefer:</p>
  10. <input type="checkbox" name="fruits" value="Apples"
  11. <%if instr(fruits,"Apple") then Response.Write("checked")%>>
  12. Apple
  13. <br>
  14. <input type="checkbox" name="fruits" value="Oranges"
  15. <%if instr(fruits,"Oranges") then Response.Write("checked")%>>
  16. Orange
  17. <br>
  18. <input type="checkbox" name="fruits" value="Bananas"
  19. <%if instr(fruits,"Banana") then Response.Write("checked")%>>
  20. Banana
  21. <br>
  22. <input type="submit" value="Submit">
  23. </form>
  24. <%
  25. if fruits<>"" then%>
  26.    <p>You like: <%Response.Write(fruits)%></p>
  27. <%end if
  28. %>
  29.  
  30. </body>
  31. </html>

在线实例

获取服务器变量
本例演示如何取得访客的浏览器类型、IP 地址等信息。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>
  5. <b>You are browsing this site with:</b>
  6. <%Response.Write(Request.ServerVariables("http_user_agent"))%>
  7. </p>
  8. <p>
  9. <b>Your IP address is:</b>
  10. <%Response.Write(Request.ServerVariables("remote_addr"))%>
  11. </p>
  12. <p>
  13. <b>The DNS lookup of the IP address is:</b>
  14. <%Response.Write(Request.ServerVariables("remote_host"))%>
  15. </p>
  16. <p>
  17. <b>The method used to call the page:</b>
  18. <%Response.Write(Request.ServerVariables("request_method"))%>
  19. </p>
  20. <p>
  21. <b>The server's domain name:</b>
  22. <%Response.Write(Request.ServerVariables("server_name"))%>
  23. </p>
  24. <p>
  25. <b>The server's port:</b>
  26. <%Response.Write(Request.ServerVariables("server_port"))%>
  27. </p>
  28. <p>
  29. <b>The server's software:</b>
  30. <%Response.Write(Request.ServerVariables("server_software"))%>
  31. </p>
  32.  
  33. </body>
  34. </html>
  1. You are browsing this site with:
  2. Your IP address is: 42.120.45.233
  3. The DNS lookup of the IP address is: 42.120.45.233
  4. The method used to call the page: GET
  5. The server's domain name: shouce.ren
  6. The server's port: 80
  7. The server's software: Microsoft-IIS/7.5

创建 welcome cookie
本例演示如何创建一个 Welcome Cookie。

  1. <%
  2. dim numvisits
  3. response.cookies("NumVisits").Expires=date+365 
  4. numvisits=request.cookies("NumVisits")
  5.  
  6. if numvisits="" then
  7.    response.cookies("NumVisits")=1
  8.    response.write("Welcome! This is the first time you are visiting this Web page.")
  9. else
  10.    response.cookies("NumVisits")=numvisits+1
  11.    response.write("You have visited this ")
  12.    response.write("Web page " & numvisits)
  13.    if numvisits=1 then
  14.      response.write " time before!"
  15.    else
  16.      response.write " times before!"
  17.    end if
  18. end if
  19. %>
  20. <!DOCTYPE html>
  21. <html>
  22. <body>
  23. </body>
  24. </html>

探测用户发送的字节总数
本例演示如何探测用户在 Request 对象中发送的字节总数。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form action="demo_totalbytes.asp" method="post">
  6. Please type something:
  7. <input type="text" name="txt"><br><br>
  8. <input type="submit" value="Submit">
  9. </form>
  10.  
  11. <%
  12. If Request.Form("txt")<>"" Then
  13.    Response.Write("You submitted: ")
  14.    Response.Write(Request.Form)
  15.    Response.Write("<br><br>")
  16.    Response.Write("Total bytes: ")
  17.    Response.Write(Request.Totalbytes)
  18. End If
  19. %>
  20.  
  21. </body>
  22. </html>

Session 对象

设置并返回 LCID
本例演示 "LCID" 属性。该属性设置并返回一个指示位置或者地区的整数。类似于日期、时间和货币等内容都要根据位置或者地区来显示。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%  
  6. response.write("<p>")
  7. response.write("The default LCID for this page is: " & Session.LCID & "<br>")
  8. response.write("The Date format for the above LCID is: " & date() & "<br>")
  9. response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
  10. response.write("</p>")
  11.  
  12. Session.LCID=1036
  13.  
  14. response.write("<p>")
  15. response.write("The LCID is now changed to: " & Session.LCID & "<br>")
  16. response.write("The Date format for the above LCID is: " & date() & "<br>")
  17. response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
  18. response.write("</p>")
  19.  
  20. Session.LCID = 3079
  21.  
  22. response.write("<p>")
  23. response.write("The LCID is now changed to: " & Session.LCID & "<br>")
  24. response.write("The Date format for the above LCID is: " & date() & "<br>")
  25. response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
  26. response.write("</p>")
  27.  
  28. Session.LCID = 2057
  29.  
  30. response.write("<p>")
  31. response.write("The LCID is now changed to: " & Session.LCID & "<br>")
  32. response.write("The Date format for the above LCID is: " & date() & "<br>")
  33. response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
  34. response.write("</p>")
  35. %>
  36.  
  37. </body>
  38. </html>
  1. The default LCID for this page is: 1033
  2. The Date format for the above LCID is: 9/20/2013
  3. The Currency format for the above LCID is: $350.00
  4. The LCID is now changed to: 1036
  5. The Date format for the above LCID is: 20/09/2013
  6. The Currency format for the above LCID is: 350,00 €
  7. The LCID is now changed to: 3079
  8. The Date format for the above LCID is: 20.09.2013
  9. The Currency format for the above LCID is: € 350,00
  10. The LCID is now changed to: 2057
  11. The Date format for the above LCID is: 20/09/2013
  12. The Currency format for the above LCID is: £350.00

返回 SessionID
本例演示 "SessionID" 属性。该属性为每位用户返回一个唯一的 id。这个 id 由服务器生成。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Response.Write(Session.SessionID)
  7. %>
  8.  
  9. </body>
  10. </html>
  1. 619100248

session 的超时
本例演示 "Timeout" 属性。该属性设置并返回 session 的超时时间(分钟)。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%  
  6. response.write("<p>")
  7. response.write("Default Timeout is: " & Session.Timeout & " minutes.")
  8. response.write("</p>")
  9.  
  10. Session.Timeout=30
  11.  
  12. response.write("<p>")
  13. response.write("Timeout is now: " & Session.Timeout & " minutes.")
  14. response.write("</p>")
  15. %>
  16.  
  17. </body>
  18. </html>
  1. Default Timeout is: 20 minutes.
  2. Timeout is now: 30 minutes.

Server 对象

此文件最后被修改的时间是?
探测文件的最后修改时间。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set fs = Server.CreateObject("Scripting.FileSystemObject")
  7. Set rs = fs.GetFile(Server.MapPath("demo_lastmodified.asp"))
  8. modified = rs.DateLastModified
  9. %>
  10. This file was last modified on: <%response.write(modified)
  11. Set rs = Nothing
  12. Set fs = Nothing
  13. %>
  14.  
  15. </body>
  16. </html>

打开并读取某个文本文件
打开文件 "Textfile.txt" 以供读取。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set FS = Server.CreateObject("Scripting.FileSystemObject")
  7. Set RS = FS.OpenTextFile(Server.MapPath("text") & "\TextFile.txt",1)
  8. While not rs.AtEndOfStream
  9.       Response.Write RS.ReadLine
  10.       Response.Write("<br>")
  11. Wend 
  12. %>
  13.  
  14. <p>
  15. <a href="text/textfile.txt"><img src="/images/btn_view_text.gif"></a>
  16. </p>
  17.  
  18. </body>
  19. </html>

自制的点击计数器

  1. <%
  2. Set FS=Server.CreateObject("Scripting.FileSystemObject")
  3. Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 1, False)
  4. fcount=RS.ReadLine
  5. RS.Close
  6.  
  7. fcount=fcount+1
  8.  
  9. 'This code is disabled due to the write access security on our server:
  10. 'Set RS=FS.OpenTextFile(Server.MapPath("counter.txt"), 2, False)
  11. 'RS.Write fcount
  12. 'RS.Close
  13.  
  14. Set RS=Nothing
  15. Set FS=Nothing
  16.  
  17. %>
  18. <!DOCTYPE html>
  19. <html>
  20. <body>
  21. <p>
  22. This page has been visited <%=fcount%>  times.
  23. </p>
  24. </body>
  25. </html>

FileSystem 对象

指定的文件存在吗?
本例演示如何检查某个文件是否存在。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7.  
  8. If (fs.FileExists("c:\winnt\cursors\3dgarro.cur"))=true Then
  9.       Response.Write("File c:\winnt\cursors\3dgarro.cur exists.")
  10. Else
  11.       Response.Write("File c:\winnt\cursors\3dgarro.cur does not exist.")
  12. End If
  13.  
  14. set fs=nothing
  15. %>
  16.  
  17. </body>
  18. </html>

指定的文件夹存在吗?
本例演示如何检查某个文件夹是否存在。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <%
  5. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  6.  
  7. If fs.FolderExists("c:\temp") = true Then
  8.       Response.Write("Folder c:\temp exists.")
  9. Else
  10.       Response.Write("Folder c:\temp does not exist.")
  11. End If
  12.  
  13. set fs=nothing
  14. %>
  15.  
  16. </body>
  17. </html>

指定的驱动器存在吗?
本例演示如何检查某个驱动器是否存在。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <%
  5. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  6.  
  7. if fs.driveexists("c:") = true then
  8.       Response.Write("Drive c: exists.")
  9. Else
  10.       Response.Write("Drive c: does not exist.")
  11. End If
  12.  
  13. Response.write("<br>")
  14.  
  15. if fs.driveexists("g:") = true then
  16.       Response.Write("Drive g: exists.")
  17. Else
  18.       Response.Write("Drive g: does not exist.")
  19. End If
  20.  
  21. set fs=nothing
  22. %>
  23.  
  24. </body>
  25. </html>

取得某个指定驱动器的名称
本例演示如何获取某个指定的驱动器的名称。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7. p=fs.GetDriveName("c:\winnt\cursors\3dgarro.cur")
  8.  
  9. Response.Write("The drive name is: " & p)
  10.  
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

取得某个指定路径的父文件夹的名称
本例演示如何获取某个指定的路径的父文件夹的名称。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7. p=fs.GetParentFolderName("c:\winnt\cursors\3dgarro.cur")
  8.  
  9. Response.Write("The parent folder name of c:\winnt\cursors\3dgarro.cur is: " & p)
  10.  
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

取得文件名
本例演示如何获取指定的路径中的最后一个成分的文件名。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7.  
  8. Response.Write("The file name of the last component is: ")
  9. Response.Write(fs.GetFileName("c:\winnt\cursors\3dgarro.cur"))
  10. set fs=nothing
  11. %>
  12.  
  13.  
  14. </body>
  15. </html>

取得文件扩展名
本例演示如何获取指定的路径中的最后一个成分的文件扩展名。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7.  
  8. Response.Write("The file extension of the file 3dgarro is: ")
  9. Response.Write(fs.GetExtensionName("c:\winnt\cursors\3dgarro.cur"))
  10.  
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

取得文件或文件夹的基名称
本例演示如何获取指定的路径中文件或者文件夹的基名称。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7.  
  8. Response.Write(fs.GetBaseName("c:\winnt\cursors\3dgarro.cur"))
  9. Response.Write("<br>")
  10. Response.Write(fs.GetBaseName("c:\winnt\cursors\"))
  11. Response.Write("<br>")
  12. Response.Write(fs.GetBaseName("c:\winnt\"))
  13.  
  14. set fs=nothing
  15. %>
  16.  
  17. </body>
  18. </html>

TextStream 对象

读取文本文件
本例演示如何从文本文件中读取内容。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is the text in the text file:</p>
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7.  
  8. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  9. Response.Write(f.ReadAll)
  10. f.Close
  11.  
  12. Set f=Nothing
  13. Set fs=Nothing
  14. %>
  15. </body>
  16. </html>

读取文本文件中的一个部分
本例演示如何仅仅读取一个文本流文件的部分内容。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is the first five characters from the text file:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8.  
  9. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  10. Response.Write(f.Read(5))
  11. f.Close
  12.  
  13. Set f=Nothing
  14. Set fs=Nothing
  15. %>
  16.  
  17. </body>
  18. </html>

读取文本文件中的一行
本例演示如何从一个文本流文件中读取一行内容。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is the first line of the text file:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8.  
  9. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  10. Response.Write(f.ReadLine)
  11. f.Close
  12.  
  13. Set f=Nothing
  14. Set fs=Nothing
  15. %>
  16.  
  17. </body>
  18. </html>

读取文本文件的所有行
本例演示如何从文本流文件中读取所有的行。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is all the lines in the text file:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  9.  
  10. do while f.AtEndOfStream = false
  11. Response.Write(f.ReadLine)
  12. Response.Write("<br>")
  13. loop
  14.  
  15. f.Close
  16. Set f=Nothing
  17. Set fs=Nothing
  18. %>
  19.  
  20. </body>
  21. </html>

略过文本文件中的一部分
本例演示如何在读取文本流文件时跳过指定的字符数。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>The first four characters in the text file are skipped:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8.  
  9. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  10. f.Skip(4)
  11. Response.Write(f.ReadAll)
  12. f.Close
  13.  
  14. Set f=Nothing
  15. Set fs=Nothing
  16. %>
  17.  
  18. </body>
  19. </html>

略过文本文件中的一行
本例演示如何在读取文本流文件时跳过一行。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>The first line in the text file is skipped:</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8.  
  9. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  10. f.SkipLine
  11. Response.Write(f.ReadAll)
  12. f.Close
  13.  
  14. Set f=Nothing
  15. Set fs=Nothing
  16. %>
  17.  
  18. </body>
  19. </html>

返回行数
本例演示如何返回在文本流文件中的当前行号。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p>This is all the lines in the text file (with line numbers):</p>
  5.  
  6. <%
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  9.  
  10. do while f.AtEndOfStream = false
  11. Response.Write("Line:" & f.Line & " ")
  12. Response.Write(f.ReadLine)
  13. Response.Write("<br>")
  14. loop
  15.  
  16. f.Close
  17. Set f=Nothing
  18. Set fs=Nothing
  19. %>
  20.  
  21. </body>
  22. </html>

取得列数
本例演示如何取得在文件中当前字符的列号。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  7.  
  8. Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
  9. Response.Write(f.Read(2))
  10. Response.Write("<p>The cursor is now standing in position " & f.Column & " in the text file.</p>")
  11. f.Close
  12.  
  13. Set f=Nothing
  14. Set fs=Nothing
  15. %>
  16.  
  17. </body>
  18. </html>

Drive 对象

取得指定驱动器的总容量
本例演示如何使用 TotalSize 属性来获得指定驱动器的总容量。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Dim fs,d,n
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. Set d=fs.GetDrive("c:")
  9. = "Drive: " & d
  10. = n & "<br>Total size in bytes: " & d.TotalSize
  11. Response.Write(n)
  12. set d=nothing
  13. set fs=nothing
  14. %>
  15.  
  16. </body>
  17. </html>

取得指定驱动器的可用空间数
本例演示如何首先创建一个 FileSystemObject 对象,然后使用 AvailableSpace 属性来获得指定驱动器的可用空间。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Dim fs, d, n
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. Set d=fs.GetDrive("c:")
  9. = "Drive: " & d
  10. = n & "<br>Available Space in bytes: " & d.AvailableSpace
  11. Response.Write(n)
  12. set d=nothing
  13. set fs=nothing
  14. %>
  15.  
  16. </body>
  17. </html>

取得指定驱动器的剩余空间容量
本例演示如何使用 FreeSpace 空间属性来取得指定驱动器的剩余空间。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. Dim fs, d, n
  7. Set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. Set d=fs.GetDrive("c:")
  9. = "Drive: " & d
  10. = n & "<br>Free Space in bytes: " & d.FreeSpace
  11. Response.Write(n)
  12. set d=nothing
  13. set fs=nothing
  14. %>
  15.  
  16. </body>
  17. </html>

取得指定驱动器的驱动器字母
本例演示如何使用 DriveLetter 属性来获得指定驱动器的驱动器字母。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs, d, n
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set d=fs.GetDrive("c:")
  9. Response.Write("The drive letter is: " & d.driveletter)
  10. set d=nothing
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

取得指定驱动器的驱动器类型
本例演示如何使用 DriveType 属性来获得指定驱动器的驱动器类型。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs, d, n
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set d=fs.GetDrive("c:")
  9. Response.Write("The drive type is: " & d.DriveType)
  10. set d=nothing
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

取得指定驱动器的文件系统信息
本例演示如何使用 FileSystem 来取得指定驱动器的文件系统信息。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs, d, n
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set d=fs.GetDrive("c:")
  9. Response.Write("The file system is: " & d.FileSystem)
  10. set d=nothing
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

驱动器是否已就绪?
本例演示如何使用 IsReady 属性来检查指定的驱动器是否已就绪。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs,d,n
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set d=fs.GetDrive("c:")
  9. = "The " & d.DriveLetter
  10. if d.IsReady=true then 
  11.     n = n & " drive is ready."
  12. else
  13.     n = n & " drive is not ready."
  14. end if 
  15. Response.Write(n)
  16. set d=nothing
  17. set fs=nothing
  18. %>
  19.  
  20. </body>
  21. </html>

取得指定驱动器的路径
本例演示如何使用 Path 属性来取得指定驱动器的路径。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs,d
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set d=fs.GetDrive("c:")
  9. Response.Write("The path is " & d.Path)
  10. set d=nothing
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

取得指定驱动器的根文件夹
本例演示如何使用 RootFolder 属性来取得指定驱动器的根文件夹。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs,d
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set d=fs.GetDrive("c:")
  9. Response.Write("The rootfolder is " & d.RootFolder)
  10. set d=nothing
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

取得指定驱动器的序列号
本例演示如何使用 Serialnumber 属性来取得指定驱动器的序列号。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs,d
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set d=fs.GetDrive("c:")
  9. Response.Write("The serialnumber is " & d.SerialNumber)
  10. set d=nothing
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

File 对象

文件最后被修改的时间?
本例演示如何使用 DateLastModified 属性来取得指定文件最后被修改的日期和时间。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs, f
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set f=fs.GetFile(Server.MapPath("testread.txt"))
  9. Response.Write("The file testread.txt was last modified on: " & f.DateLastModified)
  10. set f=nothing
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

文件最后被访问的时间?
此例演示如何使用 DateLastAccessed 属性来取得指定文件最后被访问的日期和时间。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs, f
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set f=fs.GetFile(Server.MapPath("testread.txt"))
  9. Response.Write("The file testread.txt was last accessed on: " & f.DateLastAccessed)
  10. set f=nothing
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

返回指定文件的属性
本例演示如何使用 Attributes 来返回指定文件的属性。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim fs,f
  7. set fs=Server.CreateObject("Scripting.FileSystemObject")
  8. set f=fs.GetFile(Server.MapPath("testread.txt"))
  9. Response.Write("The attributes of the file testread.txt are: " & f.Attributes)
  10. set f=nothing
  11. set fs=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

Dictionary 对象

指定的键存在吗?
本例演示如何创建一个 Dictionary 对象,然后使用 Exists 方法来检查指定的键是否存在。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim d
  7. set d=Server.CreateObject("Scripting.Dictionary")
  8. d.Add "n", "Norway"
  9. d.Add "i", "Italy"
  10. if d.Exists("n")= true then
  11.     Response.Write("Key exists.")
  12. else
  13.     Response.Write("Key does not exist.")
  14. end if
  15. set d=nothing
  16. %>
  17.  
  18. </body>
  19. </html>

返回一个所有项目的数组
本例演示如何使用 Items 方法来返回一个所有项目的数组。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim d,a,i,s
  7. set d=Server.CreateObject("Scripting.Dictionary")
  8. d.Add "n", "Norway"
  9. d.Add "i", "Italy"
  10.  
  11. Response.Write("<p>The values of the items are:</p>")
  12. a=d.Items
  13. for i = 0 To d.Count -1
  14.     s = s & a(i) & "<br>"
  15. next
  16. Response.Write(s)
  17.  
  18. set d=nothing
  19. %>
  20.  
  21. </body>
  22. </html>

返回一个所有键的数组
本例演示如何使用 Keys 方法来返回一个所有键的数组。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim d,a,i,s
  7. set d=Server.CreateObject("Scripting.Dictionary")
  8. d.Add "n", "Norway"
  9. d.Add "i", "Italy"
  10. Response.Write("<p>The value of the keys are:</p>")
  11. a=d.Keys
  12. for i = 0 To d.Count -1
  13.     s = s & a(i) & "<br>"
  14. next
  15. Response.Write(s)
  16. set d=nothing
  17. %>
  18.  
  19. </body>
  20. </html>

返回一个项目的值
本例演示如何使用 Item 属性来返回一个项目的值。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim d
  7. set d=Server.CreateObject("Scripting.Dictionary")
  8. d.Add "n", "Norway"
  9. d.Add "i", "Italy"
  10. Response.Write("The value of the item n is: " & d.item("n"))
  11. set d=nothing
  12. %>
  13.  
  14. </body>
  15. </html>

设置一个键
本例演示如何使用 Key 属性来在 Dictionary 对象中设置一个键。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim d
  7. set d=Server.CreateObject("Scripting.Dictionary")
  8. d.Add "n", "Norway"
  9. d.Add "i", "Italy"
  10. d.Key("i") = "it"
  11. Response.Write("The key i has been set to it, and the value is: " & d.Item("it"))
  12. set d=nothing
  13. %>
  14.  
  15. </body>
  16. </html>

返回键/项目对的数量
本例演示如何使用 Count 属性来返回键/项目对的数量。

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <%
  6. dim d, a, s, i
  7. set d=Server.CreateObject("Scripting.Dictionary")
  8. d.Add "n", "Norway"
  9. d.Add "i", "Italy"
  10. Response.Write("The number of key/item pairs is: " & d.Count)
  11. set d=nothing
  12. %>
  13.  
  14. </body>
  15. </html>