An Introduction To ASP
K. Yue, copyright 2000
Created October 23, 2000
Introduction
Embedded Scripting
Example:
helloworld.asp
<html>
<body bgcolor="#ccffff">
<% ="Hello, world from ASP! <hr>" %>
Bye from regular HTML code.
</body>
</html>
The <% ... %> is executed by the Web server and the following HTML contents are sent to the browser.
<html>
<body bgcolor="#ccffff">
Hello, world from ASP! <hr>
Bye from regular HTML code.
</body>
</html>
Example
date.asp
<body bgcolor="#ccffff">
The date is <% = Date %>.
</body>
Example
loop.asp
<body bgcolor="#ccffff">
<% for i = 6 to 12 %>
<div style="font-size:<% = i %>px">Hello
World</div>
<% next %>
</body>
Example
Time.asp (adapted from Microsoft)
<div style="color:green">
<% If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then %>
Good Morning!
<% Else %>
Good afternoon!
<% End If %>
</div>
TimeAlt.asp: using the Response.write method to write to the server.
<div style="color:green">
<%
If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then
Response.write("Good Morning!")
Else
Response.write("Good Afternoon!")
End If
%>
</div>
Example:
<html>
<% @LANGUAGE="PerlScript" %>
<body bgcolor="#ccffff">
<%
$Response->Write("Hello, world! <br>");
$Response->Write("The server time is : " . (localtime));
%>
</body>
</html>
Example
ScriptTag.asp
<html>
<head>
<script runat="SERVER" language="JSCRIPT">
function Dummy () {
Response.Write("This is a dumb way to write something.")
}
</script>
</head>
<body bgcolor="#ccffff">
<% Dummy() %>
</body>
</html>
Note that JScript is used.
<script language="Javascript">
<!--
client script
<% server script %>
client script
<% server script %>
client script
...
-- >
</script>
<!--#INCLUDE VIRTUAL|FILE="filename"-->
Example:
(SSI.asp)
<html>
<head>
<title>An example of SSI</title>
</head>
Here is a message from script:
<p>
<!-- #include file="Dummy.inc"-->
<% Dummy() %>
<hr>
<!-- #include file="ExampleFooter.inc"-->
</body>
</html>
(Dummy.inc):
<script runat="Server" language="JScript">
function Dummy () {
Response.Write("This is a dumb way to write something.")
}
</script>
(ExampleFooter.inc):
<!-- Footer -->
<div style="font-size=8px;color:blue">
<script language="JavaScript">
<!--
today = new Date();
document.write("Last Modified: " + (parseInt(today.getMonth())+1));
document.writeln("/" + today.getDate() + "/200" + today.getYear()
% 10);
// -->
</script>
</div>
<div style="font-size=8px">
Dr. Kwok-Bun Yue <br>
Associate Professor, Computer Science <br>
Chair, Computer Information Systems Program <br>
University of Houston - Clear Lake <br>
2700 Bay Area Boulevard <br>
Houston, TX 77058
</div>
A Brief Introduction to VBScript
Example:
<%
' Option Explicit
' If used, all variables must be declared.
'
Dim A ' Declare the variable A.
A = 10
Response.Write("A: " & A & "<br>")
' Convert to Date data type.
strToday = "October 25, 2000"
MyDate = CDate(strToday)
Response.Write("strToday: " & strToday & "<br>")
Response.Write("MyDate month: ")
Response.Write(Month(MyDate) & "<br>")
' String concatenation and line continuation.
longString = "This is a long" & _
"string
defined in two lines."
Response.Write("longString: " & longstring & "<br>")
Const PI = 3.141592654
Response.Write("PI: " & PI & "<br>")
' Illegal assignment: PI = 2.0
%>
Example:
<%
Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function
f = 100
Response.write(f & "F is " & Celsius(f) & " Celsius.<br>")
Sub Dummy()
Response.write ("Hello from Dummy.<br>")
End Sub
Dummy()
%>
Example:
<%
' Dictionary object
Dim courses ' Create a variable.
' Creating object using Server.CreateObject.
Set courses = Server.CreateObject("Scripting.Dictionary")
courses.Add "CSCI4230", "Internet Application Development"
courses.Add "CSCI4333", "Design of Database"
courses.Add "CSCI5333", "DBMS"
classnames = courses.Keys ' Get the keys.
For i = 0 To courses.Count - 1 ' Iterate the array.
Response.Write(classnames(i) & ": " & _
courses.Item(classnames(i))
& _
"<br>")
' Return results. & ": "
Next ' i
%>
Example:
' Using set for object reference assignment
' Using 'is' for object reference test.
Set a = courses
if (a is courses) Then
Response.Write ("a and courses refer to the same object<br>")
else
Response.Write ("a and courses do not refer to the same
object<br>")
end if
Dim b
' Invalid assignment: b = courses
' Invalid test: If (b is course)
' Both b and courses must be object
' reference for the 'is' test.
Set b = Server.CreateObject("Scripting.Dictionary")
if (courses is b) Then
Response.Write ("b and courses refer to the same object<br>")
else
Response.Write ("b and courses do not refer to the same
object<br>")
end if