Active Server Pages Tutorial

Creating an ASP Page

Syntax

ASP pages are simply HTML pages containing server scripts, which are enclosed in the <% %> separators. The file extension .asp tells the server that the page contains ASP instructions. These instructions are given to the server using the properties and methods of five primary ASP objects:

  • The Response Object-Sends information from the server to the client.
  • The Request Object-Retrieves information from the client and sends it to the server.
  • The Session Object-Stores session information for individual users.
  • The Application Object-Stores application information, which can be shared among users.
  • The Server Object-Is used to access methods and properties on the server.

As an example, type the following code into Notepad (or the authoring tool of your choice):

<% Response.Write(“Welcome to my first ASP page!”) %>

Save the file as test.asp. Be sure you change the default .txt extension to .asp. Now open your browser and type the following address into your address bar: http://localhost/LearnASP/test.asp. You should see the following: ASP code can be mixed with HTML code to create and format both static and dynamic elements on the page. For example, add the tag to the word “first” in the test.asp page:

<% Response.Write(“Welcome to my first ASP page!”) %>

When the page is previewed again, the word “first” appears in bold. ASP code can appear in long blocks or in short snippets in the body of a block of HTML code. In the first case, HTML tags may be used in the ASP code (in quotation marks), as shown above. However, when you only need to use a small amount of ASP code, to display the value of a variable, for example, you can integrate the ASP code into a longer HTML block by preceding the ASP statement with an equal sign (=):

Welcome, <%=Request.QueryString(“UserName”)%>!

The equal sign instructs the server to display the result of the code on the page.

About Dynamic Content

If you view the source for the test.asp page (in Internet Explorer, select View > Source), you’ll see one characteristic of ASP code: the output, rather than the code, is displayed in the source.

This is because the page is dynamic-it is created each time the page is requested and contains whatever HTML the server has told it to render based on the instructions you provided in the source code. What the viewer sees is only the output of that instruction. This makes ASP safer than HTML for processing sensitive data like user login information.

Keeping the Page out of Cache

Most browsers cache content locally for improved performance. This is disaster for an ASP page, since the server is rendering different HTML in response to actions that the user takes. To prevent browsers from caching an ASP page, include a PRAGMA NO-CACHE line in the header:

<!–esponse.Write(“Welcome to my first ASP page!”)–>

The PRAGMA NO-CACHE line instructs the browser not to cache the page but to request it from the web server. Even though the page name test.asp is the same, the content is probably different, so the browser must be instructed to fetch the page from the server each time.

Using Scripts

ASP uses a scripting language to give instructions to the server. The default scripting language is VBScript, but you can also use JavaScript, Jscript (the Microsoft version of JavaScript) or Perl, among others. To change the default scripting language, you must include a language specification line at the top of the page:

<%@ language=”javascript”%>

Variables

A variable is a container for a value. In VBScript, all variables are variants, or untyped variables, meaning that any sort of value-numeric, text or an object-can be stored in a variable. This diverges from most programming environments, which use strongly typed variables such as Integer or String. If you plan to move to a more structured programming environment later, treat your variables as if they are typed to avoid developing habits you’ll regret later. You use the DIM statement to declare a variable. The values of text variables are enclosed in double quotation marks. You can also concatenate string variables with the & operator:

<% Dim FirstName, LastName, FullName FirstName=”Bob” LastName=”Robertson” FullName=FirstName & ” ” & LastName Response.Write(FullName) %>

Numeric variables don’t require quotation marks:

<% Dim A,B, C A=100 B=200 C=A+B Response.Write(C) %>

The scope of a variable refers to its accessibility by the application (ASP site). How you can access a variable depends on where you declare it in the application:

  • A procedure-level variable is declared inside a procedure and destroyed when the procedure ends. It can only be accessed from with that procedure.
  • A local variable is declared outside a procedure and can be accessed from anywhere on the page.
  • A session variable is accessible by all pages in a site but applies only to a single client (user). In ASP, sessions are created when a user enters a site and destroyed when the user fails to access a page on a site in a specified period of time. Session variables typically store information about that user.
  • An application variable is accessible by all pages in a site and can be used by all clients accessing the site.

Leave a Reply

Your email address will not be published. Required fields are marked *