Microsoft Active Server Pages (ASP) scripts, written in VBScript, allow you to create programs that are executed on the server side. From all the output generated, ASP scripts send only text and HTML tags to the client’s computer, where they are displayed in the browser window.
Using the ADO (ActiveX Data Objects) model, ASP scripts can access databases stored on the server, and using FSO (File System Object) they can work with text files stored there as well. This makes it easy to create various web applications such as virtual catalogs. In simplified terms, ADO can be described as a data access model optimized for web projects.
To use ASP, you need to install Microsoft’s Internet Information Services (IIS) web server to work with operating systems such as Windows XP, Windows Vista, or Windows 7.
Windows provides the ability to run server applications on the client’s computer. To do this, the server application file must be placed in the directory C:\Inetpub\wwwroot on the client’s computer. This is the root directory of the server, created by default during installation.
Clients connected to your computer via the HTTP protocol automatically access this directory. The directory contains the file iisstart.htm, which opens in the client’s browser when connecting to the server without specifying a file name.
To open an ASP file from your computer in a browser, you must enter:
http://127.0.0.1/filename.asp
or
http://localhost/filename.asp
where http://127.0.0.1 or http://localhost is a reserved IP address for connecting to a server running on the same computer from which the request originates.
Example 1: Greeting Based on Time of Day
As an example, let’s create the following code, which displays different greetings in the browser depending on the time the document is loaded.
To let the computer know which part of the ASP file should be executed on the server, you must use the <% statements %> tag, where statements are the server-side commands. To determine the document load time, the Time() function is used.
Steps:
- Verify that IIS is installed on your computer. If not, install it. For example, on Windows 7: go to Control Panel → Programs and Features → Turn Windows features on or off, and enable IIS Services.
- In Notepad, type the code and save it with the .asp extension, for example, 6-Hello.asp.
- Place the file 6-Hello.asp in C:\Inetpub\wwwroot.
- Launch iisstart.htm from C:\Inetpub\wwwroot, then in the browser’s address bar enter:
- http://127.0.0.1/6-hello.asp
- The browser window will display a greeting depending on the time of day).
- Right-click in the browser window, choose View HTML Source, and you will see the code that the client receives from the server.
Greeting. File 6-Hello.asp
<html> <body> <h1>Greeting</h1> <% If Time>= #04:00:00# And Time < #12:00:00# Then %> <h2>Good Morning!!!</h2> <% ElseIf Time>= #12:00:00# And Time < #18:00:00# Then %> <h2>Good Afternoon!!!</h2> <% ElseIf Time>= #18:00:00# And Time < #23:00:00# Then %> <h2>Good Evening!!!</h2> <% Else %> <h2>Good Night!!!</h2> <% End If %> </body> </html>
Greeting. Code returned to the client
<html> <body> <h1>Greeting</h1> <h2>Good Afternoon!!!</h2> </body> </html>
Example 2: Greeting Based on the Day of the Week
The following code displays greetings depending on the day of the week when the document is loaded.
The <% statements %> tag again marks the server-side code. The equal sign in <%=Variable%> is shorthand for the Response.Write method, which sends information from the server to the client.
Your first server-side script. File day1.asp
<%@ Language=VBScript %> <html> <h1>Today <%= Now %></h1> <% If Weekday(Now) = vbFriday Then %> <h2>Tomorrow the weekend begins!!!!</h2> <% ElseIf Weekday(Now) = vbSaturday Or Weekday(Now) = vbSunday Then %> <h2>Weekend!!!!</h2> <% Else %> <h2>Still have to work</h2> <% End If %> </html>
Example 3: Using Response.Write Explicitly
Here is the same script, rewritten using the Response object and its Write method.
Your first server-side script. File day2.asp
<%@ Language=VBScript %> <html> <h1>Today <% Response.Write Now %></h1> <% If Weekday(Now) = vbFriday Then Response.Write "<h2>Tomorrow the weekend begins!!!!</h2>" ElseIf Weekday(Now) = vbSaturday Or Weekday(Now) = vbSunday Then Response.Write "<h2>Weekend!!!!</h2>" Else Response.Write "<h2>Still have to work</h2>" End If %> </html>