Lesson 16th: Other ASP-common components

Source: Internet
Author: User
Tags date format file system connect split syslog system log html generator
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 the database, the database online operation, and so on.

In the last two articles, the author introduces the use of components such as AD rotator, Database Access, and so on, let's take a look at some other ASP-common components today.

Browser capabilities components are well known, not all browsers support all aspects of today's Internet technology. There are some features that some browsers support while others do not, such as ActiveX controls, video streaming, dynamic HTML, Flash, and scripting programs. Using the 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 user script with a description of the features of the client Web browser. This component recognizes information such as the version of the client's browser, mainly because when a client browser sends a page request to the server, it automatically sends a User Agent HTTP header, 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 BrowserType object's properties. If the object does not find an entry 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 setting is not specified in the Browscap.ini file, it sets each property to the string "UNKNOWN". By default, browscap.ini files are stored in windows\system\inersrv (if 95/98+pws4) or nt\system32\inersrv (if NT) directories, you can edit this text file yourself, To add your own properties or modify the file according to the latest published browser version of the update file. See the following Checkcookie () procedure, using the Browsercap object's cookie property 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
%>

For more information about Browser capabilities components, see Dynamic Web Design Skills--asp (2).

Second, File Access components If your age is large enough, you must have seen the "dinosaur era" of the CGI guest book, which is the earliest form of WEB guestbook. It was also difficult to connect to a server backend database in an internet-based Web application, so historical information in the guest book was not stored in the back-end database as it is today. So where exactly are these data kept? The answer is "text file", the CGI program can write information received from the client into a text file stored on the server side, which can be HTML files or TXT files, so that programmers can save customer information without connecting to the database, but writing such CGI The procedure is cumbersome, and the simplest sample of such a procedure 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 "Record time": $date \ n ";
Print FILE "name": $FORM {' name '} ';
Print FILE "unit": $FORM {' Company '}\n ';
Print FILE "Phone": $FORM {' Phone '}
Print FILE "Address": $FORM {'}\n ';
Print FILE "Zip": $FORM {' Zip '}
Print FILE "mail Address": $FORM {' email '}\n ';
Print FILE "return comment": $FORM {' content '}
Close (FILE)

You can feel that this kind of CGI program is less readable and easy to operate than ASP. Then you must ask whether the ASP can also be directly on the server to write files? The answer is certainly yes. But smart friends may think, since the connection between ASP and WEB database is so convenient, why do we need to write customer information in the text file, ASP This function is not superfluous? Indeed, for our common guest books, BBS and other Web applications, whether in the execution of the program efficiency or ease of use, we can not be used to write text files to replace the database, but in some Web applications to write a text file is a specification is a relatively convenient method of database. If you are more familiar with NT, you must know that NT has a very powerful security mechanism that can automatically save information about almost all server operations and connections in a file with a suffix named. Log, which can also be used on the WEB to record some customer login information. The following program is the use of ASP to read and write text file features, in a WEB BBS program to create automatic recording of each user to speak records of the function.

<%
Set fs = CreateObject ("Scripting.FileSystemObject")
ForReading = 1
' Open the file in read-only mode. This file cannot be written to.
ForAppending = 8
' Open the file and write at the end of the file.
Tristateusedefault =-2
TristateTrue =-1
Tristatefalse = 0

'-----------write to 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 of the log file
If not FS. FolderExists (temp) Then
Fs. CreateFolder (temp)
End If
' Detects if a folder exists or automatically creates
Dim syslog
Dim tempname
Tempname=date
syslog=temp&tempname& ". Log"
' File name is e:\bbs\log\ month \ Month day. Log
li=user& "&" &Now& "&" &request.servervariables ("REMOTE_ADDR") & "&" &tempfile & "&" &letter& "&" &title
' Log file record format: User name & time & user ip& file path & Letter 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
' Detects if log file exists, append file content if present, or write file directly
Ss. WriteLine (LI)
Ss. Close
'-----------log file write end---------
%>

If you do not fully understand the above procedure, please listen to the author slowly way. The file access component provides methods and properties that you can use to access your computer's file system. We can use the file access component to create a FileSystemObject object, the first sentence of which 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, which has no attributes, and its only meaning is to create, open, or read and write text files. There are two most common methods for FileSystemObject objects, one for creating files and another for opening and writing text files. The CreateTextFile method obtains the filename you specify and creates the file, which returns a TextStream object that you can use to manipulate the file after it is created, and the syntax of the CreateTextFile method is as follows:

Set Objtextstream=filesystemobject.createtextfile (Filename,[overwrite],[unicode])

The following authors explain the parameters of the CreateTextFile method

1. FileName contains a string of file path names, which can be the full path name of the file, including the drive and directory names, or just the file name, and the file will be created in the root directory of the site if it contains only the filename.

2, Overwrite Boolean, set to False prevents FileSystemObject object from deleting existing files when creating a new file, which is optional, and if no assignment system defaults to true, existing files with the same file name will be deleted.

3, Unicode optional parameters. The Boolean value indicates whether the file is created in Unicode or ASCII file format. True If the file is created in a Unicode file format, False if the file is created in an ASCII file format. If this section is omitted, the ASCII file is assumed to be created.

In the previous program we used the set SS=FS. CreateTextFile (Syslog,forwriting,false) to create a file and write to the file if the log file does not exist, "ForWriting" here represents the write file.

Unlike the CreateTextFile method, the OpenTextFile method is used to obtain the filename you specify and to open the file, using the parameters that it takes we can perform various operations on the file, as well as the CreateTextFile method, OpenTextFile method returns a TextStream object that allows you to manipulate the file after it has been opened. The syntax for the OpenTextFile method is as follows:

Set Objtextstream=filesystemobject.opentextfile (Filename,[iomode],[create],[format])

The parameter description is as follows:

1, filename must be variable, the same createtextfile filename

2, IOMode optional constant, the value is one of the following two constants ForReading or ForAppending, if mode is 1, the file opens as read-only, and if 8, 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 action, if the 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, and 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 kinds of tristate values to specify the format of the file,-2,-1, and 0 correspond to system defaults, Unicode, and ASCII respectively.

When 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 processor, indicating where the next character will appear, as well as the position of the character you want to read. A TextStream object cannot be created by Creatobject, and the only way to get TextStream object is to open an existing text file or create a new file with the FileSystemObject object as described earlier.

The properties and methods of the TextStream object are listed below

Textstream.atendofline a read-only Boolean that evaluates to True when the cursor is at the end of the current line, or false

Textstream.atendofstream a read-only Boolean value of TRUE if the cursor is at the end of the stream, or 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 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 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 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 lines to the stream

Lines (num)

I believe you can now feel the powerful features of ASP file Access components, in fact, it is far more than can write a log file, through which you can even effortlessly remote Automatic Updates your site, you just have to transfer the fixed format text file to the remote server, through The file Access component reads the files and automatically generates completely new HTML pages, without having to laboriously update the HTML files one by one. If you are interested, you can use the ASP's file Access component to write a fully automated HTML generator, fully enjoy the maintenance of the site ahead of ease.



Related Article

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.