| << 1.4.0- Writing Our First ASP Example | Chapter1 | 1.4.2- Common Errors and Pitfalls with ASP >> |
The Punctual Web Server Example
You can follow these instructions to set up the web page; then we'll explain what's happening and look at some pitfalls.
Try It Out – The Punctual Web Server
1. Open a new text file using the Notepad text editor that comes with your machine. (We'll discuss other code editors later in the chapter, but Notepad will suffice for now.) Then type the following code in:
<HTML>
<HEAD><TITLE>The Punctual Web Server</TITLE></HEAD>
<BODY>
<H1>Welcome</H1>
In Webserverland, the time is exactly <% = Time %>
</BODY>
</HTML>
The main advantage of typing the code in by hand is that it's a good way to become familiar with the syntax and techniques used in the code. The main disadvantages are that it's time-consuming, and it's possible to introduce typing errors. So, for your convenience, the source code for this example – and all the examples in the book – is available (for free!) in a downloadable file from the Wrox website .
2. Now, save this code as a file – call the file punctual.asp, and ensure that you save it into the C:\inetpub\wwwroot\BegASPFiles directory that we created earlier in the chapter.
When you save the file, you should double-check that your new file has the correct suffix. It should be .asp – it's your opportunity to tell the web server that the page contains ASP script. Be aware that the text editor may consider .txt to be the default! So in the Save or Save As… dialog, make sure you change the Save as Type to read All Files, or All Files (*.*).
3. Now start up your browser, and navigate to http://my_server_name/BegASP/punctual.asp:
|
|
4. Click on the Refresh button: the displayed time will change. In effect, the browser is showing a new and different instance of the same web page.
5. Now on your browser, select View | Source (or View | Page Source, or similar, depending on which browser you're using) from the browser menu to see the HTML source that was sent from the web server to the browser. The result is shown below. You can see that there is no ASP script to be seen – the <% = Time %> ASP script has been processed by the web server and used to generate pure HTML, which is hard-coded into the HTML source that's sent to the browser.
|
Here, you can see the HTML that was sent to the browser when I Refreshed the page at 10.31:00am.
|
|
6. As we mentioned before, you can expect this to work in any browser – because the ASP is processed on the web server. If you have another browser available, try it out.
Easy wasn't it? (If you didn't get it to work first time, then don't rush off to email technical support just yet – have a little look at the next section, Common Pitfalls and Errors with ASP, first.) Now let's take a look at the ASP that makes this application tick.
How It Works
Of course, there is only one block of ASP in the whole program. It's enclosed by the <% and %> tags, on this line:
In Webserverland, the time is exactly <% = Time %>
This line tells the web server to go off and run the VBScript Time function on the web server. The VBScript Time function returns the current time at the web server. If the web server and browser are on different machines, then the time returned by the web server might not be the same as the time kept by the machine you're using to browse. For example, if this page is hosted on a machine in Los Angeles, then you can expect the page to show the local time in Los Angeles – even if you're browsing to the page from a machine in Cairo.
The Time function isn't unique to ASP: indeed, it's just a VBScript function, that's being run on the server.
This example isn't wildly interactive or dynamic, but it illustrates that we can ask the web server to go off and do something for us, and return the answer within the context of an HTML page. Of course, by using this technique with things like HTML forms and other tools, we'll be able to build a more informative, interactive interface with the user.
| << 1.4.0- Writing Our First ASP Example | Chapter1 | 1.4.2- Common Errors and Pitfalls with ASP >> |

RSS



