ASP Basics

I will try to show you the basics of ASP. This is not meant to be a complete ASP tutorial, but rather tries to give you some basic understanding of how to get started developing in ASP. So here we go with the basics:
  • Give your files an .asp extension:

Unless otherwise configured, the server will only process ASP statements in files with the extension ".asp".

  • Enable scripting or execute permissions on directory with ASP files:

It's a common problem among rookie ASP developers to forget to enable scripting permissions and then spending hours trying to find errors in their code. Look in your IIS documentation for an explanation of how to do this.

  • Enclose your ASP statements between <% and %> symbols:

To distinguish your ASP statements from the HTML they are embedded in, the server needs to know where ASP code starts and ends. You place your ASP statements between <% and %> delineators so the server can make this distinction.

For example:

<B><% Response.write(someVar) %></B>

This example will write the current system date (via the ASP code between <% and %>) and make it bold (via the HTML tags outside <% and %>). To send the results of a function directly to the browser, you can use <% = and %> delineators. For instance, the above example could be rewritten as:

<B><% = someVar %></B>.

  • Use the Built-in "Server" Object to access Methods and Properties of the Server:

One of the most common usages of the Server Object is to create instances of Server components. This is done with the CreateObject method of the Server object. The following example creates an instance of ADO database connection:

<% Set cn = Server.CreateObject("ADODB.Connection") %>

  • Use the built-in Application Object to share information between all users of the application:

You can use this object to share information between several users of your application. The following example shows how you can store some value in Application collection:

<% Application("message") = "Hello World!" %> This value could be accessed by all the sessions currently using the application.

  • Store information throughout the User Session with the built-in Session Object:

You can pass information between pages user accesses within one session. For example, you can store the name of user in a Session Object on one page (for instance, a login page) and then retrieve it on another.

<% Session("visitorsname") = "Jack" %>...
Hello <% = Session("visitorsname") %> !

This will send "Hello Jack!" to the browser.

  • To access the information sent by the browser to the server, use the built-in Request Object:

You can access data submitted by the user, Cookies, Server Variables and more using the built-in Request Object. The following example shows how to output the value of an item "visitorsname" that was form submitted to the server with the POST method:

Hello <% = Request("visitorsname") %>!

  • With the built-in Response Object, you can control and actually send the data to browser:

You can write output to the browser, set Cookies, change the content type of output, Redirect the client to other pages and more by using this object. This example could be used to redirect the client to a login.asp page:

<% Response.Redirect "login.asp" %>

  • Use traditional flow control statements to implement your logic:

You can use JavaScript's if...then statements, loops and other language features to accomplish the logic you need. In the following example, a welcome message is shown if the user has verified that his/her age is at least 18 and a forbidden message is shown 5 times in different font sizes if they are underage:

<%
if (Request("age") >= 18) {
%>
     Welcome to the site!
<%
}
else {
     for (i = 1; i = 5; i++) {
%>
         
<FONT SIZE="<% = i %>">Forbidden!</FONT>
<%
     }
}
%>