VBScript in ASP
Where's the scripting in ASP? You have to add it. Essentially, the same ASP page has a mix of both VBScript code and HTML code. The VBScript code has to be added between two special tags: <% and %>. These tags tell IIS where the script code begins and ends, and allows it to distinguish between VBScript and HTML. Consider the following revision to our running example.
<HTML>
<BODY>
<%
Response.Write("It is now " & Now)
%>
<FORM ACTION="display.asp" METHOD="POST">
Computer name: <INPUT TYPE="TEXT" NAME="COMPUTERNAME"><BR>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
Believe it or not, that's a full ASP page. The key is in the VBScript line Response.Write("It is now " & Now), which is a line of VBScript that uses ASP's built-in Response object to output the current date and time to the Web page. Try running that-remember, save the page in an IIS folder and access the page by using your Web browser and the HTTP protocol-and you'll see what it looks like.
Here's how it works: When you request the file from the Web server, IIS locates the appropriate file on the hard drive. It loads the file into memory, and because the file has an .ASP filename extension, IIS hands it off to Asp.dll, which is an ISAPI plug-in that handles ASP pages. Asp.dll scans each line of the file. The first two lines obviously aren't code, because Asp.dll hasn't seen a <% tag; therefore, those lines are passed straight through to IIS, and IIS transmits them to your Web browser.
On the third line, ASP realizes that some script code is beginning. Asp.dll reads the fourth line, and executes it. The results of the code execution are passed to IIS and transmitted to your Web browser. On the fifth line of the file, Asp.dll stops looking for code and continues passing everything straight through to IIS.
TIP
If you're feeling lazy, you don't have to type out Response.Write; you can simply use the equal sign. <% = "Hello" %> is functionally the same as <% Response.Write "Hello" %>.
If you've loaded this page into your Web browser, right-click the page and select View Source from the context menu (assuming you're using Internet Explorer, of course). You should see something like this.
<HTML>
<BODY>
It is now 5/26/2003 9:54:54 AM
<FORM ACTION="display.asp" METHOD="POST">
Computer name: <INPUT TYPE="TEXT" NAME="COMPUTERNAME"><BR>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
Note that all of the HTML outside the <% and %> tags was transmitted to your Web browser unchanged. However, the code inside the tags isn't present anymore. Instead of the VBScript code and the Response object, we're simply seeing the output of the code. The code itself was never passed to IIS by Asp.dll, and so all the Web browser receives is a static Web page. That's exactly how ASP works.
Code is executed on the server and never passed to the Web browser. Anything output by the code is passed to the Web browser. Any HTML outside of the special ASP code tags is passed straight through to the Web browser.
What can you do with VBScript inside an ASP page? Virtually anything. Here are two important differences between traditional VBScripting and ASP scripting.
You don't have access to the intrinsic WScript object. That's because ASP is an independent host for VBScript, and doesn't provide the WScript object. Instead, you have access to the intrinsic ASP objects. There are five of them, but you'll spend most of your time using two of them: Response and Request.
You can continue to access WMI, ADSI, and many of the other objects that you've learned about in this book. You can use the entire VBScript language, including all of its functions, control-of-flow constructs, and so forth.
TIP
If this Web page programming stuff is still a little intimidating, don't let it be! You're basically going to use HTML pages that you can create in FrontPage or another WYSIWYG editor, and you're going to learn a grand total of two additional commands that will accomplish 90% of the work an administrative Web page needs. Everything else will just reuse the VBScript you've already learned!
|