If you have read my previous article titled "What are Active Server Pages?", you would of discovered a brief insight into the technology with a couple of senarios to when you might use them.
In this article we will take our theories one step further and actually show you some examples on how to use this exciting technology in your own web projects. Before we start, you will need to understand a few essentials in order to get you going.
To use ASP in your own web projects you will need to have a personal web server installed on your computer. If you don't use Microsoft Frontpage to build and maintain your websites, then you will have to download the Microsoft Personal Webserver from the following website address: http://www.microsoft.com/windows/ie/pws/. Be warned, the download is rather large and could take some time to come down.
Once the download has arrived onto your computer, its time to tackle the setup. When asked which components to install, it might be a good idea to install everything. The documentation is fairly useful especially when you are first starting to use the Active Server Pages technology.
Once you have everything installed you can check to ensure that everything is working ok by going to your own personal web page. For example, if your computer is called "stevens", which mine is, you can load up Internet Explorer or Netscape and type the following: http://stevens/. After pressing return you should be presented with your personal webserver homepage.
So whats actually happened so far? Well firstly we have made sure that you have a webserver installed to run your ASP web applications on. Without this installed, you web pages will not be able to be interpreted on the server side. Before we get into the nitty gritty of ASP, lets have an overview of how ASP is different from the traditional method of writing web pages.
Active Server Pages (ASP) is processed server side before writing anything to the client (the web browser). For example, you could have half of your ASP code in one section of your webpage and half standard HTML within the other half of your web page. When you actually launch your webpage, the ASP code is firstly interpretted by the personal webserver and then the HTML code, which is generated, is then displayed in your webbrowser.
So your next question might be. How does the personal webserver know the difference between an ASP page and a standard HTML page? Well quite simple really. As you know a standard webpage has a file extension of .HTML or .HTM. In order for an Active Server Page application to execute correctly it must have a .ASP extension. Your personal webserver will interpret this as a Active Server Page and hey presto, your ASP code is launched. If you have only HTML in your webpage you can still give it a .ASP extension. This is really good because when you are building your web projects, probably not all of your pages will have any Active Server Page code in whatsoever.
It's all quite simple and all will become clear shortly as we progress our introduction into Active Server Pages.
Whats Involved within an Active Server Page
The good thing about Active Server Pages is the ability to mix HTML and Active Server Code. You use standard HTML code as you would normally do when constructing your website but you have the ability to mix it with VBScript. VBScript is a subset of Visual Basic and it is really easy to learn if you have never programmed in Visual Basic before.
So what process does a normal HTML page go through when it is requested from the webbrowser:
  Reads the request from the browser
  Finds the page in the server
  Sends the page back across the Internet to the browser
The process if slightly different when requesting a Active Server Page from the webbrowser:
 Reads the request from the browser
 Finds the page in the server
 Performs any instructions provided in ASP to modify the page
 Sends the page back across the Internet to the browser
So the only difference here is the server performs any instructions provided in ASP within your webpage before it sends the page results back to the webbrowser. This really just shows you immediatly the difference between ASP and HTML. HTML is interpreted by the webbrowser and of which is not processed on the server. This gives you limited control within HTML pages.
This is where ASP really does give full control, therefor making your web pages truely dynamic.
An example of ASP
In order for you to progress comfortably with this discussion on ASP, it is required that you understand HTML to an extent of distinguishing it away from ASP code. The following piece of code includes some standard HTML and ASP code together. Take a look:
<html>
<head>
<title>Show me the Time</title>
</head>
<body bgcolor="#FFFFFF">
Hello, the time now is <% = Time %>
</body>
</html>
To test the above example, save it in your c:\inetpub\wwwroot\ directory as "time.asp". Now if you open up your web browser and type the following, you should see the page loaded with the correct time. Remember below that computername is the name of your computer as discussed before.
  http://computername/time.asp
Your web page should be loaded afer you press return and display the following:
Hello, the time now is 11:24:32
Obviously your time will be different from mine but the concept is clearly shown. So excellent, this is your first Active Server Page. It's very simple but it does demostrate some really important points. Firstly the ASP is seperated away from the HTML. As you can see we have an opening <% and a closing %>. Everything contained between these two tags contain ASP (VBScript). Remember that you cant just have 1 opening <% and no closing %>. You will get an error.
For the record, try changing <% = Time %> to <% = Date %> and see what happens. It should display the date.
Using Variables
In the following example we will store some information into a variable and then display it on the web page. Please type the following into a new page called "variable.asp".
<html>
<head>
<title>My Variable</title>
</head>
<body bgcolor="#FFFFFF">
<%
Dim strMyName
strMyName = "Bill"
%>
<%=strMyName%>
</body>
</html>
Save the file into your c:\inetpub\wwwroot\ directory and then load up your web browser and type the following:
  http://computername/variable.asp
Quite simply, you should see your name or the words "Bill" within a webpage. This example is quite simple. The DIM statement creates a variable called strName. The next line fills the contents of this variable with "Bill" or whatever you placed into that line. We then close our ASP section with the correct closing tag. We then use <%=strMyName%> to push the contents of this variable to the screen. It's using typical programming logic which can be found in all variants of the Basic programming language.
Let's change our previous example but this time include our age. In your editor, make the following changes to the code.
<html>
<head>
<title>My Variable version 2</title>
</head>
<body bgcolor="#FFFFFF">
<%
Dim strMyName
Dim strMyAge
strMyName = "Bill"
strMyAge = 23
%>
My name is <%=strMyName%>, i'm <%=strMyAge%>
</body>
</html>
When you save and run the page as discussed before, you should see the following on the screen.
My name is Bill, i'm 23
You could make the code above slightly more compact by replacing the two DIM statements with just a 1 line version. Try it and change the following from:
  Dim strMyName
  Dim strMyAge
to
   Dim strMyName, strMyAge
This will improve the look of your code while also keeping things to a minimum. This concludes this article for now. In the next part we will be looking more indepth at some of the features of ASP while also demostrating some excellent examples which you can use in your own projects. If you cannot wait until then, why not check out some of the following websites:
   ASP Today
   ASP 101
   4 Guys from Rolla
   15 Seconds
   ASP Free
   ASP Emporium
   Ultimate ASP
If you have any requests or comments, why not email me at
editor@beginners.co.uk.