Introduction

Welcome to the book.

This is an ASP cookbook. Each chapter contains one or more recipes that work together in a useful way. Briefly:

  • Chapter 1 is basically a tutorial, with a couple of very simple scripts that add dates to HTML pages.

  • Chapter 2 has two recipes: a tip-of-the-day application (a quick and dirty way to add dynamic content to a Web page) and a guest book.

  • Chapter 3 has a complicated recipe for publishing the contents of a database onto a Web site.

  • Chapter 4 has a more complicated recipe that creates a Web site that lets you edit the database.

  • Chapter 5 has a recipe for a shopping cart. It's the most complicated recipe in this book and the one that's most likely to make you some bucks.

  • Chapter 6 gets simple again with a recipe for e-mail.

All of these recipes rely on Active Server Pages (ASP) and JavaScript, which is mostly what this introduction is about.

Before Active Server Pages

Tim Berners-Lee started things off with a great way to publish documents called HTML, which is a wonderful way to publish static documents.

Pretty quickly thereafter, something called CGI was added to Web servers to let programmers generate HTML pages on the fly. With CGI, when the Web server gets a request for page, it starts a program and hands the request to the program. If the program generates any output, that output is returned to the browser.

CGI is a pretty great way to do a lot of things, but it's a very program-ish solution, which is to say it's not very HTML-ish. When you look at a CGI program, most of the time, it doesn't look much like an HTML page, even though that's usually the end result. And if there's a lot of HTML in the program, a lot of the time, it has to be carefully wrapped in quotes to separate the program parts from the HTML parts, which can get tiring.

Too Many CGI Scripts in the Kitchen

Then there's the performance issues: Most CGI programs are written using scripting languages like Perl, which is ideally suited for this kind of thing.

The problem with this is that every time a Web browser asks for a Web page that's generated by a CGI program, a new process is launched on the server. Launching new processes is kind of slow and takes up a lot of resources. Programs with too many simultaneous CGI programs can get very bogged down.

There are, of course, ways around this, but they involve doing fancy technical things that are a lot harder than writing Perl scripts.

So it became clear that there needed to be an easier way to generate dynamic Web pages.

Mixing HTML and Scripts in One Place

The solution that has emerged to the HTML vs. CGI debate is to do both. Increasingly, Web servers are getting smarter, so that it's possible to drop little program snippets inside of Web pages instead of having to maintain programs and HTML separately.

This isn't the end-all, be-all for the Web. Small sites that don't need dynamic content should still be created with HTML.

Very large sites with millions of pages and complicated applications need to be built using complicated publishing systems and application servers.

But for a lot of things, being able to mix HTML and programming is an excellent solution. And that's what ASP is all about.

ASP is probably the best way to mix HTML and simple programming in a Windows environment. ASP pages can also be hosted on a UNIX server, using software from ChiliSoft. But most people use ASP with Windows NT and Windows 2000, because it's built into IIS, [1] it's easy to use, and it's powerful.

[1] IIS is the Microsoft Web server (Internet Information Server) included with Windows NT and Windows 2000.

ASP is not the only game in town. Other products that let you mix HTML and programming include PHP, Cold Fusion, and JSP. But if you're interested in building dynamic product on the Windows platform, you'll find that ASP is often the first choice for developing dynamic Web sites.

How to Add ASP Code to an HTML Page

If you're comfortable with HTML source code, adding ASP to your arsenal will feel like the logical next step.

Warniong

This book assumes that you are comfortable with HMTL source code. If you are not familiar with HTML source code, you will want to get a good HTML book to refer to while you work with this book.


Let's start with a real simple HTML page called hello_world.html:

     <html>
     <head><title>Hello World</title></head>
     <body>

     Hello World

   </body></html

If your Web server is IIS (you need IIS or a Web server that supports Active Server Pages for this to work), you can take this file and rename it hello_world.asp:

      <%@ Language=JavaScript %>
      <html>
      <head><title>Hello World</title></head>
      <body>

      <% Response.Write("Hello World") %>

      </body></html>

Three things have changed:

  1. Instead of ending with .html, the file now ends with .asp. This tells the Web server that there may be some code on the page. Instead of just giving the page to a Web browser when someone types in the URL of the page, the Web server checks the page for <% … %> tags first. If it finds any, it removes the tags and their contents, and replaces them with the output of any program that is inside of them.

  2. The first piece of code that the Web server finds is <%@ Language=JavaScript %>. This tells the Web server that the programming language is JavaScript. Because IIS, the Microsoft Web server, often assumes that ASP programs will be written in VBScript, it's always a good idea to tell IIS if you're using JavaScript.

  3. The next piece of code that the server finds (remember that it's looking for <% … %> tags) is the line <% Re sponse.Write("Hello World") %>. The Re sponse.Write() com mand is a lot like the print() com mand in many programming and scripting languages. In the context of ASP, this code tells the server to send the string "Hello World" to the browser.

The net result is that the .asp page and the .html shown above have exactly the same result: a Web page with the words "Hello World" on it.

Nevertheless, the two pages are very different. When a browser asks the Web server for an .html page, the server passes the page to the browser without much thinking about it. On the other hand, the Web server looks at .asp pages to check for small programs to run. This means that any or all of the contents of an .asp page can be generated on the fly. This book will explore how this can be useful.

Why JavaScript?

Most books about ASP use the VBScript scripting language. Rather than use VBScript, this book uses JavaScript, for a number of reasons:

  • Active Server Pages can be written in VBScript, JavaScript, as well as PerlScript. Because all work pretty much equally well for most things, deciding which language to use is pretty much a matter of taste.

  • I'm more used to working in JavaScript than in VBScript, so it's often easier for me to use JavaScript.

  • JavaScript is the only practical language to use for client-side programming. I find the idea of switching from one language to another annoying. By using JavaScript on the server side, I can use the same language on both sides. Of course, the browser environment is very different from the ASP environment. Still, using a single language keeps my life simple. Especially because I do hardly any client-side scripting, because it never seems to work right, and figuring out the difference between all the browsers is something I never seem to get around to.

  • JavaScript is an open protocol. VBScript is not.

  • There are a lot of JavaScript programmers out there and not a lot of resources for using JavaScript on the server side. When I got an opportunity to write a book on ASP, it seemed like a good opportunity to tip the balance the other way.

What's the Difference between JavaScript, JScript, and ECMA Script?

JavaScript first emerged on the scene as a scripting language for the Netscape browser. Later, when you could use JavaScript with Microsoft Internet Explorer, I think they called it JScript (at any rate, Microsoft often calls it JScript now).

Because Netscape and Microsoft couldn't agree on how JavaScript/Jscript should work, it was hard to do anything besides rollover buttons that would work reliably.

For the last couple of years, there's been something called ECMA Script that's supposed to be an open protocol that will bring everything back together or something.

Because all the code in this book was written for Microsoft Active Server Pages, it seems likely that one could argue that the code is JScript. Yet, this book talks mostly about JavaScript, rather than JScript. I've been able to find two reasons why: First, I think JavaScript sounds better than JScript; Second, the code that I use in my scripts to declare the language is <%@ Lan guage=Jav aScript %>. And this works.

Whatever.

ASP Objects and Other Useful Objects

The reason that ASP, IIS (the Microsoft Web server), and JavaScript are worth using is that they make it easy to create a script quickly, using a suite of tools that let us limit our focus to the specific task confronting us without having to reinvent the wheel.

When our script needs to perform a task, the bulk of the task can often be performed by a built-in ASP object, Microsoft scripting object, or JavaScript object. Some of the important ones are listed below.

A Quick Look at ASP

One of the tasks that used to be a real hassle in the early CGI days was collecting information from a form. For example, if a form collects a variable called email that contains an e-mail address, you can easily access it with the Request object:

   email_address = Request("email");

Another fun trick is the ability to redirect a visitor to a different page, using the ASP Response object:

Response.Redirect("http://www.lovejoy.com/redirect_page.asp");

which is a lot faster way to redirect someone than using a <meta> tag.

And, of course, there are a lot of other neat tricks that make it fun and worthwhile to work with ASP and JavaScript! Thus, this book…

A Quick Tour of the More Important Objects

As the name implies, ASP objects are collections of logically related methods and properties. The table below lists the most commonly used ASP objects and the purposes they serve.

ASP Object Purpose
Request Manages information about the request that the browser sends to the Web server. Often, the most important element of this request is incoming form information.
Response Manages information that is being sent to the browser in response to the browser's request to the Web server.For example, the Response.Write() command can be used to send HTML to the browser.
Server Manages information that is related to the server. For example, the Server object can be used to find the URL of the current script.
Session Manages information about a user's total visit. For example, if a user looks at five pages when visiting a site, the Session object can be used to keep track of that user as she looks at the five pages.

In addition to objects that are unique to ASP, it is possible to use Microsoft scripting objects from within the ASP environment. For example:

Microsoft Scripting Object Purpose
FileSystem Can be used to read and write information to and from the filesystem, including looking at the contents of existing files and creating new files.
ADODB A database technology that can be used to read and write information stored in databases such as Microsoft Access, Microsoft SQL Server, or Oracle.
CDONTS Contains an object called NewMail that can be used to send email from a script.

Finally, the JavaScript language includes some built-in objects that help with certain tasks. For example:

JavaScript Object Purpose
Date Does the legwork necessary to work with dates—who has time to keep track of days, months, years, etc…?
Math A variety of useful math tricks such as generating random numbers and rounding off real numbers.

How to Use This Book

This book consists of a series of fully working programs that can usually be set up in a matter of minutes. Unlike CGI scripts, where you often have to worry about permissions and other configuration issues, the ASP scripts in this book can be FTPd to just about any Windows NT or Windows 2000 server (or even a Win95/98 PC with personal Web server installed) and be up and running immediately.[2]

[2] Some of the scripts in this book require server-side configuration: the database scripts need something called a DSN, which is a database connection thingy. The scripts that write data to the file system will need permission to so. If you have your own Web server, this is easily done; otherwise, your ISP will do it for you.

If you're new to Web scripting/programming, it's probably a good idea to use Chapter 1 as a tutorial.

After that, this book can be used as a cookbook: Simply jump directly to the script that looks interesting or otherwise meets your needs, and you should be up and running quickly, whether you're a novice or an experienced programmer working with ASP for the first time.

The appendixes contain reference information on topics that relate in one way or another to getting ASP scripts up and running quickly and easily.

Conventions

All code examples are identified by the use of the Courier fixed width font. If there are line numbers next to an example of code:

1.// Some code examples
2. // have line numbers next to them.

1,2. The code will usually be followed by detailed explanations of how the code works.

Note that, in some cases, a line of code will wrap to the next line. That does not mean that there is a line break in the code! You can generally tell a wrapped line because there is no line number next to the part of the line that has wrapped. Putting a line break where a line wraps can cause some odd behavior that's surprisingly difficult to track down, so be careful with this one. Don't want to worry about line breaks? Don't! Go to the companion Web site and download the code: http://www.phptr.com/essential/.

How Chapters Are Structured

Each chapter is structured as follows:

  1. Introduction: Some kind of effort to give you a sense of what the chapter is about.

  2. New Features: A description of any new ASP or JavaScript features that will be used in the code.

  3. Source Code: The source code is listed, followed by a detailed description of how it works.

  4. Recap: A brief summary of what was in the chapter.

  5. Advanced Projects: Some ideas on how you could hack the script presented in the chapter.

This book assumes that you've got at least a passing familiarity with HTML. If you haven't worked directly with HTML before, you'll probably want an HTML resource to turn to.

Case Sensitivity

JavaScript is case-sensitive. sO be cAreful about how you type things. If something doesn't work right, you can explore changing the case…

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.243