ASP Tutorial: Global.asa file to use

Posted by admin 0 comments

In the article the author introduces to us two very useful ASP built-in Application and Session objects to use. Since both the OnStart, OnEnd event script must be declared in the Global.asa file, so this will explain to you Global.asa file is used. In order to master all the knowledge learned so far, this ASP-Chat will also give a procedure for reference.

Recent letters from many of my friends asked me why the first two examples of such a program is running kind of mistake. First of all, I have to state that these procedures are to write my own, in a “factory” have been qualified before the test, no “fake and shoddy products.” :) As the procedure used in the cookie record customer information, so if you browser is not set to accept cookie program will not work correctly. In addition, the program will customer information recorded in the client’s cookie is used Response.Cookie, this statement must be written in the ASP file in the first <HTML> tags, and this is because the HTTP Cookie is transmitted as part of header information sent to the customer, if a HTTP header information has been transmitted to the customer before use Response.Cookie, the following error: “HTTP title has been written to the client browser. Any HTTP header modification must be written before the page content. “In the clip procedure may be some friends when not paying attention, disrupting the process before and after the order is wrong or HTML code to example programs in ASP before, resulting in run error. Therefore, I recommend everyone to run the routine at the first time not to make any changes to the program, try to understand the basis of the program to gradually improve, on the other hand in the ASP program is running at least an acceptable choice in the browser Cookie, or if the program uses ASP Cookie or Session, they will not work correctly.

Now I come to tell you to use Global.asa file.

What is Global.asa file? It is actually an optional file, the programmer can specify the event in the script file, and declare a session and application scope objects. The contents of the file is not used to the user, but used to store event information and the global use by the application object. The file name must be Global.asa and must be stored in the application’s root directory. Each application can have only one Global.asa file.

In the Global.asa file, if the script is not included with the <SCRIPT> tag package, or define the object is not a session or application scope, the server will return an error. We can use any scripting language supported Global.asa file contains the script. If more than one event using the same scripting language, you can organize them in a <SCRIPT> tag.

Global.asa file a statement in the course of only from one or more Application_OnStart, Application_OnEnd, Session_OnStart and Session_OnEnd events related to call the script. ASP-based applications in ASP pages, they are not available. If you want to be shared between the application process can be declared in a separate document these processes, then use the server-side inclusion (SSI) statement that the document included in the call this process ASP program. Typically, include file extension should be. Inc.

The following is a standard Global.asa file:

<SCRIPT LANGUAGE = “VBScript” RUNAT = “Server”>
\ ‘Session_OnStart when the customer first run ASP applications to run on any one page
\ ‘Session_OnEnd When a customer’s session times out or exit the application to run
\ ‘Application_OnStart when any customer’s first visit to the home page of the application to run
\ ‘Application_OnEnd WEB server when the site run off
</ 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, involving the ASP of the File Access component, it can provide the method used to access the file system, properties, and collections. This component will be carried out after the ASP discussion. Here, it played a file on the server and create a new file for writing role. This is actually a ASP page access counter Global application file, first when a client first access the application’s home page, the process Application_OnStart defined on the server specified virtual directory, create a new VisitCount.txt text file and the contents of the file path and save the application-level variable. When a customer to access any ASP application, any one of the pages, the definition of the application process Session_OnStart-level value of the variable visitors automatically add a. Thus, whenever a customer visits the page, visitors will automatically add the variable one, to play the role of statistics hits. Because the variable value of visitors is being stored in system memory, so if the server is shut down or restart, the data stored in a variable will automatically lose, so by defining the process Application_OnEnd, shut down or restart the server before data is written prior established text document, this will ensure that when the server starts again, Application_OnStart processes can read from the file before VisitCount.txt statistics.

After this the time to learn, I believe we already have relatively proficient in using what we’ve learned these ASP Neijianduixiang Lai Bianxie some simple ASP application, do not underestimate what you now grasp the basic knowledge of ASP Oh! In fact, you have to develop some simple but useful ASP application. Now I would like to give a very simple ASP WEB chat program, you will find that the original is a chat room to write so easily and easy to do. Friends who may have seen in some magazines on the compilation of ASP chat program, but the authors here write a more simple procedure, just use a. Asp file. Please cut and paste the following code to the notebook and save it as chat.asp.

<% @ Language = VBScript%>
<%
Response.Buffer = true \ ’set the output buffer, is used to display different pages.
On error resume next \ ‘Ignore error, part of the
If Request.ServerVariables (“Request_Method”) = “GET” then
\ ‘Determine in what way our customers request WEB page
\’————————
\ ‘Customer login screen
\’————————
%>
<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 of the treatment process
Else
Response.clear \ ‘empty the contents of 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
\’———————— \ ‘
Guest Chat interface
\’————————
%>
<Form method = “POST” action = “chat.asp” name = form1> <p> <% = Session (“nick”)%> to 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”> left </ a> <br> <br>
<%
If Request.Form (“log”) <> 1 then
If trim (Request.Form (“talk “))=”" then
\ ‘Determine whether the user has not entered any content
talk = Session (“nick”) & “silence is golden.”
Else
talk = trim (Request.Form (“talk”))
\ ‘To remove the spaces after the characters
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”) & “of” & Session (“nick”) & time & “says: </ 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
%>

Filed in Asp 0 comments
No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
«
Next Post
»