ASP Lecture 4: ASP Built-in Components
Author:Eve Cole
Update Time:2009-05-30 19:58:14
In the first three lectures, we mainly introduced the four built-in objects provided by ASP:
Response object: Send information to the browser.
Request object: access information sent from the browser to the server (such as obtaining form data).
Session object: stores and reads specific user conversation information.
Application object: stores and reads application information shared by all users.
In addition, there are Server objects and ObjectContext objects that we will learn in future examples (hint: in fact, you can already use the knowledge you have learned to write an online chat room without realizing it). The content of this lecture is the use of ASP's ActiveX Server Components (components).
1. Browser Capabilities Component:
We know that different browsers may support different functions. For example, some browsers support frames and some do not. Using this component, you can check the browser's capabilities so that your web page can display different pages for different browsers (such as displaying web pages without Frame for browsers that do not support Frame). The use of this component is very simple. It should be noted that to use this component correctly, you must ensure that the Browscap.ini file is up to date (in fact, every browser and its features are listed in this file. You will understand after opening it yourself. ), otherwise the results may be very different. For example, IE5.0 included in the second version of Win98 is displayed as Netscape in the following example. This file is generally located under "WinntSystem32InetSrv" on the Web server. The latest version can be downloaded from http://www.asptracker.com/ or http://www.cyscape.com/browscap.
Example: wuf22.asp
<html>
<BODY>
'Note: The use of components is similar to that of objects, but components must be created before use, and there is no need to create before using built-in objects.
<%Set BrowsCap=Server.CreateObject("MSWC.BrowserType")%>
Please wait......
<P>
<TABLE BORDER=1 CELLPADDING=10>
<TR><TD>Browser type</TD><TD><%=BrowsCap.Browser%></TD></TR>
<TR><TD>Browser version</TD><TD><%=BrowsCap.version%></TD></TR>
<TR><TD>Whether tables are supported</TD><TD><%=BrowsCap.tables%></TD></TR>
<TR><TD>Whether ActiveX controls are supported</TD><TD><%=BrowsCap.activexcontrols%></TD></TR>
<TR><TD>Whether JavaApplets are supported</TD><TD><%=BrowsCap.javaapplets%></TD></TR>
<TR><TD>Whether JavaScript is supported</TD><TD><%=BrowsCap.javascript%></TD></TR>
<TR><TD>Whether Cookies are supported</TD><TD><%=BrowsCap.Cookies%></TD></TR>
<TR><TD>Whether Frames are supported</TD><TD><%=BrowsCap.Frames%></TD></TR>
<TR><TD>Operating system</TD><TD><%=BrowsCap.Platform%></TD></TR>
<TR><TD>Whether VBScript is supported</TD><TD><%=BrowsCap.vbscript%></TD></TR>
</TABLE>
<%Set BrowsCap = Nothing%>
</BODY>
</HTML>
Note: In this example we also touched the CreateObject method of the Server object. Server.CreateObject is used to create ActiveX components that have been registered on the server (Note: There are other methods to create components). But don't forget to use "Set Object = Nothing" to release resources in time. This should become a habit.
2. File Access component
The File Access component consists of a FileSystemObject object and a TextStream object. Using the FileSystemObject object, you can create, retrieve, and delete directories and files, while the TextStream object provides the function of reading and writing files.
Example wuf23.asp. Emphasis: Only through practice can understanding be deepened. Practicing and comparing program running results is the best way to quickly master programming skills.
<%@ Language=VBScript %>
<% Option Explicit
' Note the absolute path: C:Inetpubhomeaspwuf23.asp Home page path: C:Inetpubhome
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") 'Return the physical directory of test (absolute path)
'As far as this example is concerned, the following sentence returns exactly the same Path as the above sentence
'Path = Server.MapPath("asptest")
Response.Write Path & "<Br>"
If FSO.FolderExists(Path) = false then 'Determine whether the folder exists
FSO.CreateFolder(Path) 'Create a new folder
End If
File = Path & "asptest.txt"
'Write file operation
If FSO.FileExists(File) = True Then 'Determine whether the file exists
'Create TextStream object CTF
Set CTF = FSO.OpenTextFile(File, 8, False, 0) 'Open the file, see the description for details
Else
Set CTF = FSO.CreateTextFile(File,False, False) 'New file
End If
CTF.Write "<P>The first string; " 'Write string
CTF.WriteLine "Second string; " 'Write the string and add a newline character
CTF.Write "The third string; "
CTF.Close 'Note to close the file
'Read file operation
Set CTF = FSO.OpenTextFile(File, 1,,0)
Do While CTF.AtEndOfStream <> True 'Determine whether the file ends (loop statement)
Str = CTF.ReadLine 'Read one line (each time)
StrNoHTML = StrNoHTML & Str & "<BR>" & VbCrLf
StrHTML = StrHTML & Server.HTMLEncode(Str) & "<BR>" & 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, which means appending the content after the original file. If it is 1, it means read-only, and if it is 2, it will Rewrite the original file; the third parameter false means that if the specified file does not exist, the file will not be created; if it is True, it means that the specified file does not exist, then the file will be created; the fourth parameter 0 means that it will be opened in ASCII file format. If it is -2, it means opening in the original format.
CTF = FSO.CreateTextFile(File,False, False), the second parameter false means not to overwrite the existing file, if it is True, it means overwrite (OverWrite) the existing file; the third parameter false means the file format is ASCII , being True indicates that the file format is Unicode.
The MapPath method of the Server object converts the specified virtual path into a real file path. MapPath treats "/" and "" characters the same.
The HTMLEncode method of the Server object allows you to HTML-encode a specific string, or to enable the browser to display specific characters correctly. In the above example, if it is not encoded, "<P>" will not be displayed, but will be treated as an HTML tag by the browser. You can compare the running results.
In fact, the File Access component is relatively powerful in operating files, folders and drives, and it also provides more methods. If you need to use this knowledge, don't forget to use it.
In addition, by now, writing a web page counter is a piece of cake. No wonder so many web pages provide free counters. How about it? Try writing a graphical counter yourself. You can cheat as much as you want. You have the final say. It’s so cool! (Little secret: I have an instance wuf24.asp on my homepage)
3. AD Rotator (advertising flipping component)
Nowadays, when surfing the Internet, what I hate most are the advertising banners on other people's homepages, and what I like most are the advertising banners on my own homepage. Advertising banners are everywhere like spam and are hard to guard against. You can also create such garbage yourself. ASP's AD Rotator component can randomly display ads every time you open or reload a web page. This example consists of three parts:
Routine wuf25.asp
<%@ Language=VBScript %>
<% Option Explicit
Dim adr
'Create AD Rotator object
Set adr = Server.CreateObject("MSWC.AdRotator")
adr.Border = 2 'Specify the border size of the graphics file
adr.Clickable = True 'Indicates whether the displayed image is a hyperlink
adr.TargetFrame = "_blank" 'Set the hyperlink whether to specify the Frame name, such as: _TOP _NEW _PARENT
'Get the image and hyperlink settings to be displayed - set in the file AdrSet.txt
Response.Write adr.GetAdvertisement("AdrSet.txt")
%>
Contents of AdrSet.txt (followed by comments, not the content of this file):
REDIRECT wuf26.asp After clicking on the advertisement, it will be processed by wuf26.asp
WIDTH 468 Ad image width
HEIGHT 60 Advertising image height
* separator
The location of the advertising image, which can also be a local graphic file
http://www.163.com/ points to the link. If there is no hyperlink, write a "-"
NetEase text description
20 shows the relative weight of the advertisement, that is, the frequency of display
http://www.sina.com.cn/
sina.com
30
http://www.canon.com.cn/
Canon
50
In this example, there are three pictures (picture size 468X60) and links. The description of each link occupies four lines. In actual use, you can follow the same method and add more pictures.
<% 'wuf26.asp
URL = Request.QueryString("url")
Response.Redirect(URL)
%>
wuf26.asp is the simplest processing program, you can add more code here according to actual needs.
Run it and it turns out that the use of this component is also very simple. All you have to do is get your own AdrSet.txt file. Using this component, you can even design an ad exchange homepage that is now very trendy.
4. Content Linking component Obviously this component is related to links. If you want to know the specific use of this component right away, I am afraid it is too hasty. You might as well quote a classic example first: Suppose you are reading a book on the Internet, you must not be familiar with the following links. Will be unfamiliar: Chapter 1, Chapter 2,..., previous chapter, next chapter (or previous page, next page), etc. What we have to do now is how to set up jumps between these links easily and quickly.
First create a link list text file, such as urllist.txt
wuf23.asp Chapter 1: File Operations (File Access Component)
wuf28.asp Chapter 2: Content Linking component usage example
wuf22.asp Chapter 3: Browser Capability Component Link URL address and description are separated by the Tab key. The following wuf27.asp is used to list all links in urllist.txt.
<% @LANGUAGE = VBScript %>
<% Option Explicit %>
<html><head><title>Content Linking component usage</title></head>
<body>
<h2>Table of Contents: Note that the core link is Chapter 2, you must click on it</h2>
<ul>
<%
Dim NextLink, Count
'Create Content Linking component
Set NextLink = Server.CreateObject("MSWC.NextLink")
'Get 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) 'Get hyperlink
Dscr = NextLink.GetNthDescription ("urllist.txt", I) 'Get text description
Response.Write "<li><a href = """ & url & """>" & Dscr & "</a>" & vbcrlf
Next
%>
</ul></body></html>
Then, take wuf28.asp as an example to illustrate how to automatically jump to the previous chapter and the next chapter.
<% @LANGUAGE = VBScript %>
<% Option Explicit %>
<html><head><title>Pay attention to this link</title></head>
<body>
<p>Here is the text of Chapter 2......</p>
<% 'Each file contains the following sentence to achieve automatic linking%>
<!--#include file="wuf29.asp"-->
</body></html>
Adding the last sentence here can realize automatic jump. The core is in wuf29.asp.
<%
Dim NextLink, rank
Set NextLink = Server.CreateObject ("MSWC.NextLink")
'What is the current link in urllist.txt?
rank = NextLink.GetListIndex ("urllist.txt")
Response.Write "<hr>"
If (rank > 1) Then 'rank = 1 there is no previous page
Response.Write "|<a href=""" & NextLink.GetPreviousURL("urllist.txt") & """>Previous Chapter</a>|"
End If
If (rank < NextLink.GetListCount("urllist.txt")) Then 'rank is at the end, then there is no next page
Response.Write "|<a href=""" & NextLink.GetNextURL("urllist.txt") & """>Next Chapter</a>|"
End If
%>
After running this example, you will immediately be able to truly understand the role of this component. In short, there is no need to write "previous chapter" and "next chapter" on each page. It can be done completely through wuf29.asp. Not very convenient? ! Otherwise, wouldn't it be too troublesome if you modify the link manually?
Now you should understand that there are a large number of free counters, free message boards, free chat rooms, advertising exchange networks, etc. on the Internet. Their principles are nothing more than this, so there is no need to worship them.