Page

1.1.3- Dynamic Web Pages and ASP

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 01. Getting Started With ASP.

Not tagged.
<< 1.1.2- Using Dynamic Web PagesChapter11.2.0- What Do I Need to Run ASP? >>

Dynamic Web Pages and ASP

So far, we've analyzed the difference between static and dynamic web pages, but we've barely mentioned the subject of this book – Active Server Pages. So, without further ado, here's a quick and simple definition:

 

Active Server Pages is a technology that allows for the programmatic construction of HTML pages just before they are delivered to the browser.

 

In other words, with ASP we can write a set of instructions that can be used to generate HTML just after the web page has been requested by a client, and just before it is delivered.

 

ASP is the perfect tool for any HTML writer to add to their toolkit, because (as we saw in the previous section, and will demonstrate throughout the book) it gives us the power and flexibility to generate fresher HTML, and ultimately to produce more spectacular, interactive, personalized, up-to-date web pages.

 

How can we describe ASP? It's not a language (in the sense that Pascal and C++ are languages) – although it does make use of existing scripting languages such as VBScript or JavaScript. Moreover, it's not really an application (in the sense that FrontPage and Word are applications) either. Instead, we describe ASP using a rather more ambiguous term, technology. ASP is a technology for building dynamic and interactive web pages.

What does ASP Code Look Like?

When a web author writes an ASP page, it is likely to be composed of a combination of three types of syntax – some parts ASP, some parts HTML tags, and some parts pure text. We save all these constituent parts of the ASP page in a file with an .asp extension.

 

The notion of mixing different types of logic (fragments of text, some HTML tags and some ASP code) within a single block of code is one that can be very useful in ASP – however, it can be one of the main stumbling blocks for ASP beginners. So, let's take a look at this breakdown with an example, to show that it's really not as tricky as it sounds.

 

The following table summarizes these three ingredients, their purpose and their appearance:

 

Type

Purpose

Interpreter

Hallmarks

Text

Hard-coded information to be shown to the user

Viewer's browser on their PC shows the text

Simple ASCII text

HTML tags

Instructions to the browser about how to format text and display images

Viewer's browser on their PC interprets the tags to format the text

Each tag within < > delimiters

Most HTML tags come in pairs (an open tag and a close tag), e.g. <TABLE>, </TABLE>

ASP statements

Instructions to the web server running ASP about how to create portions of the page to be sent out

Web site host's web server software with ASP extensions performs the instructions of the ASP code

Each ASP section contained within <% %> delimiters

ASP statements contain the structures of more 'traditional' programming languages, such as Visual Basic and Java, as they have features such as variables, decision trees, cyclical repetitions etc.

 

Let's take a look at a simple ASP page. At the moment, we don't need to be too concerned with exactly what it does – we'll learn all about that in good time. The main point here is that it's not too hard to distinguish the different elements of the ASP page. Anything that falls between the <% and %> markers is ASP script, and will be processed on the web server by the ASP script engine, after the ASP page is requested and just before it is delivered to the browser. The script code is shown on a gray background, and the HTML and text are on a white background:

 

<HTML>

<HEAD>

<TITLE>The Polite Web Server</TITLE>

</HEAD>

<BODY BGCOLOR="wheat">

<H1>Welcome</H1>

<FONT SIZE="3">

<B>This is the Polite Web Server, at <% = Time %> on <% = Date %></B></FONT><BR>

<BR>

<% If Hour(Now) < 8 Then %>

Do you know what time it is? I was still in bed!

<% Else

Randomize

intChoice = Int(Rnd * 4)

Select Case intChoice

 

Case 0 %> So, where do you want me to go today?

<% Case 1 %> Well, look who's back visiting us again!

<% Case 2 %> Hi there, and welcome to our site.

<% Case 3 %> It's raining here – would you like to play virtual checkers?

<% End Select

End If %> <BR>

<HR>

</BODY>

</HTML>

 

Note that ASP code is known as script. We'll be looking at the notion of scripts and script engines in more detail in the next chapter .

 

We've highlighted the script code that is executed by the server to make it easier to distinguish the ASP code from the plain HTML. The web server searches out the <%%> markers, and executes the code contained within to generate a pure HTML stream – which it sends back to the browser. Then, the browser can process the HTML and display the web page:

 

Chapter1_image006

 

Thus, the content of the resulting web page depends on the HTML that is generated by the ASP code. In this particular example, the effect of the script code is to generate HTML for the time and date that the page is requested, and then to make a decision (based on the situation) on what text will be sent to the browser as part of the HTML stream. Juxtaposing bits of ASP, HTML and text in this way is an effective way of getting the result we want.

 

ASP Code is Browser-Independent

There's one more lesson to learn from what we've seen so far. Because ASP code is executed on the web server, and generates pure HTML, the client machine doesn't need to provide any kind of ASP support at all. In fact, the web browser handles .htm pages and .asp pages in exactly the same way – because from the browser's point of view, the process involves sending a page request to a web server and receiving a stream of pure HTML:

 543636_pg18.jpg

 

As the diagram shows, the browser is blissfully ignorant of any ASP processing that might be happening on the server – it only ever gets to see pure HTML. So, your dynamic .asp pages are just as viewable in Internet Explorer, Netscape Navigator, and other browsers as their static .htm counterparts!

Advantages of Using a Server-side Technology

So, we've already stressed that ASP is processed on the web server to generate HTML, while HTML is processed solely on the browser. So what is the advantage of performing actions on the web server first?

The main advantages are that it:

 

  • Allows you to run programs in programming languages that aren't supported by your browser
  • Enables you to program dynamic web applications browser-independently, without recourse to client-side programming features such as Java applets, Dynamic HTML, ActiveX controls, all of which are browser specific
  • Can provide the client (browser) with data that does not reside at the client
  • Often makes for quicker loading times than with client-side dynamic web technologies such as Java applets or ActiveX controls, because in the end, you're actually only downloading a page of HTML
  • Provides improved security measures, since you can write code which can never be viewed from the browser

 

That's not to say ASP pages are perfect. For example, they increase the workload on the server, so if your web site becomes popular you may need to invest in more hardware – but this is true of any server-side technology, and ultimately many web developers decide that the benefits of server-side functionality outweigh any disadvantages.

<< 1.1.2- Using Dynamic Web PagesChapter11.2.0- What Do I Need to Run ASP? >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |