Active Server Pages/Basic ASP Syntax
Objectives[edit | edit source]
In this section you will learn about the different parts of Active Server Pages scripting. We don't want to get too deep in discussion of how to write ASP scripts, but instead just want to introduce the different elements of ASP scripting.
Content[edit | edit source]
Active Server Pages or ASP is a scripted language which means that it doesn't have to be compiled in order to run. Instead, the program code is parsed, interpreted and evaluated in real-time. This means that you do not have to compile code in order to execute it.
Code is executed by placing it within a web page on a Web server. ASP was developed by Microsoft and because of this, is only completely reliable on Microsoft's IIS (Internet Information Server.) We encourage you to use Microsoft IIS to develop Active Server Page code.
Statements[edit | edit source]
A statement is like a sentence in English, it describes one task for the ASP interpreter to perform. A block of code is made up of multiple statements strung together on a web page.
There are various types of statements which we will talk about in the future including: "(flow) control, output, assignment, and HTTP processing.
In Active Server Pages, it is important to realize that each statement must appear on a line by itself. There is no special delimiter that indicates the end of an ASP statement. Unless you consider the carriage-return as the delimiter, which you really shouldn't.
You can use the colon character ":" within a program block to place multiple statements on the same line such as:
<% Response.Write "Error" : Response.End %>
In practice, it is best to avoid such programming constructs since it will make your code harder to read and maintain.
Another way to include more than one statement on a line is by using multiple code blocks like in the following:
<% If I < 0 Then %>Negative<% End If %>
Here, you have two different statements (even though they look like one statement) broken into two different code blocks. Active Server Pages allows this construct on your web pages since it is sometimes necessary to write code like this to eliminate white space that appears on your web page.
Variables[edit | edit source]
One of the hallmarks of a good scripting language is that the variables in the language are loosely typed. This means that there is minimal type-checking done on the values of the variables your program code uses. Active Server Pages was built correctly as a scripting language to be loosely typed. This allows you, as the programmer, to worry less about what type a variable is which frees you from a lot of work converting values and testing for the correct type.
You are not required to declare your variables before you use them (or even at all if you so wish). The one exception to this rule is when the page directive "Option Explicit" is in effect. If you place the following at the top of your ASP page, you will be required to declare all variables:
<% Option Explicit %>
If you don't want this requirement in all your scripts, you can leave it out. It is a useful tool to throw onto a page to check for mis-spelled or mis-used variable names. If you mis-spelled a variable, you will likely see "variable not defined".
Variables are declared with a Dim statement and can include a single variable at-a-time or a comma-delimited list of variable names like shown in the following example:
<% Dim sPhoneNo Dim sFirstName, sMidleInitial, sLastName %>
There is no such thing as a global variable in the ASP language. All variables are specific to the web page they are processed in. Once the page is done processing the values are lost. One exception to this is the Application object. It can hold a number of different values, each of which is associated with a string value. Another way is to pass variables in a web form or part of the URL in the Request object. More information on these topics will be covered later.
Comments[edit | edit source]
Comments are notations you can make on your web page within an ASP script block that will not be output on the web page. For all intents and purposes, they are hidden from your site visitors since they cannot view the ASP source code for your web pages. This allows you to make comments about what your code is doing.
Active Server Pages uses the quote character (') to define comments. This comment is a "line comment" meaning that it will comment out everything following it up until the end of the line. There is no multi line comment in ASP. In order to comment multiple lines, each line must be preceded by a quote (').
<% ' example comment - show the current date Response.Write Now() %>
Server-Side Includes (SSI)[edit | edit source]
Server-Side Includes allow you to retrieve code from one web page and insert it into another page. This allows you to create common pieces of code and HTML which only need to be maintained in one place. Other web pages can include this content within their pages using this server directive.
If you are using server-side include directives, you cannot place ASP code within the directive. This is because the directive is evaluated before the ASP code is interpreted. So trying to do something like the following is illegal:
<!-- #include virtual="/lib/<%= sLibName %>" -->
But this doesn't mean that you can't place ASP code inside an included file. So if you include a file called /lib/common.asp, you can place ASP code inside that file (just like you would a normal ASP page) and it will be evaluated by IIS and the results will be placed in the calling page.
An interesting note is that your main page can use a ".html" extension and include (via SSI) an ASP page and the included file will be interpreted and processed as ASP and included in the HTML page.
The two different types of Server-Side includes allow you to retrieve and include files based on the absolute path (based on the Web site root) or relative to the calling page as shown below;
<%' absolute path - based on web site root %> <!-- #include virtual="/pathtoinclude/include.asp" --> <%' relative path - based on folder of calling page %> <!-- #include file="../../folder1/include.asp" -->
There are benefits to using both. A good rule of thumb is this: if you expect you will be moving entire folders (or trees) of your web application then relative paths are more appropriate. If you expect things to stay pretty much where you created them then you should use absolute paths.
Please note: When using IIS6 (Windows 2003 Server), relative paths are not enabled by default, which will cause any Server-Side includes using relative paths to cease functioning.
Review Questions[edit | edit source]
- What are the three different types of script delimiters?
- What file extension must be used when writing an ASP web page?
- Which web server must be used to interpret ASP web pages?
- How do you declare a variable?
- How do you enforce the rule that all variables must be declared?
- What are the benefits of server-side includes?
- When should you use absolute server-side includes (SSI)?
- How do you terminate a statement in ASP?
- What is an "interpreted" scripting language?
- How do you write comments in Active Server Pages?
- How do you make "block comments" in ASP?
- Which database works best with ASP?
Exercises[edit | edit source]
Career Fair or Exhibition