Application of ASP Built-in object application

Source: Internet
Author: User
Tags count integer modify variables

In a Web application, when a user accesses the application, the session-type variable allows the user to share data in all pages of the Web application, and if another user accesses the Web application at the same time, he has his own session variable, However, two users cannot share information through session variables, and application type variables can be implemented between multiple users of the site in allin the pageShare information. You can understand that the session is a local variable, and application is a global variable.

All. asp files in the same virtual directory and its subdirectories make up the ASP application. Instead of using the Application object, we can share information between all users of a given application and persist the data during the server's run. Also, the Application object has control over how the application-tier data is accessed and the events that can be used to trigger the process when the application starts and stops.

First, application ("name") =value
As with the session ("name") =value, the Application object also has no built-in properties. Of course, users can customize attributes, which can also be called collections.

Once the properties of the Application object are assigned, it persists until the Web Server service is turned off so that application stops. Because the values stored in the Application object can be read by all users of the application, the properties of the Application object are particularly suitable for passing information between users of the application.

<%
Application ("myname") = "WEBJX"
%>

Second, Application.Lock
The Lock method prevents other users from modifying the properties of the Application object to ensure that only one customer can modify and access the application variable at the same time. If the user does not explicitly call the Unlock method, the server will unlock the Application object after the. asp file finishes or times out. The simplest is an example of a page count.

1,num.asp

<%
Application.Lock
Application ("NumVisits") = Application ("NumVisits") + 1
Application.UnLock
%>
You are the first <%=application ("NumVisits")%> visitors to this page

Of course, if you need to count the initial value, then you should write a judgment.

<%
If Application ("NumVisits") <9999 Then
Application ("NumVisits") =10000
End If
Application.Lock
Application ("NumVisits") = Application ("NumVisits") + 1
Application.UnLock
%>
You are the first <%=application ("NumVisits")%> visitors to this page

The above program, you will find that every time you refresh, will be counted cumulative, such as by IP value access to the number of words, then establish a session

2,vnum.asp

<%
If session ("visitnum") = "" Then
Application.Lock
Application ("NumVisits") = Application ("NumVisits") + 1
Application.UnLock
Session ("visitnum") = "visited"
End If
%>
You are the first <%=application ("NumVisits")%> visitors to this page

Third, Application.UnLock
In contrast to the lock method, the Unlock method is to allow other users to modify the properties of the Application object. As can be seen in the example above, the Unlock method unlocks the object so that the next client can increase the value of the numvisits.

Of course, it should be noted that the number to ensure that the server is not restarted, because so the access is the number is based on the page, not to save the file or save to the database such processing.
Typically, a application event is triggered when the server restarts.

Four, Application_OnEnd
The Application_OnEnd event occurs after the Session_OnEnd event when the application exits, and the process of Application_OnEnd events must also be written in the Global.asa file.
For example, in the above program, if the server shutdown is bound to trigger the Application_OnEnd event, then the event can be data-saving work, has made the next data to continue along.
Of course, the Application_OnEnd event occurs after the Session_OnEnd event when the application exits


<script language= "VBScript" runat= "Server" >
.............
Sub Application_OnEnd
.............
End Sub
.............
</SCRIPT>

Five, Application_OnStart
The Application_OnStart event occurs before the first time a new session (that is, a Session_OnStart event) is created. The Application_OnStart event is triggered when the Web server starts and allows requests for files that are contained by the application.


<script Language=scriptlanguage runat=server>
.............
Sub Application_OnStart
.............
End Sub
.............
</SCRIPT>


So now you can imagine the code inside the global.asa


<script language= "VBScript" runat= "Server" >
Sub Application_OnStart
.....................
End Sub

Sub Session_OnStart
.....................
End Sub

Sub Session_OnEnd
.....................
End Sub

Sub Application_OnEnd
.....................
End Sub
</SCRIPT>


What is the exact global.asa? And listen to let's:

5 Big objects have mastered, to practice under the example

<%@ LANGUAGE = VBScript%>
<% Option Explicit%>
<Html>
<title> Webmaster Information </title>
<body>
<%
' Set page does not use cache
Response.Expires = 0
%>
<%
Dim guessnum
On Error Resume Next
Guessnum=request ("number")
If guessnum= "" then guessnum= "0" End If
GUESSNUM=CLNG (Guessnum)

Session ("Count") =session ("Count") + 1
If session ("Count") < ten and guessnum <> session ("number") then

%>

<form action= "guessnumber.asp" >
<input type= "text" name= "number" >
<input type= "Submit" value= "submitted" >
</form>
<%
End If
If guessnum<0 or guessnum>100 then
Response.Write "Please enter an integer between 1~100"
ElseIf Guessnum=0 Then
Session ("Count") = 0
Randomize
Session ("number") = Int (RND * 100 + 1)
Response.Write "Please enter an integer between 1~100"
ElseIf Guessnum > session ("Number") then
Response.Write "You're guessing too much."
ElseIf Guessnum < session ("number") then
Response.Write "Chinese Webmaster"
ElseIf Guessnum = Session ("number") then
Response.Write, "Congratulations, you guessed it."
End If

Response.Write "<br> Guess" & Session ("Count") & "Times"
If session ("Count") =10 Then
Response.Write "The answer is" & Session ("number")
End If
%>
<a href= "guessnumber.asp? Number=0 "> Chinese webmaster </a>
</body>

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.