16. Other commonly used ax Components

Source: Internet
Author: User
Tags html generator web database
When you use ASP to write server applications, you must rely on ActiveX components to provide powerful web application functions. For example, you need to connect to the database and perform online operations on the database.

In the last two articles, the author introduced how to use components such as ad rotator and database access. Today, let's take a look at some other common ASP components.

I. browser capabilities components are well known. Not all browsers support all aspects of today's Internet technology. Some browsers do not support some features, such as ActiveX controls, image streams, dynamic HTML, Flash, and script programs. With the browser capabilities component of ASP, you can design "smart" web pages to present content in a format suitable for browser performance. The browser capabilities component can create a browsertype object that provides a user script with the function instructions of the client web browser. This component identifies the version and other information of the client browser, mainly because when the client browser sends a page request to the server, a User Agent HTTP title is automatically sent, the title is an ASCII string that declares the browser and its version. The browser capabilities component maps the user agent to the browser specified in the file browscap. ini, and identifies the customer's browser through the browsertype object attributes. If the item matching the title cannot be found in the browscap. ini file, the default browser attribute is used. If no match is found for this object and the default browser settings are not specified in the browscap. ini file, it sets each attribute to the string "unknown ". By default, browscap. INI files are stored in the Windows/system/inersrv (for example, 95/98 + pws4) or NT/system32/inersrv (for example, NT) directory. You can edit the text file by yourself, to add your own properties or modify the file based on the latest browser version Update file. Please refer to the following checkcookie () process and 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, your browser does not support cookies! "
End if
End sub
%>

For more information about browser capabilities, see ASP (2 ).

Ii. File Access Components if your network age is large enough, you must have seen the CGI Guestbook of the "dinosaur age", which is the earliest prototype of the Web guestbook. At that time, it was very difficult to connect to the backend database of the server in an Internet-based Web application. Therefore, the historical information in the message book is not stored in the backend database as it is today. So where exactly is the data stored? The answer is "Text File". The CGI program can write the information received from the client into a text file stored on the server, which can be an HTML file or a TXT file, in this way, programmers can save the customer information without connecting to the database, but writing such CGI programs is very cumbersome. The following lists the simplest samples of such programs:

#! /Usr/local/bin/perl
# Perl location one your server
Print "Content-Type: text/plain/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 0-9])/Pack ("C", Hex ($1 )) /EG;
$ FORM {$ name }=$ value ;}
$ File_name = "guestbook.txt"; # file name
# Details
Open (file, ">>$ file_name") | die "file opening error ";
Print file "record time": $ date/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 "response comment": $ FORM {'content '}
Close (file)

You can feel that the CGI program is less readable and easier to operate than ASP. So you must ask whether ASP can write files directly on the server? Of course, the answer is yes. However, smart friends may think that, since ASP and web database connections are so convenient, why should we write customer information in text files? Indeed, for common messaging books, BBS, and other Web applications, we cannot use text files to replace databases in terms of program execution efficiency or ease of use, however, in some web application fields, writing text files is both a standard and a more convenient method than a database. If you are familiar with NT, you must know that NT has a very powerful security mechanism. It can automatically save almost all server operations and connection information in a suffix. in log files, this technology can also be used on the web to record some customer login information. The following program uses ASP to read and write text files and creates a function in a web BBS program to automatically record the speech records of each user.

<%
Set FS = Createobject ("scripting. FileSystemObject ")
Forreading = 1
'Open the file in read-only mode. You cannot write this file.
Forappending = 8
'Open the file and write at the end of the file.
Tristateusedefault =-2
Tristatetrue =-1
Tristatefalse = 0

'----------- Start to write the System Log --------
Servermap = server. mappath ("/BBS/log /")
'Map the physical path of the system
Temp = servermap & "/" & year (date) & month (date )&"/"
'Obtain the physical path and time of the system and use it as the physical path for storing log files.
If not fs. folderexists (temp) then
FS. createfolder (temp)
End if
'Check whether a folder exists; otherwise, it is automatically created.
Dim Syslog
Dim tempname
Tempname = Date
Syslog = temp & tempname & ". log"
'File name: e:/BBS/log/month/day. Log
Li = user & "&" & now & "&" & request. servervariables ("remote_addr") & "&" & tempfile & "&" & Letter & "&" & Title
'Log file record format: User Name, sending time, user IP address, file path, email area, and mail 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 yes, append the file content. Otherwise, directly write the file.
SS. writeline (Li)
SS. Close
'----------- Log File writing ended ---------
%>
If you do not fully understand the above program, please let the author know. The file access component provides methods and properties for accessing the 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 an object is created, you can use it to access the file. This object has no attributes. Its unique meaning is to create, open, or read/write text files. The FileSystemObject object has two common methods: one is used to create a file, and the other is used to open and read a text file. The createtextfile method obtains the specified file name 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 following author will explain the parameters of the createtextfile method.

1. filename is a string containing the file path name. It can be the full path name of the file, including the drive name and directory name, or it can only be a file name. If it only contains the file name, the file will be created under the root directory of the site.

2. A boolean value of overwrite. Setting it to false can prevent the FileSystemObject object from deleting an existing file when creating a new file. This parameter is optional. If no value is assigned, the system defaults to true, existing files with the same file name will be deleted.

3. Optional Unicode parameters. Boolean value indicates whether to create a file in Unicode or ASCII format. If a file is created in unicode format, the value is true. If a file is created in ASCII format, the value is false. If this part is omitted, an ASCII file is created.

In the previous program, we used set Ss = FS. createtextfile (syslog, forwriting, false) is used to create a file and write the file if the log file does not exist. "forwriting" indicates writing the 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 carries, we can perform various operations on the file, just like the createtextfile method, the opentextfile method returns a textstream object, allowing you to operate on the object after it is opened. The syntax of the opentextfile method is as follows:
Set objtextstream = FileSystemObject. opentextfile (filename, [iomode], [Create], [format])

Parameters are described as follows:

1. the variables required by filename are the same as the filename of createtextfile.

2. An optional constant of iomode. The value is forreading or forappending, one of the following two constants. If mode is 1, the file is opened in read-only mode. If it is 8, the file is opened in append mode.

3. Create an optional Boolean value that specifies what operations are performed if the file you want to open does not exist. If the value is true, an empty file is automatically created when the file does not exist. If it is false, an error message is generated when the file is not found. The default value is false. We recommend that you set it to true to avoid errors when opening the file.
4. Optional values of format. You can select three tristate values to specify the file format.-2,-1, and 0 correspond to the system default, Unicode, and ASCII, respectively.

After opening or creating a text file, you can get a textstream object with a cursor, as if it were in a word processing program, specifies the position where the characters to be typed will appear. It also specifies the position of the characters to be read. You cannot use creatobject to create a textstream object. The only way to obtain a textstream object is to use the FileSystemObject object to open an existing text file or create a new file.
The attributes and methods of textstream objects are listed below.

Textstream. atendofline read-only Boolean value. When the cursor is at the end of the current row, its value is true, and vice versa.

Textstream. atendofstream read-only Boolean value. If the cursor is at the end of the stream, its value is true; otherwise, it is false.

Textstream. Column read-only integer that counts the number of characters from the beginning of the row to the current cursor position

Textstream. line read-only integer that specifies the row where the cursor is located in the entire file

Textstream. Close () Close the stream and the corresponding text file

Textstream. Read (Num) specifies to read a certain number of characters 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 full 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 cursor position to a certain number of string lengths

Textstream. skiplines () in the stream, move the cursor to a certain number of rows

Textstream. writeblank writes a certain number of blank rows to the stream.

Lines (Num)

I believe that you 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 remotely without any effort, you only need to transfer a fixed format text file to a remote server, read the file through the file access component, and automatically generate a new HTML page, without having to work hard to update HTML files one by one. If you are interested, you can use the File Access component of ASP to write your own automatic HTML generator, fully enjoying the advanced ease of website maintenance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.