%@ Language=VBScript EnableSessionState=False %> <% Response.Expires = 0 LessonFile = Request.ServerVariables("SCRIPT_NAME") ScriptLanguagePreference = Request.Cookies("ScriptLanguagePreference") If ScriptLanguagePreference = "" Then ScriptLanguagePreference = Request.QueryString("ScriptLanguagePreference") If ScriptLanguagePreference = "" Then Response.Redirect "/iishelp/iis/htm/asp/iiselect.asp?LessonFile=" & Server.URLEncode(LessonFile) End If End If 'Determine the physical path for the current page and then remove the 'name of the file from the path (leaving just the directory). MainPath = Request.ServerVariables("PATH_TRANSLATED") Length = Len(MainPath) i = 0 Do Until (i = 2) Do While (Mid(MainPath, Length, 1) <> "\") Length = Length - 1 Loop i = i + 1 MainPath = left(MainPath, (Length-1)) Loop FilePath = left(MainPath, Length) + "\tutorial" 'Determine the virtual path for the current page and then remove the 'name of the file from the path (leaving just the directory). MainPath = Request.ServerVariables("PATH_INFO") Length = Len(MainPath) i = 0 Do Until (i = 2) Do While (Mid(MainPath, Length, 1) <> "/") Length = Length - 1 Loop i = i + 1 MainPath = left(MainPath, (Length-1)) Loop VirtFilePath = left(MainPath, Length) + "/tutorial" %>
Choose a scripting language for this lesson.
In this module, you will learn some ASP basics by creating your own ASP pages (.asp files). You will find the example files you are to use in these lessons in the <%= Request.ServerVariables("SERVER_NAME")%> Web server's Tutorial directory (<%=FilePath%>). Save the files you create in the Tutorial directory as well.
Important To save and view your work in this module, you must enable Write and Script Web server permissions for the <%= VirtFilePath %> virtual directory on the <%= Request.ServerVariables("SERVER_NAME")%> Web server (with Active Server Pages installed). For more information, see Setting Web Server Permissions.
The best way to learn about ASP pages is to write your own. To create an ASP page, use a text editor to insert script commands into an HTML page. Saving the page with an .asp file name extension tells ASP to process the script commands. To view the results of a script, simply request the page in a Web browser by using the HTTP protocol, that is http://<%= Request.ServerVariables("SERVER_NAME")%><%= VirtFilePath %>/filename.asp. In this lesson, you will create the popular Hello World! script by copying HTML and ASP scripting commands from this tutorial into a text editor. You can then view the scripts output with your browser after you save the file in the text editor.
The following HTML creates a simple page with the words Hello World! in a large font:
<HTML> <BODY> <FONT SIZE=7> Hello World!<BR> </FONT> </BODY> </HTML>
Suppose you wanted to repeat this text several times, increasing the font size with each repetition. You could repeat the font tags and HTML text, giving it a different font size with each repetition. When a browser opens the HTML page, the line will be displayed several times.
Alternatively, you could use ASP to generate this same content in a more dynamic manner.
<%@ LANGUAGE = "<%=ScriptLanguagePreference%>" %> <HTML> <BODY>
Notice that the very first tag is a special ASP tag that sets your default scripting language to <%=ScriptLanguagePreference%>. Always be sure to add this tag as the first line of all your .asp files.
If another user has previously created the Hello.asp file and completed this portion of the tutorial, replace the older version of Hello.asp with your newer version.
<% for (i = 3; i <=7; i++) { %><%Else%>
<% For i = 3 To 7 %><% End If %>
Script commands are enclosed within <% and %> characters (also called delimiters). Text within the delimiters is processed as a script command. Any text following the closing delimiter is simply displayed as HTML text in the browser. This script command begins a <%=ScriptLanguagePreference%> loop that controls the number of times the phrase "Hello World" is displayed. The first time through the loop, the counter variable (i) is set to 3. The second time the loop is repeated, the counter is set to 4. The loop is repeated until the counter <% If ScriptLanguagePreference = "JScript" Then %>equals<%Else%>exceeds<% End If %> 7.
<FONT SIZE=<% = i %>>
Each time through the loop, the font size is set to the current value of the counter variable (i). Thus, the first time the text is displayed, the font size is 3. The second time, the font size is 4. The last time, the font size is 7. Note that a script command can be enclosed within an HTML tag.
Hello World!<BR> </FONT> <% If ScriptLanguagePreference = "JScript" Then %><% } %><%Else%><% Next %><% End If %> </BODY> </HTML>
<% If ScriptLanguagePreference = "JScript" Then %>The JScript curly braces ({ }) enclose the content repeated in the loop (as long as the counter is less than or equal to 7).<%Else%>The VBScript Next command repeats the loop (until the counter exceeds 7).<% End If %>
<%@ LANGUAGE = "<%=ScriptLanguagePreference%>" %> <HTML> <BODY> <% If ScriptLanguagePreference = "JScript" Then %><% for (i = 3; i <=7; i++) { %><%Else%><% For i = 3 To 7 %> <% End If %> <FONT SIZE=<% = i %>> Hello World!<BR> </FONT> <% If ScriptLanguagePreference = "JScript" Then %><% } %><%Else%><% Next %><% End If %> </BODY> </HTML>
Some text editors automatically change the file name extension to .txt when you choose Text Format in the Save dialog box. If this happens, replace the .txt extension with the .asp extension before you click Save.
You should see a Web page with Hello World! displayed five times, each time in a larger font size.
Congratulations! You have completed your first ASP page. As you have learned, the process of creating an ASP page is simple. You can use any text editor to create HTML content and ASP script commands (enclosed in
<%and
%>delimiters) as long as you give your files an .asp file name extension. To test a page and see the results, you request the page in a Web browser (or refresh a previously opened page).
A common use of intranet and Internet server applications is to process a form submitted by a browser. Previously, you needed to write a program to process the data submitted by the form. With ASP, you can embed scripts written in <%=ScriptLanguagePreference%> directly into an HTML file to process the form. ASP processes the script commands and returns the results to the browser.
In this lesson, you will create an ASP page that processes the data a user submits by way of an HTML form.
To see how the .asp file works, fill in the form below. You can use the Tab key to move around the form. Click the Submit button to send your data to the Web server and have it processed by ASP.
We have created a form to request user information; you can find it in the file Form.htm in the Tutorial directory (<%= VirtFilePath%>):
<HTML> <HEAD><TITLE>Order</TITLE></HEAD> <BODY> <H2>Sample Order Form</H2> <P> Please provide the following information, then click Submit: <FORM METHOD="POST" ACTION="response.asp"> <P> First Name: <INPUT NAME="fname" SIZE="48"> <P> Last Name: <INPUT NAME="lname" SIZE="48"> <P> Title: <INPUT NAME="title" TYPE=RADIO VALUE="mr">Mr. <INPUT NAME="title" TYPE=RADIO VALUE="ms">Ms. <P><INPUT TYPE=SUBMIT VALUE="Submit"><INPUT TYPE=RESET VALUE="Reset"> </FORM> </BODY> </HTML>
Like all HTML forms, this one sends the data to the Web server as pairs of variables and values. For example, the name the user types in the First Name text box is assigned to the variable named fname. ASP provides built-in objects that you can use to access the variable names and values submitted by a form.
Now it's time to learn how a Web server processes the information it receives from an HTML form. For this lesson we also have created an .asp file called Response.asp that will manipulate and display data received from Form.asp, that is, after you add a few extra script commands.
<%@ LANGUAGE = "<%=ScriptLanguagePreference%>" %>
Remember, always add this tag as the first script line in you .asp file so that the Web server knows what language your using to write your script.
if (Request.Form("title").Count == 1){ Title = Request.Form("title")(1) }<%Else%>
<% Title = Request.Form("title")<% End If %>
If another user has previously completed this portion of the tutorial, this script command will already be in place underneath the "Tutorial Lesson" comment. Paste the copied script over the existing script, or copy an unused version of Response.asp from the Template directory to the Tutorial directory.
Your form submits three different variables or values to ASP:
fname
lname
title
ASP stores information submitted by way of HTML forms in the Forms collection of the Request object. To learn more about forms and objects, see Working with HTML Forms and Built-in ASP Objects.
To retrieve information from the Request object, you type the following:
Request.collection-name ("property-name"). Thus, Request.Form ("title") retrieves mr or ms, depending on the value the user submitted.
else{ Title = "" } LastName = Request.Form("lname") if (Title == "mr"){ Response.Write("Mr. " + LastName) } else{ if (Title == "ms"){ Response.Write("Ms. " + LastName) }<%Else%>
LastName = Request.Form("lname") If Title = "mr" Then %> Mr. <%= LastName %> <% ElseIf Title = "ms" Then %> Ms. <%= LastName %><% End If %>
If another user has previously completed this portion of the tutorial, these lines of script will already be in place. Paste the copied lines of script over the existing script, or copy an unused version of Response.asp from the Template subdirectory <%=FilePath%>\template) to the Tutorial directory.
The <%=ScriptLanguagePreference%> <% If ScriptLanguagePreference = "JScript" Then %> if...else <%Else%>If...Then..Else <% End If %> statement performs three different actions depending on the value of Title. If Title is mr, the user will be addressed as "Mr." If Title is ms, the user will be addressed as "Ms." Otherwise, the first and last name will be used to address the user. You display the value of a variable by using the expression <%= variable-name %>.
else{ Response.Write(Request.Form("fname") + " " + LastName) } }
<% Else %> <%= Request.Form("fname") & " " & LastName %> <% End If %><% End If %>
Again, if another user has previously completed this portion of the tutorial, these lines of script will already be in place. Paste the copied lines of script over the existing script, or copy an unused version of Response.asp from the Template subdirectory <%=FilePath%>\template) to the Tutorial directory.
The <%
If ScriptLanguagePreference = "JScript" Then
%>plus signs (+) join
<%Else%>ampersand (&) joins
<% End If %> the values of the variables into one string. The <%
If ScriptLanguagePreference = "JScript" Then
%>final } curly bracket
<%Else%>End If statement
<% End If %> ends the conditional statement.
Congratulations! You have activated your first HTML form for sending results to an .asp file. To learn about ActiveX server components, go on to Module 2: Using ActiveX Components.