In the first three lectures on ASP built-in components, we mainly introduced four built-in objects provided by ASP: lResponse object: sending information to the browser. LRequest object: access information sent from a browser to the server (for example, obtaining form data ). LSession object: stores and reads the conversation information of a specific user. LApplication object: stores and reads applications shared by all users
ASP built-in components
In the first three lectures, we mainly introduced four built-in objects provided by ASP:
L Response object: send information to the browser.
L Request object: access the information sent from the browser to the server (for example, obtaining form data ).
L Session object: stores and reads the conversation information of a specific user.
L Application object: stores and reads Application information shared by all users.
In addition, we will learn Server objects and ObjectContext objects in future instances (note: you can use your knowledge to compile online chat rooms ). This document describes how to use ASP ActiveX Server Components ).
I. Browser Capabilities Component (Browser capability Component ):
We know that different browsers may support different functions, for example, some browsers support frameworks, and some do not. By using this component, you can check the browser's capabilities to make your webpage compete for displaying different pages for different browsers (for example, displaying webpages without Frame in browsers that do not support Frame ). This component is easy to use. you must ensure that Browscap is used correctly. the INI file is the latest (in fact, every browser and its features are listed in this file and you can see it by yourself). Otherwise, the results may be far from each other, for example, IE5.0 in the second version of Win98, in the following example, Netscape is displayed. This file is generally located under \ Winnt \ System32 \ InetSrv on the Web server. The latest version can be downloaded at http://www.asptracker.com/or http://www.cyscape.com/browscap.
Example: wuf22.asp
'Note: the usage of a component is similar to that of an object. However, a component must be created before use, but not before use of a built-in object.
<% Set BrowsCap = Server. CreateObject ("MSWC. BrowserType") %>
Please wait ......
Browser type<% = BrowsCap. Browser %>
Browser version<% = BrowsCap. version %>
Table supported?<% = BrowsCap. tables %>
ActiveX control supported?<% = BrowsCap. activexcontrols %>
JavaApplets supported?<% = BrowsCap. javaapplets %>
Support for JavaScript?<% = BrowsCap. javascript %>
Cookies supported?<% = BrowsCap. Cookies %>
Frames supported?<% = BrowsCap. Frames %>
Operating system<% = BrowsCap. Platform %>
Support for VBScript<% = BrowsCap. vbscript %>
<% Set BrowsCap = Nothing %>
Note: In this example, we also use the CreateObject method of the Server object. Server. CreateObject is used to create ActiveX components registered on the Server (note: There are other ways to create components ). But don't forget to use "Set object = Nothing" to release resources in time, which should be a habit.
II. File Access component
The File Access component consists of FileSystemObject objects and TextStream objects. using FileSystemObject objects, you can create, retrieve, and delete directories and files. while TextStream objects provide the ability to read and write files.
Instance wuf23.asp. Emphasize: only through practice can we deepen our understanding. practice and comparison of program running results are the best way to quickly master programming skills.
<% @ Language = VBScript %>
<% Option Explicit
'Note absolute path: C: \ Inetpub \ home \ asp \ wuf23.asp homepage path: C: \ Inetpub \ home
Dim Path, File, FSO, CTF, Str, StrHTML, StrNoHTML
'Use the CreateObject method to create the FileSystemObject object FSO
Set FSO = Server. CreateObject ("Scripting. FileSystemObject ")
Path = Server. MapPath ("test") 'returns the physical Directory (absolute Path) of test)
'In this example, the following sentence is exactly the same as the Path returned by the above sentence
'Path = Server. MapPath ("\ asp \ test \")
Response. Write Path &"
"
If FSO. FolderExists (Path) = false then ', check whether the folder exists.
FSO. CreateFolder (Path) 'create a folder
End If
File = Path & "\ asptest.txt"
'File write operations
If FSO. FileExists (File) = True Then ', check whether the File exists.
'Create TextStream object CTF
Set CTF = FSO. OpenTextFile (File, 8, False, 0) 'open the File. for details, see the description.
Else
Set CTF = FSO. CreateTextFile (File, False, False) 'create a File
End If
CTF. Write"
The first string; "'write string
CTF. WriteLine "second string;" 'write string with a line break
CTF. Write "the third string ;"
CTF. Close 'Be sure to Close the file
'File read operations
Set CTF = FSO. OpenTextFile (File, 1, 0)
Do While CTF. AtEndOfStream <> True ': determines whether the end of a file (loop statement)
Str = CTF. ReadLine '(each time) reads a row
StrNoHTML = StrNoHTML & Str &"
"& VbCrLf
StrHTML = StrHTML & Server. HTMLEncode (Str )&"
"& VbCrLf
Loop
Response. Write StrNoHTML
Response. Write StrHTML
CTF. Close
Set CTF = Nothing 'release the object
Set FSO = Nothing
%>
CTF = FSO. openTextFile (File, 8, False, 0). The first parameter in the brackets is the File name. The second parameter is 8, indicating the content appended to the original File. if it is 1, it indicates read-only, if this parameter is set to 2, the original file is overwritten. if the specified file does not exist, no new file is created. If this parameter is set to True, the specified file does not exist; the fourth parameter 0 indicates opening in ASCII file format. if it is-2, it indicates opening in the original format.
CTF = FSO. createTextFile (File, False, False). The second parameter "false" indicates that existing files are not overwritten. if it is True, the existing files are overwritten; the third parameter "False" indicates that the file format is ASCII, and "True" indicates that the file format is Unicode.
The MapPath method of the Server object converts the specified virtual path to the actual file path. MapPath treats the "/" and "\" characters as the same.
The HTMLEncode method of the Server object allows you to encode specific strings in HTML, or enable the browser to correctly display specific characters. In the preceding example, if the code is not encoded"
"Not displayed, but marked by the browser as HTML. you can compare the running result.
In fact, the File Access component is powerful in operations on files, folders, and drives and provides many methods. if you need to use this knowledge, do not forget to use it.
In addition, up to now, writing a web page counter is already a piece of cake. no wonder that so many Web pages provide free counters. How is it? Try writing a graphic counter by yourself. if you want to cheat, you can cheat it. you have the final say, it's cool! (Tips: my home page has an instance wuf24.asp)
AD Rotator)
Today, I am afraid the most annoying thing about surfing the Internet is the advertisements on other people's homepages. the most popular advertisements are those on their homepages. the advertisements are like spam, so they are everywhere. You can also create such garbage by yourself. The asp ad Rotator component allows users to randomly display advertisements each time they open or reload a webpage. This example includes three parts:
Routine wuf25.asp
<% @ Language = VBScript %>
<% Option Explicit
Dim adr
'Create an AD Rotator object
Set adr = Server. CreateObject ("MSWC. AdRotator ")
Adr. Border = 2' specifies the Border size of the graphic file.
Adr. Clickable = true' indicates whether the displayed image is a hyperlink.
Adr. TargetFrame = "_ blank" 'specifies whether to specify the Frame name for the hyperlink, for example, _ TOP _ NEW _ PARENT.
'Get the image to be displayed and hyperlink settings-set in file AdrSet.txt
Response. Write adr. GetAdvertisement ("AdrSet.txt ")
%>
Content of AdrSet.txt (followed by a comment, not the content of this file ):
REDIRECT wuf26.asp click the advertisement and transfer it to wuf26.asp for processing.
WIDTH 468 advertising image WIDTH
HEIGHT 60 advertising image HEIGHT
* Separator
Http://www.soyou.com/prog/ad/468x60_1.gif ad image location, can also be a local graphics file
Http://www.163.com/point to link, write a "-" If there is no hyperlink "-"
NetEase text description
20. display the relative weight of the advertisement, that is, the display frequency.
Http://fp.cache.imgis.com/images/Ad173962St1Sz1Sq1Id2.gif
Http://www.sina.com.cn/
Sina
30
Http: // 61.139.77.73/images/canon.gif can also use local images, such as ../images/flag.gif
Http://www.canon.com.cn/
Canon
50
In this example, there are three images (the image size is 468X60) and links, each of which occupies four lines. in actual use, you can add more images.
<% 'Wuf26. asp
URL = Request. QueryString ("url ")
Response. Redirect (URL)
%>
Wuf26.asp is the simplest processing program. you can add more code here as needed.
When running the program, the original component is easy to use. you need to compile the program's adrset.txt file. With this component, you can even design a very fashionable Ad Exchange homepage.
IV. Content Linking component
Obviously, this component has something to do with the link. if you want to know the specific purpose of this component right away, I am afraid it is still in a hurry. I would like to cite a classic example: if you read a book online, you will not be unfamiliar with the following links: Chapter 1st, chapter 2nd ,... , The previous chapter, the next chapter (or the previous and next pages), and so on. What we need to do now is how to easily and quickly set the jump between these links.
First, create a chain of example files, such as urllist.txt
Chapter 1st wuf23.asp: File Access component)
Wuf28.asp chapter 2nd: Content Linking component usage example
Chapter 2 wuf22.asp: browser capability components
The url and description are separated by the Tab key. The following wuf27.aspused to list all links in urllist.txt.
<% @ LANGUAGE = VBScript %>
<% Option Explicit %>
Content Linking component usage
Directory List: note that the core link is chapter 2nd. you must click it.
<%
Dim NextLink, Count
'Establish the Content Linking component
Set NextLink = Server. CreateObject ("MSWC. NextLink ")
'Obtain the number of links in the file urllist.txt.
Count = NextLink. GetListCount ("urllist.txt ")
Dim url, Dscr, I
For I = 1 To Count
Url = NextLink. GetNthURL ("urllist.txt", I) 'to obtain the hyperlink
Dscr = NextLink. GetNthDescription ("urllist.txt", I) 'gets the text description
Response. Write"
- "& Dscr &" & vbcrlf
Next
%>
Take wuf28.asp as an example to explain how to automatically jump to the previous chapter and the next chapter.
<% @ LANGUAGE = VBScript %>
<% Option Explicit %>
Note that
Here is the body of Chapter 2 ............
<% 'Every file contains the following sentence, and the automatic link is realized. %>
The last sentence can be added to achieve automatic jump. The core is wuf29.asp.
<%
Dim NextLink, rank
Set NextLink = Server. CreateObject ("MSWC. NextLink ")
'The current link is located in the nth part of urllist.txt.
Rank = NextLink. GetListIndex ("urllist.txt ")
Response. Write ""
If (rank> 1) then' rank = 1 does not exist in the previous page
Response. Write "| previous chapter |"
End If
If (rank <NextLink. GetListCount ("urllist.txt") then' rank is at the end, there is no Next page
Response. Write "| next chapter |"
End If
%>
After running this example, you can really understand the role of this component. In short, you do not need to write a "previous chapter" or "next chapter" on each page ", it's easy to use wuf29.asp ?! Otherwise, if you manually modify the link, isn't it too troublesome?
Now you should understand that there are a lot of free counters, free message boards, free chat rooms, and ad exchange networks on the Internet ......, The principle is not the same, so you don't have to worship it.