April 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

An Active Server Pages Tutorial, Part 1

  • August 14, 2001
  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

SESSION 1

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
  1. Server receives the request for the ASP Page from the client
  2. Server loads the ASP Page and executes the Script and HTML in the page.
  3. A new HTML page is generated
  4. 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

The script in an ASP page can be written by using Visual Basic Script, JavaScript, or JScript (or ECMAScript). JScript is the Microsoft Version of JavaScript. You should use Notepad or any other text editor to write your code. The most ideal tool for coding ASP is Microsoft Visual Interdev. After typing your code, you should save the file with the extension .asp if you use Notepad. If you are using Microsoft Visual Interdev, your .asp file is automatically saved as a .asp file. (See the end of this Session for more information about this tool.)

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:

<% =total%>

Here, total is a VBScript variable. The value of the variable total will be displayed as output.

Example 1.1

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
<a href="http://localhost/Example1.1.asp">http://localhost/Example1.1.asp</a>
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.

SESSION 2

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:

  1. The server-side script is placed in the Head tag section. This is not at all compulsory.
  2. 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

SESSION 3

Data Types

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 %>

Analysis:

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.

SESSION 4

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.

The = operator assigns values to variables. The output of the above program will be Hello World.

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
  1. Names must begin with an alphabetic character.
  2. Names can't contain periods, spaces, and some special characters, such as # , ~ , /, etc.
  3. Names can't be longer than 255 characters.
  4. Reserved words (if, dim, for) can't be used as variable names.

Variable Scope

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.

Session 5

Built-in Functions

Though you cannot use the message box and input box functions found in VBScript in server-side scripting, there are some useful built-in functions which you can use in scripts. They are outlined in the chart given below. 

Function Usage Description
Date <% =date %> Returns the current date of the server
Time <% =time %> Returns the current time of the server
Now <% =now %> Returns both the date and time
Day <% =day(now) %> Returns the day of the current month 
Year <% =year(now) %> Returns the current year
Weekday <% =weekday(now) %> Returns the day of the week by name
Weekdayname <% = weekdayname(now) %> Returns the weekday by name 

 

In Part 2, we will show you Boolean functions in ASP, session and application methods, use of text files, and database access.

About the Author

Anand Narayanaswamy is a graduate, in commerce, from the University of Kerala. He is currently working as an instructor teaching Java, Visual Basic, and other Web technologies, such ASP and XML. Currently, Anand lives in Thiruvananthapuram, Kerala State, India. He can be reached via his Website http://www.learnxpress.com.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel