ASP and the Web pages
We write ASP code as we write PHP code: we insert the code (VBSCript code,
JavaScript code, as we want) directly inside the HTML pages, where we want an operation to be performed. When
a Web browser asks for an ASP page, the Web server interprets and executes the code and returns the result as
an ordinary HTML page.
Their are two special tags we can use to insert ASP code in HTML pages:
HTML code
<%
default language code (mostly VBScript)
%>
HTML code
...
<%@ language="JavaScript" %>
<%
JavaScript code
%>
...
<SCRIPT language="VBScript" runat="server">
VBScript code
</SCRIPT>
...
|
The second example that uses the tags <% ... %> that allow people to tell what programming
language they want to use to write the ASP script of the page. It is then necessary to close tags after telling
the language we want (<%@ language="JavaScript" %>), even if we write some code immediately
afterwards, as it is showed on the example. Besides, this line must be the very first one of the asp code.
Including a file
As well as PHP, it is possible to include the content of any file inside an ASP page. This is performed
thanks to one of the following lines:
<!--#include file = "C:\InetPub\wwwroot\asp\my_file.inc"-->
<!--#include virtual = "/asp/my_file.inc"-->
|
Important note: do not add extra spaces in "<!--#include".
The difference between these two lines is set in the keywords file and virtual.
The keywork file means that the file to include must be found using an absolute path
regarding the local drives ("C:\..."). On the other hand, the keyword virtual means that
the file must be found regarding the root of the Web server, i.e. as we would access it with the HTTP
request http://www.mywebserver.com/asp/my_file.inc.
The difference between the PHP function include is that in ASP, the content of the file is included
only once, before the ASP engine has started to interpret the ASP code of the page. the include procedure
of ASP corresponds to the #include in C. If the file to include contains ASP code to be interpreted,
this code must by embrassed between relevant tags.
The global.asa file
The file called global.asa is a optional file to be placed in the root directory of the ASP
application. It is used to define ASP events (start and end of the application or sessions) as well as application
functions and variables.
We define an application as the set of functions the ASP pages of a Web site can provide. Starting
an application is equivalent to starting the Web server. The global.asa file is read and interpreted
only once when the application starts. All data defined in this file are therefore loaded in memory at the
beginning, once for all.
A global.asa file must start with the script HTML tags. Here is an example of
global.asa file:
<SCRIPT language="VBScript" runat="server">
' Application start event: the application starts,
' we initialize the visitors counter
sub Application_OnStart
Application("visitors") = 0
end sub
' Application end event
sub Application_OnEnd
...
VBScript code
...
end sub
' Session start event: we say when the session has started
' and we increment the visitors counter
sub Session_OnStart
Session("start") = now()
Application.Lock
Application("visitors") = Application("visitors") + 1
Application.UnLock
end sub
' Session end event: we decrement the visitors counter
sub Session_OnEnd
Application.Lock
Application("visitors") = Application("visitors") - 1
Application.UnLock
end sub
' Custom procedure
sub my_function
...
VBScript code
...
end sub
</SCRIPT>
|
Because it is defined in the global.asa file, the procedure my_function can
be instantly called in any script in any page of the ASP application. If this file implements an event and
if this event occurs, the associated code is executed. It is then possible to build a counter to count the
number of persons currently visiting the Web site using both ASP events Session_OnStart and
Session_OnEnd, as it is illustrated in the example. This counter may be used as follows:
<HTML>
<HEAD>
<TITLE>Visitors counter</TITLE>
</HEAD>
<BODY>
<P><%=Response.Write(Application("visitors")) %> visitors are currently
visiting this Web site.</P>
</BODY>
</HTML>
|
Using a global.asa file has some constraints:
- it is not possible to display data (
Response.Write is forbidden)
- we cannot include files (
<!--#include... is forbidden)
- in the events Application_OnStart and Application_OnEnd, we cannot but use the
Server and Application objects.
Likewise, only the Server, Application
and Session objects can be used in the Session_OnEnd event.
However, we can use any of the ASP objects in Session_OnStart.
printable format
|