📄 asp_globalasa.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASP Global.asa</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>ASP The Global.asa file</h1>
<a href="asp_incfiles.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="asp_send_email.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">The Global.asa file is an optional file that can contain
declarations of objects, variables, and methods that can be accessed by every
page in an ASP application.</p>
<hr />
<h2>The Global.asa file</h2>
<p>The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that
can be accessed by every page
in an ASP application. All valid browser scripts (JavaScript, VBScript, JScript,
PerlScript, etc.) can be used within Global.asa.</p>
<p>The Global.asa file can contain only the following:</p>
<ul>
<li>Application events</li>
<li>Session events</li>
<li><object> declarations </li>
<li>TypeLibrary declarations</li>
<li>the #include directive</li>
</ul>
<p><b>Note:</b> The Global.asa file must be stored in the root directory of the
ASP application, and each application can only have one Global.asa file.</p>
<hr />
<h2>Events in Global.asa</h2>
<p>In Global.asa you can tell the application and session objects what
to do when the application/session starts and what to do when the
application/session ends. The code for this is placed in event handlers.
The Global.asa file can contain four types of events:</p>
<b>Application_OnStart</b> - This event occurs when the FIRST user calls the
first page from an ASP application. This event occurs after the
Web server is restarted or after the Global.asa file is edited. The "Session_OnStart"
event occurs immediately after this event.
<p><b>Session_OnStart</b> - This event occurs EVERY time a NEW user requests his
or her first page
in the ASP application.</p>
<p><b>Session_OnEnd</b> - This event occurs EVERY time a user ends a session. A
user ends a session after a page has not been requested by the user for a
specified time (by default this is 20 minutes).</p>
<p><b>Application_OnEnd</b> - This event occurs after the LAST user has ended
the session. Typically, this event occurs when a Web server stops. This procedure is used to clean up
settings after the Application stops, like delete records or write information
to text files.</p>
<p>A Global.asa
file could look something like this:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3">
<tr valign="top">
<td>
<pre><script language="vbscript" runat="server"></pre>
<pre>sub Application_OnStart
'some code
end sub</pre>
<pre>sub Application_OnEnd
'some code
end sub</pre>
<pre>sub Session_OnStart
'some code
end sub</pre>
<pre>sub Session_OnEnd
'some code
end sub</pre>
<pre></script></pre>
</td>
</tr>
</table>
<p><b>Note:</b> Because we cannot use the ASP script delimiters (<% and %>) to
insert scripts in the Global.asa file, we put subroutines inside an HTML
<script> element.</p>
<hr />
<h2><object> Declarations
</h2>
<p>It is possible to create objects with session or application scope in Global.asa
by using the <object> tag.
</p>
<p><b>Note:</b> The <object> tag should be outside the <script> tag!</p>
<h3>
Syntax
</h3>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3">
<tr valign="top">
<td>
<pre><object runat="server" scope="scope" id="id"
{progid="progID"|classid="classID"}>
....
</object></pre>
</td>
</tr>
</table>
<br />
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3">
<tr>
<th align="left" valign="top" width="20%">Parameter</th>
<th align="left" valign="top" width="80%">Description</th>
</tr>
<tr>
<td valign="top">scope </td>
<td valign="top">
Sets the scope of the object (either Session or
Application)
</td>
</tr>
<tr>
<td valign="top">id </td>
<td valign="top">
Specifies a unique id for the object
</td>
</tr>
<tr>
<td valign="top">
ProgID </td>
<td valign="top">
An id associated with a class id. The format for ProgID is
[Vendor.]Component[.Version]
<p> Either ProgID or ClassID must be specified.</p>
</td>
</tr>
<tr>
<td valign="top">
ClassID </td>
<td valign="top">
Specifies a unique id for a COM class object.
<p> Either ProgID or ClassID must be specified.</p>
</td>
</tr>
</table>
<h3>
Examples
</h3>
<p>
The first example creates an object of session scope named "MyAd" by using the ProgID
parameter:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3">
<tr valign="top">
<td>
<pre><object runat="server" scope="session" id="MyAd"
progid="MSWC.AdRotator">
</object></pre>
</td>
</tr>
</table>
<p>
The second example creates an object of application scope named "MyConnection" by using the ClassID
parameter:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3">
<tr valign="top">
<td>
<pre><object runat="server" scope="application" id="MyConnection"
classid="Clsid:8AD3067A-B3FC-11CF-A560-00A0C9081C21">
</object></pre>
</td>
</tr>
</table>
<p>
The objects declared in the Global.asa file can be used by any script
in the application:</p>
<table class="ex" cellspacing="0" border="1" width="100%" cellpadding="3">
<tr valign="top">
<td>
<p>GLOBAL.ASA:</p>
<pre><object runat="server" scope="session" id="MyAd"
progid="MSWC.AdRotator">
</object></pre>
<p>You could reference the object "MyAd" from any page in the ASP application:</p>
<p>SOME .ASP FILE:</p>
<pre><%=MyAd.GetAdvertisement("/banners/adrot.txt")%> </pre>
</td>
</tr>
</table>
<br />
<hr />
<h2>TypeLibrary Declarations</h2>
<p> A TypeLibrary is a container for the contents of a DLL file corresponding to a COM object. By including
a call to the TypeLibrary in the Global.asa file, the constants of the COM object can be accessed, and errors can be better reported by the ASP code. If your Web application relies on COM objects that have declared data types in type libraries, you can declare the type libraries in Global.asa.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -