When you use ASP to write server-side applications, you must rely on ActiveX components to powerful WEB application functions, such as: you need to connect to the database, online operation of the database and so on.
In two articles, the author introduces the use of AD rotator, Database access and other components, and today we will take a look at some other ASP common components.
I. BROWSER capabilities components It is well known that not all browsers support every aspect of today's Internet technology. There are some features that some browsers support while others do not, such as: ActiveX controls, image streams, dynamic HTML, Flash, and scripting programs. With ASP's Browser capabilities component, you can design "smart" WEB pages to render content in a format that is appropriate for browser performance. The Browser capabilities component is able to create a BrowserType object that provides a user script with a description of the functionality of the client's Web browser. This component identifies information such as the version of the customer's browser, mainly because when the client browser sends a page request to the server, a User Agent HTTP header is automatically sent, which is an ASCII string that declares the browser and its version. 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 in the Browscap.ini file that matches the title, the default browser properties are used. If the object does not find a match and the default browser settings are not specified in the Browscap.ini file, it sets each property to the string "UNKNOWN". By default, the Browscap.ini file is stored in windows\\\\system\\\\inersrv (if it is 95/98+pws4) or nt\\\\system32\\\\inersrv (in the case of NT) directory, You can edit the text file yourself to add your own properties or modify the file based on the latest version of the browser's updated file. Consider the following checkcookie () procedure, using the cookie property of the Browsercap object to determine whether the client browser supports cookies and returns information:
<%
Sub Checkcookie ()
Set browsercap=server.createobject ("MSWC. BrowserType ")
If Browsercap.cookie=true Then
Response.Write "Your browser supports cookie!"
Else
Response.Write "Sorry, the browser you are using does not support cookie!"
End If
End Sub
%>
Ii. File Access Components If your age is large enough, you must have seen the "Dinosaur era" CGI book, which is the earliest form of WEB guestbook. It was also difficult to connect a server backend database in an Internet-based WEB application, so the historical information in the guestbook was not stored in the back-end database as it is today. So where is this data stored? The answer is "text file", the CGI program can send the information received from the client into a text file stored on the server side, the file can be an HTML file or a TXT file, This allows programmers to save customer information without connecting to the database, but it is cumbersome to write such CGI programs, and the simplest sample of such a program is listed below:
#!/usr/local/bin/perl
# Perl Location One your server
print "content-type:text/plain\\\\n\\\\n";
if ($ENV {\\\ ' request_method\\\ '}eq "POST") {
Read (STDIN, $buffer, $ENV {\\\ ' content_length\\\ '});
}elsif ($ENV {\\\ ' request_method\\\ '}eq "GET") {
$buffer = $ENV {\\\ ' query_stirng\\\ '};
}
@pairs =split (/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) =split (/=/, $pair);
$value =~tr/+//;
$value =~s/% ([a-f a-f 0-9][a-f a-f 0-9])/pack ("C", Hex ($))/eg;
$FORM {$name}= $value;}
$file _name= "Guestbook.txt"; #File name
#具体内容
Open (FILE, ">> $file _name") | | Die "Open File Error";
Print FILE "Recording 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": $FORM {\\\ ' zip\\\ '}
Print FILE "mail Address": $FORM {\\\ ' email\\\ '}\\\\n ";
Print FILE "return comment": $FORM {\\\ ' content\\\ '}
Close (FILE)
As you can feel, these CGI programs are less readable and easy to operate than ASP. Then you have to ask the ASP can also be directly on the server to write files? The answer, of course, is yes. But smart friends may think, since ASP and WEB database connection so convenient, we need to write the customer information in the text file, ASP This function is not superfluous? Indeed, for those of our common guestbook, BBS and other Web applications, Whether in the execution efficiency or ease of use of the program, we can no longer write text files to replace the database, but in some WEB applications, writing text files is both a specification and a relatively simple method of database. If you are familiar with NT, you must know that NT has a very strong security mechanism, it can be almost all the server operation and connection information automatically saved in a file suffix named. log, in fact, this technology can also be used in the WEB, to record some of the customer login information. The following program is the use of ASP read and write text file features, in a WEB BBS program to create automatic recording of each user's statement record function.
<%
Set fs = CreateObject ("Scripting.FileSystemObject")
ForReading = 1
\\\ ' opens the file in read-only mode. This file cannot be written to.
ForAppending = 8
\\\ ' opens the file and writes to the end of the file.
Tristateusedefault =-2
TristateTrue =-1
Tristatefalse = 0
\\\ '-----------Write system log start--------
Servermap=server. MapPath ("\\\\bbs\\\\log\\\\")
\\\ ' Mapping System physical Path
temp=servermap& "\\\\" &year (date) &month (date) & "\\\\"
\\\ ' Gets the physical path and time of the system and takes this as the physical path where the log file is stored
If not FS. FolderExists (temp) Then
Fs. CreateFolder (temp)
End If
If you do not fully understand the above procedure, please listen to the author slowly. The file access component provides methods and properties that you can use to access the computer's file system. We can use the file access component to create a FileSystemObject object, and the first sentence of the above program is to create an object instance named FS with the file access component. After the object is created, you can access the file through it, which has no attributes, and its only meaning is to create, open, or read or write a text file. The FileSystemObject object has two most common methods, one for creating files and the other for opening and reading text files. The CreateTextFile method obtains the file name you specify and creates it, which returns a TextStream object that you can manipulate after the file is created, with the syntax of the CreateTextFile method as follows:
Set Objtextstream=filesystemobject.createtextfile (Filename,[overwrite],[unicode])
The following author explains the parameters of the CreateTextFile method
1. FileName contains a string of file pathname, which can be the full path name of the file, including the drive name and directory name, or just the file name, if only the file name is included, the files will be created at the root of the site.
2, Overwrite Boolean, set to False can prevent the FileSystemObject object when creating a new file delete the existing file, this parameter is optional, if there is no assignment system by default is true, existing files with the same file name will be deleted.
3, Unicode optional parameters. A Boolean value indicates whether the file is created in Unicode or ASCII file format. True If the file is created in the Unicode file format, False if the file is created in the ASCII file format. If this section is omitted, an ASCII file is assumed to be created.
In the previous program we used set SS=FS. CreateTextFile (Syslog,forwriting,false) to create a file without a log file and write to the file, where "ForWriting" represents a write file.
Unlike the CreateTextFile method, the OpenTextFile method is used to obtain the file name you specify and open it, using the parameters we can do a variety of different operations on the file, and the CreateTextFile method, like OpenTextFile The TextStream method returns an object that allows you to manipulate the file after it is opened. The syntax for the OpenTextFile method is as follows:
Set Objtextstream=filesystemobject.opentextfile (Filename,[iomode],[create],[format])
The parameters are described as follows:
1. FileName must be variable, with CreateTextFile filename
2, IOMode optional constant, the value is one of the following two constants ForReading or ForAppending, if mode is 1, the file is read-only open, if 8, then the file is opened in an append way.
3, create Optional Boolean, specify if you want to open the file does not exist is what to do, if its value is True, when the file does not exist, automatically create an empty file. If False, an error message is generated when the file is not found, and the default value is False, which is recommended to be set to True to avoid checking for errors when opening the file. Shanghai Treatment Impotence Hospital}
4, format optional value, you can choose three kinds of Tristate values to specify the format of the file, 2, 1, and 0 correspond to system default, Unicode and ASCII respectively.
After you open or create a text file, you get a TextStream object that has a cursor, as if it were a cursor in a word-processing program, indicating where the character next to be typed will appear, and also the position of the character you want to read. A TextStream object cannot be created by Creatobject, and the only way to get the TextStream object is to open an existing text file with the FileSystemObject object as described earlier or create a new file.
The properties and methods of the TextStream object are listed below
Textstream.atendofline read-only Boolean, when the cursor is at the end of the current line, its value is true, and vice versa is False
Textstream.atendofstream Read-only boolean if the cursor is at the end of the stream with a value of true, otherwise false
Textstream.column A read-only integer that counts the number of characters from the beginning of the line to the current cursor position
Textstream.line A read-only integer that indicates the line number in the entire file where the cursor is located
Textstream.close () Close the stream and the corresponding text file
Textstream.read (Num) specifies that a certain number of characters are read from the text file starting at 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) writes a string to the stream
Textstream.writeline () writes a text string to the stream
Textstream.skip (Num) in the stream, move the position of the cursor a certain number of string lengths
Textstream.skiplines () in the stream, move the cursor a certain number of rows
Textstream.writeblank writes a certain number of empty rows to the stream
Lines (num)
I believe you can now feel the ASP file Access components of the powerful features, in fact, it can not only write some log files, through which you can even effortlessly automatically update your site, you simply transfer the fixed-format text files to the remote server, through the file The Access component reads the file and automatically generates a completely new HTML page without having to painstakingly update the HTML file one after the other. If you are interested, you can fully use the ASP's File Access component to write your own fully automated HTML generator, to fully enjoy the ease of maintaining the site's sense of advance.
\\\ ' detects if a folder exists or is automatically created
Dim syslog
Dim tempname
Tempname=date
syslog=temp&tempname& ". Log"
\\\ ' file is named e:\\\\bbs\\\\log\\\\ month \\\\ month day. Log
li=user& "&" &Now& "&" &request.servervariables ("REMOTE_ADDR") & "&" &tempfile & "&" &letter& "&" &title
\\\ ' log file is in the format: User name & Send time & user ip& file path & Letter Area & Letter Header
If Fs. FileExists (syslog) Then
Set SS = fs. OpenTextFile (Syslog,forappending,true)
Else
Set SS = fs. CreateTextFile (Syslog,forwriting,false)
End If
\\\ ' detects if the log file exists, appends file contents if present, or writes the file directly
Ss. WriteLine (LI)
Ss. Close
\\\ '-----------log file end of write---------
%>
If you do not fully understand the above procedure, please listen to the author slowly. The file access component provides methods and properties that you can use to access the computer's file system. We can use the file access component to create a FileSystemObject object, and the first sentence of the above program is to create an object instance named FS with the file access component. After the object is created, you can access the file through it, which has no attributes, and its only meaning is to create, open, or read or write a text file. The FileSystemObject object has two most common methods, one for creating files and the other for opening and reading text files. The CreateTextFile method obtains the file name you specify and creates it, which returns a TextStream object that you can manipulate after the file is created, with the syntax of the CreateTextFile method as follows:
Set Objtextstream=filesystemobject.createtextfile (Filename,[overwrite],[unicode])
The following author explains the parameters of the CreateTextFile method
1. FileName contains a string of file pathname, which can be the full path name of the file, including the drive name and directory name, or just the file name, if only the file name is included, the files will be created at the root of the site.
2, Overwrite Boolean, set to False can prevent the FileSystemObject object when creating a new file delete the existing file, this parameter is optional, if there is no assignment system by default is true, existing files with the same file name will be deleted.
3, Unicode optional parameters. A Boolean value indicates whether the file is created in Unicode or ASCII file format. True If the file is created in the Unicode file format, False if the file is created in the ASCII file format. If this section is omitted, an ASCII file is assumed to be created.
In the previous program we used set SS=FS. CreateTextFile (Syslog,forwriting,false) to create a file without a log file and write to the file, where "ForWriting" represents a write file.
Unlike the CreateTextFile method, the OpenTextFile method is used to obtain the file name you specify and open it, using the parameters we can do a variety of different operations on the file, and the CreateTextFile method, like OpenTextFile The TextStream method returns an object that allows you to manipulate the file after it is opened. The syntax for the OpenTextFile method is as follows:
Set Objtextstream=filesystemobject.opentextfile (Filename,[iomode],[create],[format])
The parameters are described as follows:
1. FileName must be variable, with CreateTextFile filename
2, IOMode optional constant, the value is one of the following two constants ForReading or ForAppending, if mode is 1, the file is read-only open, if 8, then the file is opened in an append way.
3, create Optional Boolean, specify if you want to open the file does not exist is what to do, if its value is True, when the file does not exist, automatically create an empty file. If False, an error message is generated when the file is not found, and the default value is False, which is recommended to be set to True to avoid checking for errors when opening the file.
4, format optional value, you can choose three kinds of Tristate values to specify the format of the file, 2, 1, and 0 correspond to system default, Unicode and ASCII respectively.
After you open or create a text file, you get a TextStream object that has a cursor, as if it were a cursor in a word-processing program, indicating where the character next to be typed will appear, and also the position of the character you want to read. A TextStream object cannot be created by Creatobject, and the only way to get the TextStream object is to open an existing text file with the FileSystemObject object as described earlier or create a new file.
The properties and methods of the TextStream object are listed below
Textstream.atendofline read-only Boolean, when the cursor is at the end of the current line, its value is true, and vice versa is False
Textstream.atendofstream Read-only boolean if the cursor is at the end of the stream with a value of true, otherwise false
Textstream.column A read-only integer that counts the number of characters from the beginning of the line to the current cursor position
Textstream.line A read-only integer that indicates the line number in the entire file where the cursor is located
Textstream.close () Close the stream and the corresponding text file
Textstream.read (Num) specifies that a certain number of characters are read from the text file starting at 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) writes a string to the stream
Textstream.writeline () writes a text string to the stream
Textstream.skip (Num) in the stream, move the position of the cursor a certain number of string lengths
Textstream.skiplines () in the stream, move the cursor a certain number of rows
Textstream.writeblank writes a certain number of empty rows to the stream
Lines (num)
I believe you can now feel the ASP file Access components of the powerful features, in fact, it can not only write some log files, through which you can even effortlessly automatically update your site, you simply transfer the fixed-format text files to the remote server, through the file The Access component reads the file and automatically generates a completely new HTML page without having to painstakingly update the HTML file one after the other. If you are interested, you can fully use the ASP's File Access component to write your own fully automated HTML generator, to fully enjoy the ease of maintaining the site's sense of advance.
ASP Basics Tutorial: Other ASP Common components