ASP知识讲座四
ASP内置组件
前三讲中,我们主要介绍了ASP提供的四大内置对象:
l Response对象:向浏览器发送信息。
l Request对象:访问从浏览器发送到服务器的信息(如获取表单数据)。
l Session对象:存储、读取特定用户对话信息。
l Application对象:存储、读取所有用户共享的应用程序信息。
此外还有Server对象和ObjectContext对象我们将在以后的实例中学习(提示:其实不经意之间你已经可以利用所学知识编写网上聊天室了)。本讲的内容是使用ASP的ActiveX Server Components(组件)。
一、 Browser Capabilities Component(浏览器能力组件):
我们知道,不同的浏览器也许支持不同的功能,如有些浏览器支持框架,有些不支持。利用这个组件,可以检查浏览器的能力,使你的网页争对不同的浏览器显示不同的页面(如对不支持Frame的浏览器显示不含Frame的网页)。该组件的使用很简单,需注意的是,要正确使用该组件,必须保证Browscap.ini文件是最新的(其实每一个浏览器及其特性都列在这个文件中,自己打开看看就明白了),否则结果可能相去甚远,如Win98第二版所带的IE5.0,在下例中显示为Netscape。这个文件一般位于Web服务器的"\Winnt\System32\InetSrv"下,最新的版本可去http://www.asptracker.com/或http://www.cyscape.com/browscap下载。
例:wuf22.asp
'注意:组件的使用与对象类似,但是组件在使用前必须先创建,而使用内置对象前不必创建。
请稍候......
浏览器类型 | |
浏览器版本 | |
是否支持表格 | |
是否支持ActiveX控件 | |
是否支持JavaApplets | |
是否支持JavaScript | |
是否支持Cookies | |
是否支持Frames | |
操作系统 | |
是否支持VBScript |
注意:在本例中我们也接触了Server对象的CreateObject方法,Server.CreateObject用于创建已经注册到服务器上的ActiveX组件(说明:还有其他方法可以创建组件)。不过别忘了用"Set 对象 = Nothing"来及时释放资源,这应该成为一个习惯。
二、 File Access组件
File Access组件由FileSystemObject对象和TextStream对象组成,使用FileSystemObject对象,可以建立、检索、删除目录及文件,而TextStream对象则提供读写文件的功能。
实例wuf23.asp。强调:只有通过实践才能加深理解,实践和比较程序运行结果是快速掌握编程技巧的最好方法。
' 注意绝对路径: C:\Inetpub\home\asp\wuf23.asp 主页路径: C:\Inetpub\home
Dim Path, File, FSO, CTF, Str, StrHTML, StrNoHTML
'使用 CreateObject 方法创建 FileSystemObject 对象 FSO
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Path = Server.MapPath("test") '返回test的物理目录(绝对路径)
'就本例而言, 下面这句与上面这句返回的Path完全一样
'Path = Server.MapPath("\asp\test\")
Response.Write Path & "
"
If FSO.FolderExists(Path) = false then '判断该文件夹是否存在
FSO.CreateFolder(Path) '新建文件夹
End If
File = Path & "\asptest.txt"
' 写文件操作
If FSO.FileExists(File) = True Then '判断该文件是否存在
'建立 TextStream 对象 CTF
Set CTF = FSO.OpenTextFile(File, 8, False, 0) '打开文件, 详见说明
Else
Set CTF = FSO.CreateTextFile(File,False, False) '新建文件
End If
CTF.Write "
第一个字符串; " '写字符串
CTF.WriteLine "第二个字符串; " '写字符串, 并加上一个换行符
CTF.Write "第三个字符串; "
CTF.Close '注意要关闭文件
' 读文件操作
Set CTF = FSO.OpenTextFile(File, 1,,0)
Do While CTF.AtEndOfStream True '判别是否文件结尾(循环语句)
Str = CTF.ReadLine '(每次)读取一行
StrNoHTML = StrNoHTML & Str & "
" & VbCrLf
StrHTML = StrHTML & Server.HTMLEncode(Str) & "
" & VbCrLf
Loop
Response.Write StrNoHTML
Response.Write StrHTML
CTF.Close
Set CTF = Nothing '释放对象
Set FSO = Nothing
%>
CTF = FSO.OpenTextFile(File, 8, False, 0),括号内第一个参数为文件名;第二个参数为8,表示在原文件后追加内容,若为1表示只读,为2则会重写原文件;第三个参数false表示,若指定文件不存在,也不新建文件,若为True,表示指定文件不存在,则新建该文件;第四个参数0表示以ASCII文件格式打开,若为-2,则表示以原来的格式打开。
CTF = FSO.CreateTextFile(File,False, False),第二个参数false表示不覆盖已有文件,若为True,则表示覆盖(OverWrite)已有文件;第三个参数为False表示文件格式为ASCII,为True表示文件格式为Unicode。
Server对象的MapPath方法将指定的虚拟路径转换为真实的文件路径。MapPath将"/"和"\"字符视为相同。
Server对象的HTMLEncode方法允许你对特定的字符串进行HTML编码,或者说使浏览器中可以正确显示特定的字符。上例中,若未编码,则"
"显示不出来,而是被浏览器作为HTML标记,你可以对比一下运行结果。
实际上,File Access组件对文件、文件夹和驱动器的操作还是比较强大的,也提供了较多的方法,如果需要用到这方面的知识,别忘了使用它。
另外,到现在为止,写一个网页计数器已经是小菜一碟了吧,难怪那么多的网页提供免费计数器。怎么样?自己写一个图形计数器试试看,想怎么作弊就怎么作弊,完全自己说了算,爽呆!(小秘密:我的主页上有实例wuf24.asp)
三、 AD Rotator(广告翻转组件)
现在上网,恐怕最讨厌的是别人主页上的广告条,最喜欢的是自己主页上的广告条,广告条如同垃圾邮件一样,比比皆是,防不胜防。你也可以自己动手制造这样的垃圾,ASP的AD Rotator组件就可使每次打开或者重新加载网页时,随机的显示广告。这个例子包括三部分:
例程wuf25.asp
Dim adr
'创建 AD Rotator 对象
Set adr = Server.CreateObject("MSWC.AdRotator")
adr.Border = 2 '指定图形文件的边框大小
adr.Clickable = True '指示显示的图片是否是一个超链接
adr.TargetFrame = "_blank" '设置超链接是否要指定Frame名称,如: _TOP _NEW _PARENT
'获取将要显示的图片及超链接设置 - 在文件 AdrSet.txt 中设置
Response.Write adr.GetAdvertisement("AdrSet.txt")
%>
AdrSet.txt内容(后面为注释,不是这个文件的内容):
REDIRECT wuf26.asp 点击广告后,转由wuf26.asp来处理
WIDTH 468 广告图片宽度
HEIGHT 60 广告图片高度
* 分隔符
http://www.soyou.com/prog/ad/468x60_1.gif 广告图片所在位置,也可为本地图形文件
http://www.163.com/ 指向链接,若没有超链接,写入一个"-"
网易 文字说明
20 显示该广告的相对权重,即显示频率
http://fp.cache.imgis.com/images/Ad173962St1Sz1Sq1Id2.gif
http://www.sina.com.cn/
新浪网
30
http://61.139.77.73/images/canon.gif 也可以使用本地图片,如../images/flag.gif
http://www.canon.com.cn/
佳能
50
本例中一共有三个图片(图片大小468X60)及链接,每个链接的描述占四行,实际使用时,你可如法炮制,增加更多的图片。
URL = Request.QueryString("url")
Response.Redirect(URL)
%>
wuf26.asp是一个最简单的处理程序,你可根据实际需要在这里加入更多的代码。
运行一下,原来这个组件的使用也很简单,你要做的就是得到自己的AdrSet.txt文件。利用这个组件,你甚至可以设计一个现在已非常时髦的广告交换主页。
四、 Content Linking组件
显然这个组件与链接有关系,如果想马上知道这个组件的具体用途,恐怕还操之过急,不妨先引用一个经典的例子:假设在网上阅读一本书,你对以下这些链接一定不会陌生:第1章、第2章、…、上一章、下一章(或前一页、后一页)等等。我们现在要做的就是如何在这些链接之间方便快速地设置跳转。
首先建一个链接列表文本文件,如urllist.txt
wuf23.asp 第1章:文件操作(File Access组件)
wuf28.asp 第2章:Content Linking组件使用示例
wuf22.asp 第3章:浏览器能力组件
链接url地址和描述之间用 Tab 键分隔。下面wuf27.asp用来列出urllist.txt中的所有链接。
目录列表: 注意核心链接是第2章, 你一定要点击它
-
" & Dscr & "" & vbcrlf
Next
%>
Dim NextLink, Count
'建立 Content Linking 组件
Set NextLink = Server.CreateObject("MSWC.NextLink")
'获取文件 urllist.txt 中链接数目
Count = NextLink.GetListCount("urllist.txt")
Dim url, Dscr, I
For I = 1 To Count
url = NextLink.GetNthURL ("urllist.txt", I) '取得超链接
Dscr = NextLink.GetNthDescription ("urllist.txt", I) '取得文字描述
Response.Write "
然后,以wuf28.asp为例说明如何自动实现上一章和下一章跳转。
这里是第 2 章的正文............
这里最后一句加上去就可以实现自动跳转,核心在wuf29.asp中。
Dim NextLink, rank
Set NextLink = Server.CreateObject ("MSWC.NextLink")
'当前的链接在 urllist.txt 中位于第几个
rank = NextLink.GetListIndex ("urllist.txt")
Response.Write "
"
If (rank > 1) Then 'rank = 1 不存在前一页
Response.Write "|上一章|"
End If
If (rank Response.Write "|下一章|"
End If
%>
运行这个例子后,你马上能真正理解这个组件的作用,简而言之,就是不需要在每页都写一个"上一章"、"下一章",完全通过wuf29.asp一下搞定,是不是很方便?!不然你要是手工修改链接的话,不是太麻烦了几点吗?
现在你应该明白了,网上大量的免费计数器、免费留言板、免费聊天室、广告交换网等等……,其原理都不过如此,大可不必崇拜。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open
