Article
Handling Submitted Data with ASP
There are two main reasons why developers are keen to build Web sites with Active Server Pages (ASP):
- It adds interactivity without exposing your code to scrutiny – whether for security reasons or to protect your intellectual property – as JavaScript would.
- It helps take advantage of server-side resources (such as a database or credit card processing facilities) to enhance your site's functionality or make it easier to administer.
In this, the third article in SitePoint's series on ASP, we'll focus on the first of these two goals. We'll look at the Request and Response objects that ASP provides, and how to use them to receive information that a user submits in a form and generate a dynamically-calculated result in return. Since we have not yet covered the concepts surrounding objects in ASP before, I'll explain them as we go along to make sure you're up to speed.
This article assumes that you’re familiar with the concepts covered in the previous two articles in this series: Getting Started with ASP and ASP Language Basics. If you haven't read them, and are approaching ASP for the first time, I would suggest at least skimming them before jumping into this article.
If you're ready, let's get started...
Built-in ASP Objects
You've probably heard the term object oriented programming bandied about on occasion, whether in newspaper ads seeking to hire programmers or in some other discussion related to current programming trends. I'm sorry to say that writing dynamic Web pages in ASP isn't object oriented programming, strictly speaking. Most of the programming involved in the development of a typical ASP Web site is procedural in nature; that is to say, it involves the writing of a series of instructions to be executed one at a time, with the possibility of loops and conditional control structures like those we saw in the previous article in this series.
What makes ASP a little special, however, is that very little functionality is actually built into ASP itself. Rather, ASP is a simple framework that allows you to make use of libraries of software objects written in other object oriented languages (typically C++) to do most of the heavy lifting.
A software object is basically a combination of two things: properties and methods. An example of a typical software object in ASP might be a file stored on the server's hard drive. Properties of the file include its:
- name,
- path,
- MIME type (e.g. "image/gif" in the case of a GIF file), and
- size.
Methods are actions that can be taken using the object. To continue with our file object, methods might include:
- reading a line of text from the file,
- moving the file to a different directory, or
- deleting the file completely.
The properties and methods belonging to a particular object depend on the class (or type) of that object (i.e. every object of class 'file' will have a filename). The particular values of the properties, however, are unique to each object (i.e. each file has its own filename, which can be different from every other file out there).
Before I boggle your mind further, however, let me calm you down a little by saying that we’ve already used an object in the simple scripts that we’ve looked at so far. In fact, here's an extremely simple line of one of the first scripts we looked at. Can you spot the object?
Response.Write "<p>This is a test of ASP.</p>"
The object in this line is Response. This is a special, built-in object that’s available to all ASP scripts, and it allows you to write to and otherwise control the flow of data that is sent back to the browser in response to the page request. The Response object also has a counterpart called the Request object, which we'll look at in detail later in this article. In the above line, we used the Write method of the Response object to write a line of HTML as part of the page to be returned to the requesting browser. So the code Response.Write can be roughly read aloud as "In the Response object, invoke the Write method".
The string that makes up the rest of the line, "<p>This is a test of ASP.</p>", is a parameter that is being passed to the Write method. Parameters provide whatever information is needed for a method to do its job. In this case, the Write method needs to know what to write out, and the parameter provides that information.
Of course, there's more to the Response object than just this one method. Let's examine some other methods, as well as some of the properties that it supports.
Kevin began developing for the Web in 1995 and is a highly respected technical author. He wrote