When you write server-side applications using ASP, you must rely on ActiveX components to powerful web applications' functions, such as: you need to connect to the database, operate the database online, etc.
In the previous two articles, the author introduced the usage methods of AD Rotator, Database Access and other components. Today we will take a look at some other commonly used components of ASP.
1. Browser Capabilities Components are well known, and not all browsers support all aspects of today's Internet technology. There are some features that some browsers support but others do not, such as: ActiveX controls, image streams, dynamic HTML, Flash, and scripting programs. Using the Browser Capabilities component of ASP, you can design smart web pages to present content in a format that suits browser performance. The Browser Capabilities component is able to create a BrowserType object that provides user scripts with functional descriptions of the client web browser. The reason why this component can recognize the version of the client browser is mainly because when the client browser sends a page request to the server, it will automatically send a User Agent HTTP title, which is an ASCII character that declares the browser and its version. string. The Browser Capabilities component maps the User Agent to the browser indicated in the file Browscap.ini and identifies the client browser through the properties of the BrowserType object. If the object cannot find an item matching the title in the browser.ini file, the default browser properties will be used. If the object has neither a match found and the default browser settings are specified in the browser.ini file, it sets each property to the string UNKNOWN. By default, the browser.ini file is stored in the WINDOWS/SYSTEM/INERSRV (if 95/98+PWS4) or NT/SYSTEM32/INERSRV (if NT) directory, you can edit this text file yourself to add Modify the file by its own attributes or based on the latest released browser version update file. Please see the following checkCookie() process, use the cookie attribute of the BrowserCap object to determine whether the client browser supports cookies and return information:
< %
Sub checkCookie()
Set BrowserCap=Server.CreateObject(MSWC.BrowserType)
if BrowserCap.Cookie=True then
response.write Your browser supports cookies!
else
response.write Sorry, the browser you are using does not support cookies!
end if
end Sub
%>
For more information about the Browser Capabilities component, see Dynamic Website Design Eighteen Martial Arts-ASP (2).
2. File Access Component If you are old enough in the network, you must have seen the CGI guestbook in the dinosaur era, which is the earliest prototype of the WEB guestbook. At that time, it was very difficult to connect to the server backend database in Internet-based WEB applications, so the historical information in the guestbook was not stored in the backend database as it is today. So where are these data stored? The answer is a text file. The CGI program can write the information received from the client into a text file stored on the server. The file can be an HTML file or a TXT file, so that programmers can not connect through the same database. Customer information can be saved, but writing such CGI programs is very cumbersome. Here is a simplest sample of such programs:
#!/usr/local/bin/perl
# Perl Location one your server
print Content-type: text/plain/n/n;
if($ENV{'REQUEST_METHOD'}eqPOST){
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
}elsif($ENV{'REQUEST_METHOD'}eqGET){
$buffer=$ENV{'QUERY_STIRNG'};
}
@pairs=split(/&/,$buffer);
foreach $pair(@pairs){
($name,$value)=split(/=/,$pair);
$value=~tr/+//;
$value=~s/%([af AF 0-9][af Af 0-9])/pack(C,hex($1))/eg;
$FORM{$name}=$value;}
$file_name=guestbook.txt;#File name
#Specific content
open(FILE,>>$file_name)||die error in opening the file;
print FILE Record time: $date /n/n;
print FILE Name:$FORM{'name'};
print FILE unit: $FORM{'company'}/n;
print FILE Phone:$FORM{'phone'}
print FILE Address: $FORM{'address'}/n;
print FILE Zip Code: $FORM{'zip'}
print FILE Email address: $FORM{'email'}/n;
print FILE Return comment: $FORM{'content'}
close (FILE)
You can feel that compared with ASP, this type of CGI program is poorer in readability and operability. Then you must want to ask whether ASP can also write files directly on the server? The answer is of course yes. But smart friends may think that since the connection between ASP and WEB database is so convenient, why do we need to write customer information in text files? Isn’t this function of ASP extravagant? Indeed, for those common WEB applications such as guestbooks and BBS, we cannot replace the database with writing text files in terms of program execution efficiency or ease of use, but in some WEB applications Writing text files in the field is both a standardized and a relatively convenient method compared to a database. If you are familiar with NT, you must know that NT has a very powerful security mechanism, which can automatically save almost all server operations and connection information in a file with the suffix name .log. In fact, this technology is completely different. It can be used on WEB to record some customer login information. The following program uses the feature of ASP reading and writing text files to create a function that automatically records each user's speech records in a WEB BBS program.
< %
Set fs = CreateObject(Scripting.FileSystemObject)
ForReading = 1
'Open the file in read-only mode. This file cannot be written.
ForAppending = 8
'Open the file and write it at the end of the file.
TristateUseDefault = -2
TristateTrue = -1
TristateFalse = 0
'------------------------------
servermap=server.MapPath(/bbs/log/)
'Mapping system physical path
temp=servermap&/&year(date)&month(date)&/
'Get the physical path and time of the system and use this as the physical path stored in the log file
if Not fs.FolderExists(temp) then
fs.CreateFolder(temp)
end if
'Check whether the folder exists, otherwise it will be automatically created
dim syslog
dim tempname
tempname=date
syslog=temp&tempname&.log
'The file name is e:/bbs/log/month/month day.log
li=user&&&Now&&Request.ServerVariables(REMOTE_ADDR)&&tempfile&&&letter&&title
'Log file record format is: username & sending time & user ip& file path & message area & letter title
if fs.FileExists(syslog) then
Set ss = fs.OpenTextFile(syslog,ForAppending,true)
else
set ss = fs.CreateTextFile(syslog,ForWriting,false)
end if
'Check whether the log file exists. If it exists, add the file contents, otherwise write the file directly
ss.WriteLine(l
i)
ss.Close
'-----------------------------------------------------------------------------------------------------------------------------
%>
If you don’t fully understand the above program, please listen to the author’s instructions. The File Access component provides methods and properties that can be used to access a computer's file system. We can use the File Access component to create a FileSystemObject object. The first sentence of the above program is to use the File Access component to create an object instance named fs. After the object is created, you can access the file through it. The object has no properties. Its only meaning is to create, open, or read and write text files. There are two most commonly used methods for the FileSystemObject object, one is used to create files, and the other is used to open and read and write text files. The CreateTextFile method obtains the file name you specified and creates the file. It returns a TextStream object. You can use this object to operate the file after the file is created. The syntax of the CreateTextFile method is as follows:
Set objTextStream=FileSystemObject.CreateTextFile(Filename,[Overwrite],[Unicode])
The author will explain the parameters of the CreateTextFile method below
1. Filename A string containing the file path name can be the full path name of the file, including the drive name and directory name, or it can be just a file name. If only the file name is included, the file will be created in the root directory of the site.
2. Overwrite boolean quantity. When set to False, it can prevent the FileSystemObject object from deleting existing files when creating a new file. This parameter is optional. If the system does not have an assignment defaults to true, existing files with the same file name will be Deleted.
3. Unicode optional parameters. Boolean values indicate whether to create a file in Unicode or ASCII file format. This value is True if a file is created in Unicode file format, and False if a file is created in ASCII file format. If this section is omitted, an ASCII file is assumed to be created.
In the previous program, we use set ss=fs.CreateTextFile(syslog,ForWriting,false) to create a file and write a file when the log file does not exist. Here, ForWriting means writing a file.
Unlike the CreateTextFile method, the OpenTextFile method is used to obtain the file name you specified and open the file. Using the parameters it contains, we can perform various operations on the file. Like the CreateTextFile method, the OpenTextFile method returns a TextStream object, so that You can operate the file after it is opened. The syntax of the OpenTextFile method is as follows:
Set objTextStream=FileSystemObject.OpenTextFile(Filename,[IOmode],[Create],[Format])
The parameters are as follows:
1. Filename is the necessary variable, same as the filename of CreateTextFile
2. IOmode optional constant, with the value as one of the following two constants ForReading or ForAppending. If mode is 1, the file is opened read-only, and if it is 8, the file is opened appended.
3. Create optional boolean quantity, specifying what operation is done if the file you want to open does not exist. If its value is True, an empty file will be automatically created when the file does not exist. If False, an error message will be generated when the file is not found. The default value is False. It is recommended to set it to True to avoid checking for errors when opening the file.
4. Format optional value, you can select three Tristate values to specify the format of the file, respectively. -2, -1, and 0 correspond to the system default, unicode and ASCII, respectively.
After opening or creating a text file, you get a TextStream object, which has a cursor, just like a cursor in a word processing program, indicating where the character to be typed will appear, and it also Indicate the location of the character you want to read. You cannot create a TextStream object through CreatObject. The only way to get a TextStream object is to use the FileSystemObject object to open an existing text file or create a new file as described previously.
The properties and methods of the TextStream object are listed below
TextStream.A
tEndOfLine read-only boolean quantity, when the cursor is at the end of the current line, its value is true, otherwise it is false
TextStream.AtEndOfStream Read-only boolean quantity, if the cursor is at the end of the stream, its value is true, otherwise it is false
TextStream.Column Read-only integer, counting the number of characters from the beginning of the line to the current cursor position
TextStream.Line Read-only integer indicating the line number of the line in the entire file where the cursor is located
TextStream.close() Closes the stream and the corresponding text file
TextStream.read(Num) Specifies that a certain number of characters are read from the text file starting from the current position of the cursor.
TextStream.readall() Reads the entire stream into a string
TextStream.readline() Reads a whole line of characters into a string
TextStream.write(text) Write a string to the stream
TextStream.writeline() Writes a text string to the stream
TextStream.skip(Num) In a stream, move the cursor position by a certain number of string lengths
TextStream.skiplines() In a stream, move the cursor to a certain number of rows
TextStream.writeblank Writes a certain number of empty lines to the stream
lines(num)
I believe everyone can now feel the powerful functions of the ASP File Access component. In fact, it can not only write some log files, but also automatically update your website effortlessly. You just need to put text in fixed format. The file is transferred to the remote server, read the file through the File Access component, and automatically generate a brand new HTML page, without having to work hard to update the HTML files one by one. If you are interested, you can use the File Access component of ASP to write your own fully automatic HTML generator to fully enjoy the advanced and easy feeling of maintaining the website.