You are on page 1of 5

The objective of this tutorial is to introduce Active Server Pages (ASP) to individuals who are responsible for developing,

building, managing or maintaining a Web site. We will define ASP, differentiate between ASP and conventional HTML, demonstrate how to create and convert Web application pages to ASP, and demonstrate how to use ASP for further Web applications. This will involve using JavaScript or Visual Basic Script (VBScript) to write applications that can be run on an ASP Web server. It is assumed that the reader has some knowledge of Web servers, HTML, and VBScript. (Note: If you are viewing this tutorial from a downloaded zip file, some links will not work.) Introduction to Active Server Pages What is an Active Server Page? An Active Server Page is a web page that "executes" in a server-side execution environment in Microsoft Internet Information Server (IIS) 3.0, or higher, and that uses ActiveX scripting, normally VBScript or JavaScript code. When a browser requests an ASP page, the Web server generates a page with HTML code and transmits it back to the browser. An Active Server 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 on your 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. ASP is a component of the latest version of IIS, which is a component of the latest Windows NT Option Pack.

Internet Information Server (IIS) The foundation of ASP is Microsoft's Internet Information Server. IIS utilizes scripting engines for VBScript and other languages. VBScript is the default language of ASP, however you can use other scripting languages such as JavaScript or PerlScript. 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:\InetPub\wwwroot. 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: <script LANGUAGE="VBScript" runat="server"> </script> which are known as delimiter tags. You simply place your code between the delimiters. Your code may be as simple or as complex as need be. By default, ASP assumes the use ofVBScript. However, other scripting engines such as JavaScript can be used. 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: <%=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. <html> <head> <title>Date and Time</title> </head> <body> <% Dim dtmCurrent, intWeekDay, strWeekDay dtmCurrent = Date intWeekDay = WeekDay(dtmCurrent) If intWeekDay = 1 Then strWeekDay = "Sunday" ElseIf intWeekDay = 2 Then strWeekDay = "Monday" ElseIf intWeekDay = 3 Then strWeekDay = "Tuesday" ElseIf intWeekDay = 4 Then strWeekDay = "Wednesday" ElseIf intWeekDay = 5 Then strWeekDay = "Thursday" ElseIf intWeekDay = 6 Then strWeekDay = "Friday" ElseIf intWeekDay = 7 Then strWeekDay = "Saturday" Else strWeekDay = "Something obviously went wrong!" End If %> Today is <%=strWeekDay %> </body> </html> <html> <head> <title>Date and Time</title> </head> <body> <% Dim dtmCurrent, intWeekDay, strWeekDay dtmCurrent = Date intWeekDay = WeekDay(dtmCurrent) Select Case intWeekDay Case 1 strWeekDay = "Sunday" Case 2 strWeekDay = "Monday" Case 3 strWeekDay = "Tuesday" Case 4 strWeekDay = "Wednesday" Case 5 strWeekDay = "Thursday" Case 6 strWeekDay = "Friday" Case 7 strWeekDay = "Saturday" Case Else strWeekDay = "Something obviously went wrong!" End Select %> Today is <%=strWeekDay %> </body> </html>

The result of this page, when run on a Wednesday, is shown below.

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")

For example: <% Response.Write("Hello" & "<BR>") Response.Write("Hi There") %> produces the output "Hello" followed by "Hi There" on the next line. Notice the & "<BR>" 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" & "<BR>") places Hello <BR> in the HTML file. This, of course, produces a line break after the "Hello" when the HTML is displayed in the browser. Although <BR> is a tag recognized in HTML as a line break, ASP does not recognize <BR>. 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: <% @ LANGUAGE=script language %> 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.) For example: <% @LANGUAGE=JavaScript %> <% mydate = new Date() %> Display the date and time: <%=mydate %> Other Sample ASP Code Enter the following code now and save it in a file called test2.asp in your virtual directory. <html> <head> <title>Date,Time, and Display</title> </head> <body> WELCOME TO DevGuru's ACTIVE SERVER PAGES TUTORIAL <p> The time is <%= Time %> <p> Today's date is <%= Date %> <p> <% Dim intH intH = Hour(Now) If intH < 6 Then Response.Write "Do you know what time it is? " ElseIf intH < 12 Then Response.Write "Good morning! How are you? "

ElseIf intH < 17 Then Response.Write "Good afternoon! How are you? " Else Response.Write "Good evening! How are you? " End If %> <p> YOUR BROWSER TYPE IS <%= Request.ServerVariables("HTTP_USER_AGENT") %> </body> </html> You can view your page by entering http://localhost/test2.asp in your browser's location bar. Your page should look something like this:

You might also like