In the previous article, the author introduced in detail two very practical methods for using ASP built-in objects Application and Session. Since both OnStart and OnEnd events scripts must be declared in the Global.asa file, this article will introduce in detail how to use the Global.asa file. In order to enable everyone to master the knowledge they have learned so far, this article will also include an ASP Chat program for your reference.
Recently, many friends have sent me a letter to ask me why there are such and such mistakes when running the example program in the first two issues. First of all, I want to declare to you that these programs are written by myself and have passed the qualification inspection before leaving the factory, and there are no counterfeit and shoddy products. :) Since cookies are used in the program to record customer information, if you do not set accept cookies in your browser, the program will not run normally. In addition, the method used by the program to record customer information in the client's cookies is Response.Cookie. This statement must be written before the first <HTML> tag in the ASP file. This is because the cookie is the header information transmitted as HTTP If the HTTP header information has been transmitted to the customer, if the Response.Cookie is used after the HTTP header information has been transmitted to the customer, the following error will appear: The HTTP header has been written to the customer's browser. Any modification of an HTTP header must be before writing to the page content. , Some friends may not pay attention when clipping the program, disrupting the pre and back order of the program, or incorrectly adding HTML code before the ASP sample program, resulting in an error in the program running. Therefore, I suggest that on the one hand, do not make any changes to the program when running the routine for the first time, and try to gradually improve it based on understanding the program. On the other hand, when running the ASP program, you should at least select acceptable cookies in the browser. , otherwise, once cookies or sessions are used in the ASP program, they will not work properly.
Next, I will introduce to you how to use Global.asa file.
What is a Global.asa file? It is actually an optional file in which the program writer can specify event scripts and declare objects with session and application scopes. The contents of this file are not used to display to the user, but to store event information and objects used globally by the application. The name of the file must be Global.asa and must be stored in the root directory of the application. Each application can only have one Global.asa file.
In the Global.asa file, if the included script is not encapsulated with <SCRIPT> tag, or the defined object has no session or application scope, the server returns an error. We can write scripts included in the Global.asa file in any script-enabled language. If multiple events are in the same scripting language, they can be organized in a set of <SCRIPT> tags.
The process declared in the Global.asa file can only be called from one or more scripts related to the Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd events. They are not available in the ASP page of ASP-based applications. If you want to share procedures between applications, you can declare them in a separate file and then use the Server-Side Inclusion (SSI) statement to include the file in the ASP program that calls the procedure. Generally, the extension of the containing file should be .inc.
Here is a very standard Global.asa file:
< SCRIPT LANGUAGE=VBScript RUNAT=Server>
'Session_OnStart Runs when the client first runs any page in the ASP application
'Session_OnEnd Runs when a client's session timed out or exits the application
'Application_OnStart Runs when any customer first accesses the homepage of the application
'Application_OnEnd Runs when the site's WEB server is down
< /SCRIPT>
< SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
VisitorCountFilename = Server.MapPath (/ex2) + /VisitCount.txt
Set FileObject = Server.CreateObject(Scripting.FileSystemObject)
Set Out= FileObject.OpenTextFile (VisitorCountFilename, 1, FALSE, FALSE)
Application(visitors) = Out.ReadLine
Application(VisitorCountFilename) = VisitorCountFilename
End Sub
'=================================================================== =========
SUB Application_OnEnd
Set FileOutObject = Server.CreateObject(Scripting.FileSystemObject)
Set Out= FileOutObject.CreateTextFile (Application(VisitorCountFilename), TRUE,FALSE)
Out.WriteLine(application(visitors))
End Sub
'=================================================================== ==========Sub Session_OnStart
Session.Timeout = 5
Application(visitors) = Application(visitors) + 1
Session(ID)=Session.SessionID
End Sub
< /SCRIPT>
In this Global.asa program, the File Access component of ASP is involved, which provides methods, properties, and collections for accessing the file system. This will be discussed in future ASP components. Here, it serves to create new files on the server and write to the files. This is actually a Global file of an ASP page access counter application. First, when the customer first accesses the homepage of the application, the process Application_OnStart defines a text file of VisitCount.txt in the virtual directory specified on the server, and Save the path and content of the file in application-level variables. When any client accesses any page in an ASP application, the process Session_OnStart definition automatically adds the value of the application-level variable visitors. In this way, whenever a customer visits the page, the variable visitors will be automatically added to the function of counting click-through rate. Since the value of the variable visitors is stored in the system memory, if the server is shut down or restarted, the data stored in the variable will be automatically lost. Therefore, by defining the process Application_OnEnd, the data is written to the established before the server is shut down or restarted. In the text file, this ensures that when the server starts again, the Application_OnStart process can read previous statistics from the VisitCount.txt file.
After this period of study, I believe everyone has been able to use the built-in ASP objects we have learned to write some simpler ASP applications. Don’t underestimate the basic ASP knowledge you have now! In fact, you have been able to develop some simple but practical ASP applications. Let me give you a very simple ASP WEB chat room program. You will find that writing a chat room is such an easy and easy thing. Maybe friends have seen how to write ASP chat programs in some magazines, but the author wrote a simpler program here, using only one .asp file. Please clip the following code into the notepad and save it as chat.asp.
< %@ Language=VBScript %>
< %
Response.Buffer=true ' Set the output cache to display different pages.
On error resume next ' Ignore the program error part
If Request.ServerVariables(Request_Method)=GET then
' Determine how the client requests the WEB page
'-----------------------------------------------------------------------------------------------------------------------------
' Customer login interface
'-----------------------------------------------------------------------------------------------------------------------------
%>
< form method=POST action=chat.asp>< p>
< input type=text name=nick size=20 value=nick style=background-color: rgb(192,192,192)>< br>
< input type=submit value= Enter the chat room name=B1 style=color: rgb(255,255,0); font-size: 9pt; background-color: rgb(0,128,128)>
< p>< input type=hidden name=log size=20 value=1>< br></p>
< /form>
< %
Response.End ' End the process of the program
Else
Response.clear ' Clear the content in the cache
dim talk
If Request.Form(nick)<> then
' Determine whether the customer is in the chat interface
Session(nick)=Request.Form(nick)
End If
'-----------------------------------------------------------------------------------------------------------------------------
Customer chat interface
'-----------------------------------------------------------------------------------------------------------------------------
%>
< form method=POST action=chat.asp name=form1> < p>< %=Session(nick)%> Speak: < input type=text name=talk size=50>< br>
< input type=submit value= Submit name=B1>
< input type=reset value= Cancel name=B2></p>
< /form>
< A HREF=/asptest/shusheng/chat.asp> Leave</ /a>< br>< br>
< %
If Request.Form(log)<>1 then
If trim(Request.Form(talk))= then
' Determine whether the user has not entered anything
talk=Session(nick)& Silence is gold.
Else
talk=trim(Request.Form(talk))
' Remove the space after the character
End If
Application.lock
Application(show)=< table border='0' cellpadding='0' cellpacing='0' width='85%' >< tr>< td width='100%' bgcolor='#C0C0C0'>< /td >< /tr>< tr>< td width='100%'>< font color='#0000FF'> &Session(nick)&time& from &Request.ServerVariables(remote_addr)& said: < /font>&talk&< /td >< /tr>< tr>< td width='100%' bgcolor='#C0C0C0'>< /td>< /tr>< /table>< br>&Application(show)
Application.UnLock
Response.Write Application(show)
End If
End If
%>
Let’s conduct a step-by-step analysis of this chat room program.
First of all, since all customers in the chat room must be able to share information, it is inevitable to use the object Application with application-level variables. This is the key to establishing a Chat program. All conversation data is stored at an application-level. variables so that all clients can read. We can use the request object we have learned to get the conversation entered by the client and save it in the variable talk, and then store the value of the talk into the application-level variable show, as follows:
< % Application(show)=talk&Application(show) %>
The next thing to consider is how to deal with different customers when they operate on Chat applications at the same time. This problem is actually the same as two users in the database writing the same record at the same time. If two users write to the same application-level variable at the same time, the modifications made by one user will be overwritten by the operation of another user. , so if certain measures are not taken to concurrent access to Application object data, it will cause one user's submission to be overwritten by another user's submission when two users try to submit to the Asp chat room application at the same time. It disappeared before anyone who was present in the session saw it. To avoid this kind of problem, we need to use the Application object's Lock property to constrain only the current user to edit or add properties of the Application object. This way, when the user starts to modify application-level variables, the Application object is explicitly Unlocked Until then, the properties of the Application object can only be edited by the user. If other users request to edit the Application object at this time, these users will have to queue up until they know that the application is Unlocked. As shown below:
Application.lock
Application(show)=talk&Application(show)
Application.UnLock
Now that you have understood the core part of the entire program, consider how to save customer information. Here we are going to use a session-level variable, that is, save the customer's nick in the Session. like:
Session(nick)=Request.Form(nick)
Finally, what we need to consider is how to handle various events only in one .asp file, such as: customer login interface, customer chat interface. Since the first time the client requests the .asp file, it uses a simple HTTP GET method, but when the client adds data to the form field on the page and submits the form to itself, the .asp file will be requested again, but this The data is passed through HTTP POST. The way the file is requested can be determined in two ways. First, test whether the Request.Form collection contains members. If not, it means that there is no data sent to the form for processing. The second is to use the Request.ServerVariables(Request_Method) variable. If the form is requested via HTTP GET, this variable returns GET and POST if the form is submitted. Since the latter method is more direct in determining the request method of a file, we use the following code to make a judgment:
if Request.ServerVariables(Request_Method)=GET then
Since we set up the ASP cache, when the program determines that the page request method is GET, the program runs Response.End to end all subsequent operations. Otherwise, run Response.clear, clear the existing content in the cache, and continue the program's run. In this way, we can use the same .asp file to display different interfaces to customers according to different situations.
Okay, I have told you the core part of this ASP Chat program. Please read the program yourself for some details. In fact, this program still has many shortcomings. The biggest problem is that I did not write automatic refresh. , so if you don't speak in Chat, you won't see what other customers say. Everyone knows the charm of Chat in the Internet. In fact, using ASP can also achieve powerful functions similar to irc. Of course, this requires writing more code. If you are interested, please write me and I will In the future, we will gradually improve the level of this chat program so that everyone can also pass on the hidden version of Opera. Note: With this chat program, you can use it to build a chat application on your PWS. Just tell your friend your IP address, such as http://202.96.210.33/asp/chat.asp, you will You can chat online through your computer. It’s very cool. If you don’t believe it, try it!