Passing Data Between Pages
- Understanding the HTTP Protocol
- Retrieving Data from the URL
- Sending Data with Hyperlinks
- Setting and Retrieving Cookies
- What You Have Learned
The hallmark feature of dynamic Web pages is that the contents displayed in the browser are not coded in the page, as in static HTML, but rather are inserted into the page on the fly as it is served. The implication of this is that the core skill in developing dynamic Web applications consists of knowing how to capture and embed data, so the data can be inserted into Web pages when they are served.
At the end of the preceding lesson, you got a taste of this process, when you displayed the first name that users entered on a form on a different page. To accomplish this task, you used ASP, Adobe ColdFusion, or PHP code to capture the firstName variable and inserted it inline into regular XHTML code. Though it was a simple little application, the form-to-Web transfer you achieved at the end of Lesson 3 is representative of a good portion of dynamic site development.
You probably already know that there’s a lot more to dynamic Web site development than forms and form variables. You know that you can embed database content in a Web page, and you have probably also heard of cookies, which store little bits of information on client computers for later use. Building dynamic Web pages, then, usually means working with several different types of data, coming from different sources, and outputting them into a standard XHTML page.
In this lesson, you will explore two other ways of capturing and embedding data in Web pages (also called binding). In doing so, you will discover how similar each of these approaches to binding data is to the other, from the perspective of coding. And yet you will also see that each approach offers unique benefits. You will not do anything for Newland Tours in this lesson; nor will the files be particularly attractive or useful in themselves. But they will teach you quite a bit about the core skill of binding data to Web pages.
As you learn these approaches to binding data, you’ll also learn more about the Web’s HTTP protocol. The nature of this protocol shapes the ways we bind data to Web pages, and understanding HTTP basics takes a lot of the mystery out of the inner workings of dynamic Web pages. As the book progresses, in addition to form, database, and cookie data, you’ll learn several more ways to bind data to Web pages, and when and why to use each technique.
Understanding the HTTP Protocol
Pages on the Web are transferred using HTTP (the HyperText Transfer Protocol). This protocol specifies how users (or, in many cases, systems) make requests of servers over the World Wide Web, and how these servers respond to these requests. Understanding the basics of this protocol will help you understand how dynamic pages work.
At its core, HTTP is a transactional system. A client sends a request to a server, and the server sends a response back to the client. One part of the request is the URL, or Uniform Resource Locator. When you click a link in a browser, a request is sent to the server that contains the desired file.
What most people don’t realize is that the client’s computer sends a lot more to the server than simply the URL request. It also sends quite a bit of information about itself, including the browser (referred to as the user agent), username, IP address, file types it can accept (such as GIF and JPEG), and several sources of data. The request contains a header and a body; most of the information just specified is sent in the header. The reason people don’t realize this is happening, of course, is that the header is not visible to the user.
Once the server receives the request, it responds if it can. It looks at the requested document, and if that document has any server-side code on it (such as ASP VBScript, ColdFusion Markup Language, or PHP code), the server processes those instructions. When it is finished, it removes the server-side code and combines the output from the server-side code along with the XHTML in the document, and sends it all back to the client in the body of the response. The response, like the request, has a header, which contains information for the client system (such as the document size and type and the date and time of the response). About all a user can see of this transaction are the URL and the output XHTML page.
The following figure represents an HTTP transaction. Solid rectangles show documents visible to the user, while dashed rectangles represent documents hidden from the user.
If you wondered at the end of Lesson 3 exactly how test_form_processor.asp gained access to the data that the user entered in the form on that page, the request is your answer. The firstName variable and its value were sent in the body of the request message when you clicked the Submit button. In other words, the hidden portions of the HTTP request and response messages make it possible to send data values back and forth between the client and server, which makes them available to ASP, ColdFusion, or PHP scripts on the server. It also means, as you’ll see in this lesson, that the server can send data back to the client with directions to store the data in a cookie.
Before we start looking at how to take advantage of HTTP to facilitate dynamic Web development, there is one more vital behavior you need to know about the protocol: Once the server sends its response to the client, it forgets about the entire transaction. That is, if the client makes a second request of the server, the server has no idea that this is the same client who made a request a moment ago. For this reason, HTTP is known as a stateless protocol.
The statelessness of HTTP is a significant problem for those developing Web applications. If the Web forgets who you are in between every request, how can you get through the successive screens of an online shopping cart? If you have a multipart survey, how can you ensure that the data entered on page 1 is inserted into the database along with the data entered on page 4? You have seen online shopping carts and probably taken multipage surveys, so you know there are solutions to these problems.
It is hard to develop dynamic Web sites without an awareness of the request-and-response transaction and the statelessness of HTTP. Binding data to pages takes place within this context, and the differences between querystring (or URL) variables and form variables, GET and POST, and setting and retrieving cookies—all of which you are about to become familiar with—are considerably less obscure when you understand how they relate to HTTP.


