Active Server Page For DummiesBy Bill HatfieldChapter 1: So What's an Active Server Page? In This Chapter
Whats the big deal about ASP? It doesnt really let you do anything you couldnt already do before, right? So why bother taking on yet another new thing. There are way too many new things to figure out already! To answer these questions, in this chapter I show you what your non-ASP options are for creating interactive Web pages. Then I show you exactly what ASP is all about and how it works. Boring, Dumb, Static HTML Think about how a simple Web page works.
This is how the World Wide Web was originally conceived. A pretty simple idea. But it provided for a very easy way to access information and gave the page designer quite a bit of flexibility in laying out a page. Forms and CGI But the communication for static HTML works one way. There is no way to send information back to a Web server. To fix this problem, forms and CGI were created. Forms are HTML tags that allow Web page creators to include controls like edits, checkboxes, and radio buttons in their Web pages. That way, the user can enter information. It also provides a Submit button that sends the information off to the server. But now the server has to be smarter, too. It cant just get requests for pages and send out pages. The server has to know what to do with this form information when it gets it. Thats where CGI comes in. CGI stands for Common Gateway Interface. CGI makes it possible for the Web server to talk to another application that can handle the form information when its sent back. Often these CGI applications are written in a language called Perl. When the CGI application receives the form information, it can save it to a text file, store it in a database, or do whatever else the Web site administrator wants it to do. This system works great for simple
Guest Books and the like. But if you want to make your
Web pages really interactive, you may start running into
trouble. Server APIs Because of the problems with CGI forms slowing everything down, server APIs were born. API stands for Application Programming Interface. Microsofts server API is called ISAPI. It stands for the Internet Server Application Programming Interface (clever, no?). Like CGI, ISAPI allows the Web server to communicate with other applications running on the server machine. But ISAPI is much more efficient than CGI. It doesnt launch a separate program each time someone sends back information from a form. And it allows the application developer a lot more flexibility in how the server responds to the browser. But ISAPI doesnt solve all the problems. You still have to write separate computer programs that have complex interfaces to your Web server and that work closely with your Web pages. ISAPI isnt very intuitive and its difficult to create and maintain. Because of these problems, few businesses go to the trouble of creating truly engaging, interactive Web sites. Creating great Web sites with ISAPI just takes too much time, and most companies cant afford to dedicate a group of expensive programmers to the task of making their Web sites really, really cool. Lofty ASPirations Active Server Pages (ASP) solves all of the problems associated with CGI and server APIs. In addition to being just as efficient as ISAPI applications, ASP is simple to learn and easy to use. With CGI or ISAPI, you had to write a computer program in a language like Perl or C that had complex interface code connecting it to the server. Then you had to compile the application and associate it with the appropriate Web pages. With ASP, you simply write your
code in the HTML page itself. The HTML tags and the code
are side by side. You write the code in a simple
scripting language that is easy to learn and easy to use.
Then you save the page to your Web site and its
ready to go. No compiling and no complex interfacing! ASP as Easy as 1, 2, 3 So what happens when an ASP page is requested by the browser? It works like this:
This arrangement has lots of advantages:
What Does an ASP Page Look Like? If youre like me, you can only hear someone describe something for so long before you want to see it yourself. The code below shows you what a real, live ASP page looks like. <html> Most of this should look familiar. Ive identified a title for the page and used a first level head to display My Home Page at the top. But then comes the weird part. What are <% and %>? In ASP these are called delimiters. They set off the code from the rest of the HTML tags. That way youll always know which is which. If it is inside the delimiters, you know its code. Otherwise, its got to be HTML.
Its not too hard to guess how this page works, even if you havent done much computer programming. This code is written in VBScript and is very English-like. Time is a VBScript function that returns the current time (on the servers system clock). So first I check to see if the time is between 12:00 a.m. and 12:00 p.m.. If it is, the HTML after the Then is sent back to the browser: <h2>Good Morning! </h2><p> The next line is a continuation of the If..Then statement. It begins ElseIf. In other words, If that didnt work, try this. It checks to see if the time is after 12:00 p.m. and before 6:00 p.m.. If it is, this HTML is sent back to the browser: <h2>Good Afternoon! </h2><p> Finally, Else is a catch-all. If none of the other conditions worked, send this HTML back to the browser: <h2>Good Evening! </h2><p> The End If lets you know that the If..Then statement is over. The next line of HTML is displayed, no matter how the If..Then condition worked out: <h2>Im happy you could stop by </h2><p> Notice that there are five lines of HTML in the page. The first and last are always displayed. But only one of the middle three is displayed, depending on the time. Suppose the time is 7:00 p.m. and you go to this page. Your screen would look like Figure 1-1. ***asp0101.bmp Crop to the IE Window*** Figure 1-1: The page at 7:00 PM. If you chose View --> Source from the Internet Explorer menus youd see the HTML below. <html> As you can see, all the VBScript code has been stripped out. And only the appropriate HTML line in the If..Then statement was sent. From looking at this, youd have no idea that this was anything more than a simple HTML page that always says Good Evening!. For more information on If..Then and all the other VBScript commands, see Chapters 3 and 4. How Is ASP Different from Client-Side Scripting? Youve probably seen all the books on the market teaching JavaScript and VBScript. How are these different from ASP? Before ASP, you could use JavaScript and VBScript in your Web pages to do client-side scripting. Client-side refers to the browser and the machine running the browser, as opposed to server-side where the Web server is running. ASP is a way of doing server-side scripting. Client-side scripting works a little differently:
Client-side scripting and server-side scripting are similar in that they both allow you to write code right alongside your HTML and have that code executed when the page is requested. And often you can do the same thing using either client-side scripts or server-side scripts. There are some important differences, though.
The good news is that you
dont have to pick between client-side scripting and
server-side scripting. You can use both! Even in the same
page. In fact there are some techniques for using them
together that are very effective. Of course, you could check to make sure everything was filled in and looked good in your ASP page. But that means the user would have to wait for the bad data to be sent to the server, evaluated, and a message returned to the browser. Doing it on the client, before it is sent is much quicker and makes a lot more sense. How Is ASP Different from ActiveX Controls and Java Applets? You dont have to read much about the Internet before you see the words Java and ActiveX kicked around. They are two of the hottest topics in the Web development world. How is ASP different? The concept of Java applets was created by Sun Microsystems. ActiveX controls were created by Microsoft. Both are small programs that are downloaded as part of a page, just as a graphic would be. But when they are received by a client machine, they are executed in the browser and the result is displayed on the Web page. Simple ActiveX controls or Java applets may provide a control that the user can use to enter information, like a listbox. More complex controls or applets might allow you to explore 3-dimensional VRML worlds, or view an animation, or see live video as it is broadcast from a Web site. Client-side scripts are often used to coordinate several controls or applets on the page so that they work together. Using these technologies together, you can create something that works like a multimedia application or a game. Java applets are written in Java and ActiveX controls can be written using Visual Basic, C/C++, Java, or other languages. They allow the site developer to create complex programs that couldnt be created using scripting alone. You may wonder, if there are controls and applets that supplement scripts on the client-side, are there also controls or applets on the server-side that supplement ASP? The answer is yes. Server components are the topic of Chapter 9. The World of Internet Development The Internet is still a very young platform for application development. The growth of interest in the Internet both for personal and professional use has been staggering. This interest is bound to make the Internet a central focus almost all major new development in the future. But today we're still in the early stages and the standards and development environments and tools aren't nearly as well-defined as they are for stand-alone application software like Word Processors and Spreadsheets. This is both good news and bad news. The pioneers always have the biggest opportunity to leave their mark on the future. But they also often have to work with very crude tools. ASP, Java and ActiveX are the forerunners of the tools of the future, and they are the tools that will begin to turn the promise of a truly exciting, interactive Internet into a reality. |