Active Server Pages: Subroutines and Include/virtual



Subroutines

Subroutines have the same utility in ASP as it has in other languages.

In the next two examples, we have asked our visitors his name, and depending on that answer a different response is sent to the client. The response will be the same in both cases, but in the second one subroutines are used. The use of subroutines may be very useful when there are a lot of instructions to be perform within a subroutine. This way it will allow us to simplify the structure of our script.

Example 1

<% 
TheName=request.form("name)

if TheName="John" then
response.write ("Hi, John. How are you?")
response.write ("<br>Did you know I got married last month?")
else
response.write ("Hi. How are you?")
end if
%>

Example 2
<% 
TheName=request.form("name)

if TheName="John" then
ResponseToJohn()
else
ResponseToUnknown()
end if

Sub ResponseToJohn()
response.write ("Hi, John. How are you?")
response.write ("<br>Did you know I got married last month?")
End Sub

Sub ResponseToUnknown()
response.write ("Hi. How are you?")
End Sub

%>

In order to call a subroutine, we will use this kind of code:

Whatever()

Where Whatever is the name of the subroutine (it is recommended to use a very descriptive name of the task we want to perform within the subroutine to make it easier to understand the script). We may also provide information to the subroutine in order to perform the specified task. The data will be provided this way:

Whatever(data1, data2 ... dataN)

In the following example we will provide different  data to a unique subroutine depending on the Name of the person provided throw a form:

Example 3

<% 
TheName=request.form("name)

if TheName="John" then
ResponseToVisitor(35,Sue,New York)
else
   if TheName="Peter" then
   ResponseToVisitor(33,Sally,Los Angeles)
   else
   response.write("Who are you?")
   end if
end if

Sub ResponseToVisitor(AA,BB,CC)
response.write ("I know your are" & AA & "years old, ")
response.write ("you are married to" & BB & ", and")
response.write ("you are living in " & CC)
End Sub
%>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

In line 14 it is specified AA is the first variable to be get, BB the second one, and CC the third one. The values for the three variables are provided in the same order in line 5 or line 8.

The example above also shows subroutines are very useful to avoid repeating a specific number of tasks several times within the script, so that the script looks more organized and it is smaller.
 

Include/virtual

Server Site includes or SSI is a very simple programing language (see a small tutorial) but it also has a very limited number of instructions. We will consider only one option SSI allows us to use within our asp scripts: include/virtual.

In the next example we will use the include option of SSI in a our asp script (response.asp). This command allows as to add a set of instructions from different files (file1.txt and file2.txt bellow) and execute them.

Example 4

response.asp
<%
TheName=request.form("name)

if TheName="John" then
   %>
   <!--#include virtual="/file1.html" -->
   <% else %>
   <!--#include virtual="/file2.asp" -->
   <%
end if %>

File1.html
Hi, John.<br>
I know your are 31 years old, you are married to Sue, and you are living in New York.
File2.asp
<%
for i=1 to 3
response.write(Thename & "...<BR>")
next
response.write("Who are you?")
%>

 

In this case, if the name of the person who is visiting our page is John, then we will respond with file1.html. If not, then we will execute some asp instructions from file2.asp.

The include file must be a text file (.txt, .html, .htm, .shtml, .asp...). Although we have used file1.html and file2.asp, the script will work exactly in the same way with file1.txt and file2.txt (changing the name of the files would have no effect).
 

By using SSI and asp we may also get a secret page:
 

secret_page.asp
<%
UserName=request.form ("username")
Password=request.form("password")
if UserName="myusername" and Password="mypassword" then
%>
<!--#include virtual="/cgi-bin/secret_info.txt" -->
<% else %>
<Form Action=secretpage.asp method=post>
Username:  <input type=text name=username size=15><BR>
Password:  <input type=text name=password size=15><BR>
<input type=Submit Value=Send>
</form>
<% end if %>
secret_info.txt
This is my secret information:<BR>
My name is John.<BR>
My surname is Smith.<BR>
<BR>End of secret information.

In this case it is convenient to save secret_info.txt file in the cgi-bin directory (the .txt file is not accessible by visitors from this directory, but it will be accessible from our top directory).

 

Tutorial home page