Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting started with ASP

__General FAQ__

Getting started with ASP

by  woja  Posted    (Edited  )
This FAQ contains information for ASP novices. It shows how ASP pages are built and explains what they do. This FAQ does not deal with more complex issues such as acessing databases and other data sources. If you need help with these, take a look at the FAQs for this forum where you will find more information (e.g., faq333-178).

Intended Audience

You should be familiar with programming principles and should have a knowledge of either [color green]VBScript[/color] or [color green]Javascript[/color] as well as a knowledge of [color green]HTML[/color]. Although ASP can be used to generate other types of data other than [color green]HTML[/color], this FAQ concentrates solely on [color green]HTML[/color] in order to make the examples as simple as possible.

Experienced users of ASP will note that I have oftened simplified concepts here and there are exceptions to some of the more general statements I make. This is intentional: this FAQ is for novices who could become bogged doen in too many "but"s.

Fundamental Concepts

ASP is a filter for data returned by a web server (usually IIS, but it is available for other platforms). When a client (such as IE) makes a request to a server for an ASP page, the page is procesed by the ASP filter before any data is returned to the user. The ASP page, like a normal HTML page is simply a text file. It is edited using an ordinary text editor or customised application for creating and editing ASP pages.

The ASP filter processies the file returning all text to the client except for any text between [tt][color red]<%[/color][/tt] and [tt][color red]%>[/color][/tt]. Note:: There are some other parts of the file that ASP processes, but they are not discussed here.

At its simplest, an ASP page can simply return some HTML:

[color red]Example 1 [ignore][NoCode][/ignore]: A very simple ASP page[/color]
Code:
   <html>
      <head>
         <title>A very simple ASP Page</title>
      </head>
      <body>
         <p>Hello, world.</p>
      </body>
   </html>

If this was all ASP could do, it would hardly be necessary.

Let's add some code which demonstrates how ASP works (although the result is no more interesting):

[color red]Example 2 [ignore][Javascript][/ignore]: Simple ASP code[/color]
Code:
   <%@LANGUAGE=JScript%>
   <%
      var strHello = "Hello, world.";
   %>
   <html>
      <head>
         <title>A very simple ASP Page</title>
      </head>
      <body>
      <%
         Response.Write("<p>" + strHello + "</p>");
      %>
      </body>
   </html>
[color red]Example 2 [ignore][VBScript][/ignore]: Simple ASP code[/color]
Code:
   <%@LANGUAGE=VBScript%>
   <%
      Dim strHello
      strHello = "Hello, world."
   %>  
   <html>
      <head>
         <title>A very simple ASP Page</title>
      </head>
      <body>
      <%
         Response.Write "<p>" & strHello & "</p>"
      %>
      </body>
   </html>

In this case the response sent to the client would be identical to that in [color red]Example 1[/color].

Note: The first line of each of the code exceprts in [color red]Example 2[/color] tells ASP which programming language to use. It is good practice to put this line first in all your ASP scripts even if you know what the default setting for the language is on your server.

ASP Objects

In addition to providing support for the scripting languag, ASP also makes visible a number of objects and their associated methods, properties and collections. These objects provide information about the request, response, server environment and persistent user session. This introduction would be overly large if we covered all the properties, collections and methods of these objects; indeed this bried introduction does not mention the [tt][color blue]Session[/color][/tt] and [tt][color blue]Server[/color][/tt] objects at all.

We have already seen the [tt][color blue]Response[/color][/tt] object in use and one of its methods ([tt][color blue]Write[/color][/tt]) in [color red]Example 2[/color], above; it should be relatively plain what that does.

As an example of the [tt][color blue]Request[/color][/tt] object, consider the following:

[color red]Example 3 [ignore][Javascript][/ignore]: Multi-language response[/color]
Code:
   <%@LANGUAGE=JScript%>
   <%
      var strParam = Request.QueryString("country");
  		var strHello = "";
      if (strParam == null) {
         // The query paramter is undefined
         strParam = "uk";
      }
   %>
   <html>
      <head>
         <title>ASP</title>
      </head>
      <body>
      <%
      	switch (strParam.toLowerCase()) {
      		case "usa":
      		   strHello = "Hey, dudes.";
      		   break;
      		case "france":
      		   strHello = "Bonjour, tout le monde.";
      		   break;
      		case "uk":
      		default:
      		   strHello = "Hello, world.";
      		   break;
         }
         Response.Write("<p>" + strHello + "</p>");
      %>
      </body>
   </html>
[color red]Example 3 [ignore][Javascript][/ignore]: Multi-language response[/color]
Code:
   <%@LANGUAGE=VBScript%>
   <%
      Dim strParam
  		Dim strHello
  		strParam = Request.QueryString("country")
  		strHello = ""
      If (strParam = "") Then
         ' The query paramter is undefined
         strParam = "uk"
      End If
   %>
   <html>
      <head>
         <title>ASP</title>
      </head>
      <body>
      <%
      	Select Case (LCase(strParam))
      		Case "usa"
      		   strHello = "Hey, dudes."
      		Case "france"
      		   strHello = "Bonjour, tout le monde."
      		Case "uk"
      		   strHello = "Hello, world."
      		Case Else
      		   strHello = "Hello, world."
         }
         Response.Write "<p>" & strHello & "</p>")
      %>
      </body>
   </html>
You would then use something like [tt][color blue][ignore]http://myserver.com/multilang.asp?country=usa[/ignore][/color][/tt] (this is not a link) to view the result.

That concludes this (very) brief introduction to ASP. Below you will find some links to other FAQs which can take you further.

Tips & Techniques

Just a couple of hints.
[ul]
[li]Always remember that the text between the [tt][color red]<%[/color][/tt] and [tt][color red]%>[/color][/tt] is only exceuted on the server. This can cause confusion when the generated page contains scripts in the same scripting language as the ASP page. The following example is in [tt][color red]Javascript[/color][/tt], but the same considerations apply to [tt][color red]VBScript[/color][/tt].
Code:
   <%
      var x = "This is the server";
      function myFunc() {
         return x;
      }
   %>
   <script type="text/javscript">
      var x = "This is the client";
      function myFunc() {
         return x;
      }
   </script>
   <body>
      <p onclick="window.alert(myFunc());">Click</p>
   </body>
This will cause a message box to appear saying "[tt]This is the client[/tt]", not "[tt]This is the server[/tt]".
[/li]
[li]When you just want to include that value of a vraible in your code, it is often simpler to use the special [tt][color red]<%=[/color][/tt] syntax:
Code:
   <p><%=myVar%></p>
is identical in effect to the following [tt][color red]Javascript[/color][/tt]:
Code:
   <p>
   <%
      Response.Write(myVar);
   %>
   </p>
[/li]
[li]Remember that you can use ASP to generate all the parts of the HTML page, including scripts and stylesheets.[/li]
[/ul]

Have fun.

Other relevant FAQs

The following FAQs provide links and answers to specific questions:
[ul]
[li]ASP basics for beginners - faq333-3048 (12 March 2003)[/li]
[li]General FAQ - faq333-2924 (07 January 2003)[/li]
[li]The ASP Concept - faq333-3615 (19 June 2003)[/li]
[li]ASP documentation/tutorials - faq333-1325 (05 December 2003)[/li]
[li]ASP: VBScript vs. JavaScript - faq333-2493 (15 April 2003)[/li]
[/ul]
These are the most recent and simple introductions I found in the FAQ list after a (I must adimit, very brief) search. Look in the FAQ list for this forum for other references: you never know someone may have answered your question already!

Why I wrote this FAQ

This FAQ came about as a result of my reading of serveral threads in this forum. There were many general questions like "Just getting started with ASP -- need help". I also found many examples of misunderstandings of how ASP works. Whilst there are several FAQs provinding links to answers to these questions (see abolve), I though it would be better to summarise the basic ideas in a single document which can be read immediately.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top