Lesson 3: Your first ASP page

From lesson 1 and 2, you now know a little about what ASP is, and you've installed (or have access to) a server. Now we are ready to begin making our first ASP page. We keep it simple and easy, but after you have gone through this lesson, you will understand much more about what ASP is and what you can do with it.

Basically, an ASP file is a text file with the extension .asp which consists of:

  • Text
  • HTML tags
  • ASP Scripts

You already know what text and HTML tags are. So let's look a little more at ASP scripts.

ASP Scripts

ASP scripts can be written in different languages. The examples in this tutorial are written in Microsoft Visual Basic Scripting Edition (VBScript), but could also be written in another language - eg. JScript.

Microsoft has issued detailed Documentationdocumentation for VBScript and Documentationdocumentation for ASP Objects. Throughout the tutorial, there will be many links to the documentation. The goal is that you become accustomed to looking up and finding answers to your questions. VBScript is so extensive that you can manage to learn all facets in this tutorial. But VBScipt is not difficult! On the contrary, VBScript is often very similar to plain English.

Let's get started with your first ASP page.

Example: Hello World!

Start by making an ordinary HTML document. But name the file default.asp and place it in the root of the site. On your computer (which is now a server), the path is c:\inetpub\wwwroot\default.asp.

The HTML code should look like this:


	<html>
	<head>
	<title>My first ASP page</title>

	</head>
	<body>

	</body>
	</html>

	
	

As you probably remember from lesson 1, ASP is all about writing commands to a server. So let's write a command to the server.

First, we need to tell the server when the ASP will start and end. In ASP you use the tags <% and %> to mark the start and end for the ASP codes that the server must execute.

Now try to add the following simple code snippet to your HTML code:


	<html>
	<head>
	<title>My first ASP page</title>
	</head>
	<body>

	<%   
	Response.Write "<h1>Hello World!</h1>"
	%>

	</body>
	</html>
	
	

When we look at the ASP document in a browser, it should look like this:

Illustration: Result in the browser

But it gets interesting when you look at the HTML code in the browser (by selecting "view source"):

Illustration: Viewing code

The ASP codes are gone! As you may remember from lesson 1, it is only the server that can see the ASP codes - the client (the browser) only sees the result!

Let's look at what happened. We asked the server to write <h1> Hello World!</h1>. In a more technical language, one would say that we used the object DocumentationResponse and the method DocumentationWrite to write a specified string to the client. But do not worry! In this tutorial we try to keep the technical language at a minimum.

Our first example is obviously not particularly exciting. But just wait! From now on, it's only going to be more and more interesting. Let's look at another example.

Example: Now!

Let's make the server write something else. We could, for example, ask it to write the current date and time:


	<html>
	<head>
	<title>My first ASP page</title>

	</head>
	<body>

	<%   
	Response.Write Now
	%>

	</body>
	</html>
	
	

Will look like this in the browser:

Illustration: Result in the browser

And the corresponding HTML code:

Illustration: Viewing code

Now things are getting interesting, right?

We make the server write the date and time when the ASP page is displayed. Note that if you refresh the page in the browser, a new time is written. The server writes the current date and time each time the page is sent to a client.

It is also important to note that the HTML code contains only the date - not the ASP codes. Therefore, the example is not affected by which browser is used. Actually, all functionalities that are made with server-side technologies always work in all browsers!

In the example, we used DocumentationNow - which is a function that returns the current date and time on the server.

Let's try to extend the example by writing both a string and a function - separated by & - it's done like this:


	<html>
	<head>
	<title>My first ASP document</title>
	</head>
	<body>

	<%   
	Response.Write "<p>Current time: " & Time & "</p>" 
	%>

	</body>
	</html>
	
	

Will look like this in the browser:

Illustration: Result in the browser

And the corresponding HTML code:

Illustration: Viewing code

As you can see, the function DocumentationTime returns the current time. There are several functions that relate to time and date. And that's exactly what we'll look at in the next lesson.



<< Lesson 2: Servers

Lesson 4: Working with time and dates >>