A Beginner’s Guide to Using Active Server Pages
It is assumed that the reader has some knowledge of Web servers, HTML, and VBScript.
Introduction to Active Server Pages
Page can combine HTML with scripts to create dynamic, interactive, and appealing Web pages. With ASP you can customize your Web pages to be more efficient and more receptive to your users. Some examples of what you can do with ASP include placing counters onyour Web site that count the number of times your Web site is accessed, and
creating a Web page that displays different information to different users.
The Differences between Active Server Pages and Conventional HTML
With conventional static HTML, the user types in a Web address and the browser sends a request for the Web page to a Web server. The Web server receives the request and retrieves the appropriate HTML file from a disk or memory and transmits it back to the browser. The user’s Web browser translates the HTML file and the results are
displayed in the browser window. The results are static as long as the underlying HTML page is unchanged.
Static Web pages are used when information is not expected to change much over time. Static Web pages are ideal for home pages or sites where content is not going to be modified frequently. In order to make changes to static Web pages, one must edit the HTML file.
ASP and conventional HTML work in almost the same way. One major difference between ASP and conventional HTML is that ASP is dynamic.This is because with ASP, scripting code runs on the server and produces HTML output, which is then sent to the browser for display. When a browser requests an ASP file from the Web server, the ASP
interpreter reads through the ASP file, executes any of the ASP commands contained within and sends the resulting HTML to the browser. This means that the same ASP page might produce HTML that appears in the browser differently each time it is requested. An ASP file can contain any combination of HTML and script. Since ASP pages produce standard HTML, they require no specific browser.
Creating and Converting Active Server Pages
You can create ASP files using any text editor or Web development tool. Just remember to convert your existing .html page to an .ASP page by changing the file extension. This informs the server to process the page and produce the HTML output proceeding sequentially through the file. Anything not delimited as script will be sent directly to the output; any scripts will be executed and their results sent to the output.
Web Server Installation
Before being able to run ASP pages, you need to confirm that you have an ASP-enabled Web server installed on your computer. If you have installed Windows NT or Personal Web Server (PWS) for Windows 9x on your system, you were most likely asked if you wanted to install Internet Information Server (IIS). After installing IIS, you should
have installed ASP. However, if ASP is not installed on your system, you can download the most recent version of ASP from our Resources page:
http://www.devguru.com/features/resources/resources.htmlASP is a component of the latest version of IIS, which is a component of the latest Windows NT Option Pack.
Internet Information Server (IIS)
In order to run your ASP page, you must place it in a directory that is accessible to the Web server. This is because the server must first interpret the ASP file so that it can run properly. If you simply try to open the ASP file in a browser, it will not work.The ASP file itself contains VBScript that we do not want to send to the browser. However, we do want to send the output of the interpreted ASP file to the browser. This is why an ASP page will
only appear correctly when a Web server sends it to the browser.
IIS’s default root of the web directory for Windows NT and Windows 9x is C:InetPubwwwroot. The directory that we will be using as the home for the web pages we create is InetPub. Files in this directory are accessed through your browser using the URL http://localhost/page.asp, where page.asp is the ASP file you wish to view.
If you wish to place your ASP page in a folder other than the default home directory, you will need to tell the Web server where your chosen folder is by designating it as a virtual web directory.
Simple ASP Coding
As stated previously, when a browser sends a request to the server for an ASP page, the server interprets the ASP before it is sent out. Your page could have text that says “good morning” when a client visits your site before noon, text that says ” good afternoon” to another client who first visits your site after noon, and “good evening” to the client who visits your site after the workday is over. In other words, your page is being created on the fly: it could be different each time the client sees your page.
When writing an ASP, you must differentiate between what is ASP code and what is HTML. You do this by using:
or by using:
Let’s look at a few examples of simple ASP coding.
To display the time you would insert <%= Time %> into your HTML page. Enter the following code now and save it in a file called test1.asp in your virtual directory.
Display the time:
You can view your page by entering http://localhost/test1.asp in your browser’s location bar.
Your browser should display the current time.
“Date” and “Time” are VBScript functions. Displaying the date follows the same convention <%=date %>.
We can even produce the actual day by using a slightly more complex If Then or Select Case structure. Below is an example of displaying the current day of the week by using the If Then or Select Case structure.
In general, <%=expression %> sends the value of the expression to the browser. It is also a very simple process to output text to the browser using the following alternate syntax:
Response.Write(“your statement here”)
produces the output “Hello” followed by “Hi There” on the next line. Notice the & “
” within the first response.write. The server is interpreting all statements between the <% %> delimiter tags before the client sees the page. Response.write tells the server to write its arguments directly to the HTML page being created. Therefore, response.write(“Hello” & “
“) places Hello
in the HTML file. This, of course, produces a line break after the “Hello” when the HTML is displayed in the browser. Although
is a tag recognized in HTML as a line break, ASP does not recognize
. Keep in mind that HTML is interpreted by the browser and ASP is, as stated earlier, interpreted by the server.
Response is one of seven intrinsic ASP objects, all of which are documented in the ASP Quick Reference on the DevGuru site.
There are numerous other scripting languages available besides VBScript. If you wish to use a language other than VBScript in your ASP page, you need to specify which script language you will be using. To do this place:
as the first line in your ASP page. (NOTE: you can leave a space between the @ character and LANGUAGE or not, either way will work.)
Display the date and time:
Other Sample ASP Code
Enter the following code now and save it in a file called test2.asp in your virtual directory.
You can view your page by entering http://localhost/test2.asp in your browser’s location bar.
DevGuru.com is a developer’s resource featuring comprehensive quick references for current technologies, free online tutorials, and “ask DevGuru,” the place to get your most difficult programming questions answered from leading experts around the world. Check
them out at http://www.devguru.com