ASP Tutorial: The tenth article Global.asa the use of documents and chat procedures

Source: Internet
Author: User
Tags empty file system header http post variables variable trim access
In the previous article, the author gave you a detailed introduction of the two very useful ASP built-in object application and session of the use of the method. Since both the OnStart and the OnEnd event scripts must be declared in the Global.asa file, this article will give you a detailed introduction to the use of global.asa files. In order to make everyone familiar with the knowledge learned so far, this article will also cite an ASP Chat procedures for your reference.

Recently, a lot of friends have been asking me why the example program in the previous two period was so wrong when it ran. First of all, I would like to declare to you that these procedures are written by myself, in the "factory" before the qualified inspection, there is no "fake products." :) Because a cookie is used in the program to record customer information, the program will not function correctly if you do not set the Accept cookie in the browser. In addition, the method used by the program to record customer information in the client's cookie is Response.cookie, which must be written before the first < html> tag in the ASP file, because the cookie is sent to the customer as part of the header information for the HTTP transmission. User, the following error occurs if the HTTP header information has been transmitted to the customer and then used Response.cookie: "HTTP headers have been written to the client browser. Any changes to the HTTP headers must precede the page content. "Maybe some friends didn't notice when they were clipping, disrupting the sequence of the program, or mistakenly adding HTML code to the ASP sample program, causing the program to run incorrectly. Therefore, I suggest that you do not make any changes to the program on the first run of the routine, as far as possible in the understanding of the program based on the gradual improvement, on the other hand, in the running ASP program at least in the browser to select acceptable cookies, or once the ASP program using cookies or Session, All of them will not work correctly.
Let me tell you how to use the Global.asa file.

What is a Global.asa file? It is actually an optional file where program writers can specify event scripts and declare objects with session and application scopes. The contents of this file are not intended to be displayed to the user, but are used to store event information and objects that are used globally by the application. The name of the file must be global.asa and must reside in the application's root directory. There can be only one Global.asa file per application.

In the Global.asa file, the server returns an error if the included script is not encapsulated with a < script> tag, or the defined object does not have a session or application scope. We can write scripts contained in global.asa files in any language that supports scripting. If multiple events use the same scripting language, they can be organized into a set of < script> tags.

A procedure declared in a Global.asa file can only be invoked from one or more scripts related to Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd events. In ASP pages for ASP-based applications, they are not available. If you want to share procedures between applications, you can declare them in a separate file and then use server-side containment (SSI) statements to include the file in the ASP program that calls the procedure. Typically, an extension of the containing file should be. Inc.

The following is a very standard Global.asa file:
< SCRIPT language= "VBScript" runat= "Server" >
' Session_OnStart run when a customer first runs any page in an ASP application
' Session_OnEnd run when a client's session times out or quits the application
' Application_OnStart run when any customer first accesses the first page of the application
' Application_OnEnd run when the Web server for the site 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>

The

In this Global.asa program involves the ASP's file access component, which provides methods, properties, and collections for accessing the file system. This will be discussed later in the ASP's components. Here, it plays the role of creating a new file on the server and writing to the file. This is actually an ASP page accessing the Register application's Global file, first when the customer first accesses the application's home page, the procedure Application_OnStart defines a new VisitCount.txt text file under the virtual directory specified on the server. and save the path and content of the file in an application-level variable. When any client accesses any page in an ASP application, the procedure Session_OnStart defines an automatic addition of the value of the application-level variable visitors. In this way, every time a customer visits the page, the variable visitors will automatically add one, to play the role of statistical clicks. Because the value of the variable visitors is stored in system memory, if the server shuts down or restarts, the data stored in the variable is automatically lost, so by defining the procedure Application_OnEnd, the data is written to a previously established text file before the server shuts down or restarts. This ensures that when the server starts again, the Application_OnStart process can read the previous statistics from the VisitCount.txt file.

After this period of study, I believe that we have been able to more skillfully use of the ASP we have learned to write some of the more simple ASP applications, you can not underestimate the basic knowledge of the ASP you now know Oh! In fact, you have been able to develop some simple but useful ASP applications. Here I will give a very simple ASP WEB chat room program, you will find that writing a chat room is so easy and easy. Perhaps friends have already seen in some magazines the ASP chat program writing method, but the author here himself wrote a simpler program, just use an. ASP file. Please clip the following code into a memo book and save it as a chat.asp.
<%@ language=vbscript%>
<%
Response.buffer=true ' sets output cache to display different pages.
On Error Resume Next ' ignores program error section
If request.servervariables ("request_method") = "get" then
' determines how the customer requests the WEB page br> '------------------------
Client Login Interface
'------------------------
%>

< form method= "POST" action= "chat.asp" >< p>
< input type= "text" name= "Nick" size= "value=" Nick "style=" Background-color:rgb (192,192,192) ">< br>
< input type= "submit" value= "into 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= "" value= "1" >< br></p>
</form>

<%
Response.End ' End of program processing
Else
Response.Clear ' Empty the contents of the cache
Dim talk
If Request.Form ("Nick") <> "then
' Determine if 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= ">< br>"
< input type= "submit" value= "submitted" name= "B1" >
< input type= "reset" value= "Cancel" name= "B2" ></p>
</form>
< A href= "/asptest/shusheng/chat.asp" > Departure </a>< br>< br>

<%
If Request.Form ("log") <>1 Then
If Trim (Request.Form ("talk")) = "" Then
' Determine if the user has not entered any content
Talk=session ("Nick") & "Silence is Golden." "
Else
Talk=trim (Request.Form ("Talk"))
' Remove the space after the character
End If

Application.Lock
Application ("show") = "< table border= ' 0 ' cellpadding= ' 0 ' cellspacing= ' 0 ' width= ' 85% ' >< tr>< TD width= ' 100% ' bgcolor= ' #C0C0C0 ' ></td></tr>< tr>< td width= ' 100% ' >< font color= ' #0000FF ' > from ' &request.servervariables ("REMOTE_ADDR") & "&session" ("Nick") &time& "Say:</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
%>

The first time you run the chat.asp program, you will first see the following page:

 

When you select Nick into the chat room, you will see the following page:

 

Enter what you want to say, and the dialog will appear below the input box, as shown below:

Now let's take a step-by-step analysis of this chat room program.

First of all, because all the customers in the chat room have to be able to share information, it is unavoidable to use object application with application-level variables, which is the key to establishing a Chat program where all the conversation data is stored in an application-level variable so that all customers can read it. We can use the Request object we have learned to get the conversation entered by the customer, save it in the variable talk, and then deposit the talk value in the application-level variable show, as follows:
<% application ("show") =talk&application ("show")%>

The next thing to consider is what to do when different customers are working on the Chat application at the same time. This problem is actually written to the same record as the two users in the database, if two users write to the same application-level variable at the same time, the modifications made by one user are overwritten by the actions of another user, so if the concurrent access to the Appliation object data does not take some measure Will cause two of users to attempt to submit to an ASP chat room application at the same time, a user's submission will be overwritten by another user's submission, disappearing before anyone in the session is seen. To avoid such problems, we need to use the Lock property of the Application object to constrain properties that allow only the current user to edit or increase the Application object, so that when the user starts modifying the application-level variable, the Application object is explicitly Unlocked, the properties of the Application object can only be edited by that user, and if there are other users requesting to edit the Application object at this time, those users will wait in line to know that the application is Unlock. As shown below:
Application.Lock
Application ("Show") =talk&application ("show")
Application.UnLock

Now that you know the core of the whole program, here's how to save the customer information, and here we'll use a session-level variable that will keep the customer's nick on the sessions. Such as:
Session ("Nick") =request.form ("Nick")

Finally, we want to consider how to handle various events in only one. asp file, such as Customer login interface, customer chat interface. Due to the first request from the customer. asp files, the simple HTTP GET method is used, but when the customer adds data to the form field on the page and submits the form to itself, the. asp file is requested again, but this time the data is delivered via HTTP POST. The way a file is requested can be determined in two ways. One is to test whether the Request.Form collection contains members, and if not, indicates that no data has been sent to the form's processing. The second is to use Request.ServerVariables ("Request_method") variables. If a form is requested through an HTTP get method, the variable returns "get" and returns "POST" if the form commits processing. Since the latter method is more straightforward in determining how the file is requested, we use the following code to determine:
If Request.ServerVariables ("request_method") = "Get" Then

Because we set up the ASP cache, so when the program to judge the way the page request is get, the program runs Response.End, to end all the following operations, otherwise run response.clear, empty the contents of the cache, and continue to run the program. So that we can use the same. asp file to show customers different interfaces according to different situations.

Okay, I have this ASP Chat the core part of the program to tell you, the rest of the details of the problem please read your own program it, in fact, this program still has a lot of deficiencies, the biggest problem is that I did not write automatic refresh function, so if you do not speak in the Chat, That would not see what other customers were saying. Chat's charm in the Internet is known to all, in fact, with the ASP can also do with IRC similar powerful functions, of course, this need to write more code, if you are interested please write to me, I will be in the future of the article gradually improve the level of the Chat program, so that everyone has been "Ope R "of the hidden. Note: With this chat program, you can use it to build a chat application on your own PWS, just tell your friends about your IP address, such as http://202.96.210.33/asp/chat.asp, You can chat through your computer online, very cool, do not believe you try!



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.