INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

__General FAQ__

Getting started with ASP by woja
Posted: 12 Jan 04

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 VBScript or Javascript as well as a knowledge of HTML. Although ASP can be used to generate other types of data other than HTML, this FAQ concentrates solely on HTML 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 <% and %>. 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:

Example 1 [NoCode]: A very simple ASP page

   <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):

Example 2 [Javascript]: Simple ASP 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>

Example 2 [VBScript]: Simple ASP 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 Example 1.

Note: The first line of each of the code exceprts in Example 2 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 Session and Server objects at all.

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

As an example of the Request object, consider the following:

Example 3 [Javascript]: Multi-language response

   <%@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>

Example 3 [Javascript]: Multi-language response

   <%@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 http://myserver.com/multilang.asp?country=usa (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.

  • Always remember that the text between the <% and %> 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 Javascript, but the same considerations apply to VBScript.

       <%
          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 "This is the client", not "This is the server".

  • When you just want to include that value of a vraible in your code, it is often simpler to use the special <%= syntax:

       <p><%=myVar%></p>

    is identical in effect to the following Javascript:

       <p>
       <%
          Response.Write(myVar);
       %>
       </p>


  • Remember that you can use ASP to generate all the parts of the HTML page, including scripts and stylesheets.



Have fun.

Other relevant FAQs

The following FAQs provide links and answers to specific questions:

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.

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close