Introduction to Active Server Pages
Active Server Pages (ASP) is one of the most exciting and interesting Internet technologies ever created by Microsoft. With ASP, developers can build dynamic websites very easily. A script running on the server receives a request from the client and generates a customized HTML page, which will be returned to the client. ASP is available with Microsoft IIS (Internet Information Server). It is available only with the Windows NT operating system. If you are using Windows 98 or 2000, you can use Microsoft Personal Web Server to test your ASP scripts.
Overall Working of an ASP Page
- Server receives the request for the ASP Page from the client
- Server loads the ASP Page and executes the Script and HTML in the page.
- A new HTML page is generated
- It is then returned to the user and viewed in the browser.
Since pure HTML is generated, it obviously works on browsers such as Internet Explorer and Netscape.
How to Write an ASP Page
An ASP page also contains HTML tags. So how will the server distinguishes between ASP script code and HTML tags? It is through the special <% ---- %> tags, called delimiters. The script within <%----%> executes on the Web server. The other type of delimiter is <% = ---%>, which is mainly used for output expressions. For example, consider the following script:
Here, total is a VBScript variable. The value of the variable total will be displayed as output.
Type in the following script in a text editor or Visual Interdev.
<body> <% for I = 1 to 7 %> <font size = <%=I%>>ASP IS GREAT!!!</font><br> <%next%> </body>
Save the file as Example 1.1 in the <a href="file:///C:/Inetpub/wwwroot">C:Inetpubwwwroot</a> directory. You should save all your ASP files in this directory so that the server can be able to locate the files and execute them. Otherwise, you can create a virtual directory and configure the server (creating alias) appropriately. Then you can access the .asp files using that alias name. Click on the Advanced options in the Microsoft Personal Web Server.
Now, it's time to fire up your Web browser to view the output. Open
your browser and type
in the address bar. If every thing goes well, you should view the text ASP IS GREAT seven times with different font sizes.
Note to Microsoft Interdev Users:
Click File - New File and select ASP page to type your script. This will add a new ASP file page for you. You will notice the unique coloring to the code which will automatically appear as you write your Script.
Getting Started Using ASP
Let's begin our discussion by examining a classic Hello World ASP program.
<html> <head> <title>Hello World</title> <% = "HelloWorld"%> </head> <body> </body> </html>Analysis:
- The server-side script is placed in the Head tag section. This is not at all compulsory.
- About <% & %>: They are script delimiters. Server-side script is generally enclosed within delimiters.
Now Consider this:
<% fontsize = 5 %>
This is a typical VBScript statement, except that the delimiters tell the server to execute the code. (The script tag is also used as a delimiter.)
How to Use the <Script> Delimiter
>You can easily embed server-side script in the <Script> tag. When you use the <SCRIPT> tag, you must include the RUNAT attribute, which you set to Server.
<html> <head><title>ASP</title> <script language = vbs RUNAT = server> response.write("Hello World") </script> </head> </html>
The Object Response is an intrinsic Active Server component that allows you to send output to the client. Another object request allows you to request info from the server.
Response Object ---> Server to Client
Request Object ---> Client to Server
VBS Supports only one data type, the variant. The variant data type can contain different types of data, depending on how it is used. The different data types represented by the variant are called subtypes, such as Boolean, Byte, Currency, Date, Double, etc.
Consider the following script:
1. <% = "25" +75 %> 2. <% = "<br>" %> 3. <% = "25" &75 %>
Line 1: It gives a value of 100. The + operator indicates arithmetic. VBS temporarily converts the string "25" to a number and performs the arithmetic operation.
Line 2: Indicates line break.
Line 3: Visual Basic script converts the integer 75 to a string and appends to the string "25".
Now Consider this script:
<% "vbs" +75 %>
This is an error. It will indicate type mismatch.
Variables in ASP
A variable is simply a location in memory that has been given a name and whose contents can be changed. Data is placed in the variable and subsequently accessed through the Variable name.
Consider the following script:
<% strgreeting = "Hello World" %> <% =strgreeting %>
The above statement declares the variable
strgreeting and assigns a string value to it.
= operator assigns values to variables. The
output of the above program will be
Implicit and Explicit Variables:
- Implicit Variables -- By simply using the variable like above.
- Explicit Variables -- Using Dim statement.
Dim greetings. Here, there is no type information, since all variables are variants in VBScript. You can declare multiple names like Dim, greetings, age, etc.
Including Option Explicit Statement:
If you include this statement and later try to implicitly declare a variable, VBScript will generate an "undefined variable" error message. See the code below.
<% option Explicit Dim greetings Greetings = "Good Morning" ---------------> Allowed Name = "David" ------------------------------> Not Allowed %>
How to Name Variables
- Names must begin with an alphabetic character.
- Names can't contain periods, spaces, and some special characters, such as
# , ~ , /,etc.
- Names can't be longer than 255 characters.
- Reserved words (if, dim, for) can't be used as variable names.
The two levels of scope in VBScript are script level and procedure level.
1. Script Level: A variable has script-level scope when it is declared outside a procedure. It can be used anywhere in the script after declaration. It is similar to a global variable. It exists until the script finishes executing.
2. Procedure Level: Variables declared within a procedure have procedure-level scope and are available only within the procedure. They are also called as local variables. They exist until the procedure finishes executing.
Private and Public Variables
Private: Creates variables that are available only in the script that declares them.
Public: Creates variables that are available to all scripts. You can use this only at the script level.