search
HomeBackend DevelopmentPHP TutorialASP知识讲座四_php基础


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中的所有链接。


Content Linking组件使用
 

目录列表: 注意核心链接是第2章, 你一定要点击它



    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 "
  • " & Dscr & "" & vbcrlf
    Next
    %> 

然后,以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一下搞定,是不是很方便?!不然你要是手工修改链接的话,不是太麻烦了几点吗?
现在你应该明白了,网上大量的免费计数器、免费留言板、免费聊天室、广告交换网等等……,其原理都不过如此,大可不必崇拜。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool