ASP Lecture Notes
ASP Lecture Notes
ASP Lecture Notes
Server Pages
(ASP)
Uses of ASP
There are many things you can do with Active Server Pages .
You can display date, time, and other information in different
ways.
You can make a survey form and ask people who visit your site
to fill it out, send emails, save the information to a file, etc
Dynamically edit, change, or add any content of a Web page
Respond to user queries or data submitted from HTML forms
Access any data or databases and return the results to a browser
Customize a Web page to make it more useful for individual
users
The advantages of using ASP instead of CGI and Perl, are those
of simplicity and speed
Provide security - since ASP code cannot be viewed from the
browser
Clever ASP programming can minimize the network traffic
What is localhost?
To connect to a remote computer using its URL, you are
in effect calling it by its hostname.
For example, when you type in http://www.google.com/
you are really asking the network to connect to a
computer named www.google.com. It is called the
hostname of that computer
localhost is a special hostname. It always references
your own machine. So what you just did was to try to
access a webpage on your own machine.
For testing all your pages, you will need to use localhost
as the hostname
There is also a special IP address associated with
localhost, which is 127.0.0.1
http://127.0.0.1/ and would have received the same
page.
Scripting delimiters
Scripting delimiters <% and %>
wrapped around the VBScript code
these delimit the scripting code that
is executed on the server, not the client.
Script enclosed in scripting delimiters is
not sent to the client; it is processed by
the scripting engine.
Everything outside <% and %> is simply
written to the client.
Another way
<HTML>
<HEAD>
<TITLE>Hello, World !</TITLE>
</HEAD>
<BODY>
<%= Hello, World! %>
</BODY>
</HTML>
= sign just after the <%. It has a similar
effect to that of the Response.Write
statement.
<HTML>
<HEAD>
<TITLE>Hello, World !</TITLE>
</HEAD>
<BODY>
<%= Date %>
</BODY>
</HTML>
Function Date/Time
Using the function Date gives you the current date. And the
function, Time returns the time. To get both, use the function,
Now.
<HTML>
<HEAD>
<TITLE>Hello, World !</TITLE>
</HEAD>
<BODY>
<%
Response.Write Now
%>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Hello, World !</TITLE>
</HEAD>
<BODY>
<%
Response.Write Year: & Year (Now)
Option Explicit
<%
Option Explicit
Dim myVar
%>
Example
<%
Option Explicit
Dim Pi
Pi = 3.141592654
%>
Example
<html>
<body>
<%
dim name
name="Donald Duck"
response.write ("My name is: " &
name)
%>
</body>
</html>
My name is: Donald Duck
Declare an array
<html>
<body>
<%
Dim famname(5),i
famname(0) = "Jan Egil"
famname(1) = "Tove"
famname(2) = "Hege"
famname(3) = "Stale"
famname(4) = "Kai Jim"
famname(5) = "Borge"
For i = 0 to 5
response.write(famname(i) & "<br />")
Next
%>
</body>
</html>
Control structures
Conditional Statements.
Conditional statements are used to perform
different actions for different decisions.
In VBScript we have four conditional statements:
If statement - executes a set of code when a
condition is true
If...Then...Else statement - select one of two sets
of lines to execute
If...Then...ElseIf statement - select one of many
sets of lines to execute
Select Case statement - select one of many sets
of lines to execute
If..Then
If...Then...Else
If...Then...Else If
You can use the If...Then...ElseIf statement if you want to select one of many
blocks of code to execute.
<html>
<body>
<%
dim h
h=hour(now())
response.write("<p>" & now())
response.write("</p>")
If h<11 then
response.write("Good Morning!")
elseif
h=12
response.write("Good noon!")
elseif
h=13
response.write(Im having lunch!")
Else
response.write(Im sleeping!")
end if
%>
</body>
</html>
Select Case
You can also use the "Select Case" statement if you want
to select one of many blocks of code to execute.
<%
username=request.form("username")
Select Case username
Case "Peter"
Response.write ("Hello, Peter")
Case "John"
Response.write ("Hello, John")
Case "Joe"
Response.write ("Hi, Joe")
Case Else
Response write ("I do not know you")
End Select
%>
Looping Statements
Looping statements are used to run the same
block of code a specified number of times.
In ASP t we have four looping statements:
For...Next loop - runs code a specified number
of times
For Each...Next loop - runs code for each item
in a collection or each element of an array
Do While...Loop
While...Wend statement - Do not use it - use
the Do...Loop statement instead
<%
For Each Member in Team
Response.Write Member
Next
%>
Do While...Loop
<%
mynumber=0
Do While mynumber<10
response.write("Hello<HR>")
mynumber=mynumber+1
Loop
%>
Do Until....Loop
Subroutines
Subroutines are defined via the Sub keyword.
<%
Sub SayHello
Response.Write Hello !
End Sub
%>
Example
<html>
<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head>
<body>
</body>
</html>
Functions
Example
<%
Function Calculate (A, B, Op)
Select Case Op
Case +
Calculate = A + B
Case -
Calculate = A - B
Case *
Calculate = A * B
Case /
Calculate = A / B
End Select
End Function
<%
Response.Write Calculate(2, 3, +)
Response.Write Calculate(2, 3, -)
%>
Request object
The Request object is commonly used
to access the information passed by a
get or post request. This information
usually consists of data provided by the
user in an XHTML form. The Request
object provides access to information
(such as cookies) that is stored on
aclients machine. This object also
provides access to binary information
(e.g., afile upload).
Response object
The Response object sends
information, such as XHTML or text
to the client.
The Response object is used to send
information to the user. The Response
object supports only Cookies as a
collection (to set cookie values). The
Response object also supports a
number of properties and methods
Server object
The Server object provides access to
methods and properties on the server
The Server object provides access to
methods and properties on the server.
Most of these methods and properties
serve as utility functions.
Processing Forms
GET and POST
Form is submitted may be one of the
two methods: GET or POST.
Request.QueryString
<html>
<form method="get" action="simpleform.asp">
First Name: <input type="text" name="fname" /><br />
Last Name: <input type="text" name="lname" /><br
/><br />
<input type="submit" value="Submit" />
</form>
<body>
Welcome
<%
response.write(request.querystring("fname"))
response.write(" " & request.querystring("lname"))
%>
</body>
<html>
<head>
<form method="post" action="myform.asp">
First Name: <input type="text" name="name1" /><br />
Last Name: <input type="text" name="email1" /><br /><br />
<input type="submit" value="Submit" />
</form>
</head>
<body>
Welcome
Your name is <% =Request.Form("name1") %> <BR />
Your email is <% =Request.Form("email1") %>
</body>
</html>
<html>
<body>
<form method="post" action="simpleform2.asp">
FirstName: <input type="text" name="fname"></br >
SeconName: <input type="text" name="sname"></br >
Email: <input type="text" name="email"></br >
Password: <input type="text" name="pword"></br >
<p>Please specify your Gender:</p>
Male<input type="radio" name="gender" value="male">
Female<input type="radio" name="gender" value="Female">
<p><h2>Which package would you like</h2></p>
Adventure<input name="package" type="checkbox" value="adventure" />
Surfing<input name="package" type="checkbox" value="seasurfing" />
<p><h2>Select your continent</h2></p>
<select name="continent">
<OPTION selected="selected">select-continent</OPTION>
<OPTION>South America</OPTION>
<OPTION>North America</OPTION>
<OPTION>Australia</OPTION>
<OPTION>Artica</OPTION>
<OPTION>NewZealand</OPTION>
<OPTION>Europe</OPTION>
<OPTION>Asia</OPTION>
</SELECT>
<input type="submit" value="Submit">
</form>
Welcome
<%
response.write(Request.Form("fname"))
response.write(" " & Request.Form("sname"))
%><br>
Your Email address is <%response.write(Request.Form("email"))%>
and your password is <%response.write(" " & Request.Form("pword"))%><br>
Your are a <%response.write(Request.Form("gender"))%><br>
Your favourite game is <%response.write(Request.Form("package"))%>
and you come from <%response.write(Request.Form("continent"))%>
</body>
<html>
form_response.asp
<html>
<head><title>Responding to a
form</title></head>
<body>
Your name is <%
=Request.Form("name") %> <BR>
Your email is <%
=Request.Form("email") %>
</body>
</html>
Creating a Variable
you may want to create a variable and insert that variable
in different places of your response page
<% TheName = Request.Form("CatName") %>
<form action="demo_reqquery.asp"
method="get">
Your name: <input type="text" name="fname"
size="20" />
<input type="submit" value="Submit" />
</form>
<%
dim fname
fname=Request.QueryString("fname")
If fname<>"" Then
Response.Write("Hello " & fname & "!<br />")
Response.Write("How are you today?")
End If
%>
Another example
<html>
<%
dim cars
cars=Request.Form("cars")
%>
<body>
<form action="demo_radiob.asp" method="post">
<p>Please select your favorite car:</p>
<input type="radio" name="cars
<% if cars="Volvo" then Response.Write("checked")%>
value="Volvo">Volvo</input>
<br />
<input type="radio" name="cars
<%if cars="Saab" then Response.Write("checked")%>
value="Saab">Saab</input>
<br />
<input type="radio" name="cars"
<%if cars="BMW" then Response.Write("checked")%>
value="BMW">BMW</input>
<br /><br />
<input type="submit" value="Submit" />
</form>
<%
if cars<>"" then
Response.Write("<p>Your favorite car is: " & cars & "</p>")
end if
%>
</body>
</html>
nameandcolor.html
<html>
<head><title>Name and Color</title></head>
<body>
Example
backgroundform.html
<html>
<head><title>Chose background color</title></head>
<form action="backgroundresponse.asp" method="post">
Which color do you prefer to use as your background?
<BR>
<input type="radio" name="kindofcolor" value="defined" checked>
Defined color
<select name="definedcolor">
<option value="#FFFFFF">White</option>
<option value="#FF0000">Red</option>
<option value="#00FF00">Green</option>
<option value="#0000FF">Blue</option>
</select>
<BR>
<input type="radio" name="kindofcolor" value="custom">
Custom color
<input type="text" size="8" name="mycolor"></input>
<BR><input type="Submit" value="Submit"></input>
</form>
</body>
</html>
backgroundresponse.asp
<%
kindofcolor=Request.form("kindofcolor")
Select Case kindofcolor
case "defined"
colorofbackground=Request.form("definedcolor")
Select Case colorofbackground
case "#FFFFFF"
texttoshow="White"
case "#FF0000"
texttoshow="Red"
case "#00FF00"
texttoshow="Green"
case "#0000FF"
texttoshow="Blue"
End select
case "custom"
colorofbackground=Request.form("mycolor")
texttoshow="Custon color"
End select
%>
<html>
<head><title>Chose background color</title></head>
<body bgcolor="<% =colorofbackground %>">
<center>
<H1><% =texttoshow %></H1>
</center>
</form>
</body>
</html>
ADOIntroduction
ADODatabase Connection
ADORecordset
Recordset
Suppose we have a database named "students", we
can get access to the "studentmarks" table inside
the database with the following lines:
<%
set
conn=Server.CreateObject("ADODB.Connectio
n")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/marks/students.mdb"
set
rs=Server.CreateObject("ADODB.recordset")
rs.Open "studentmarks", conn
%>
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/marks/students.mdb"
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT * FROM studentmarks", conn
do until rs.EOF
for each x in rs.Fields
Response.Write(x.name)
Response.Write(" = ")
Response.Write(x.value & "<br />")
next
Response.Write("<br />")
rs.MoveNext
loop
rs.close
conn.close
%>
</body>
</html>
<html>
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/marks/students.mdb"
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Studentname, Marks FROM studentmarks"
rs.Open sql, conn
%>
<table border="1" width="50%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%
loop
rs.close
conn.close
%>
</table>
</body>
</html>
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/marks/students.mdb"
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Studentname, studentmarks FROM Customers"
rs.Open sql, conn
%>
<table border="1" width=50%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
</table>
</body>
</html>
ADOAdd Records
Enter Form
<html>
<body>
<form method="post" action="test2.asp">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr><tr>
<td>Course Name:</td>
<td><input type="text" name="cname"></td>
</tr><tr>
<td>Marks:</td>
<td><input type="text" name="marks"></td>
</tr>
</table>
<br /><br />
<input type="submit" value="Add New">
<input type="reset" value="Cancel">
</form>
</body>
</html>
ADOAdd Records
test2.asp"
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.mode=adModeReadWrite
conn.Open "c:/marks/students.mdb"
Set Recordset=Server.CreateObject("ADODB.Recordset")
sql="INSERT INTO studentmarks(StudentName,CourseName,Marks)
VALUES('"&Request.Form("name")&"','"&Request.Form("cname")&"','"&Request.Form("marks")
&"')"
on error resume next
conn.Execute sql,recaffected
if err<>0 then
Response.Write("No update permissions!")
else
Response.Write("<h3>" & recaffected & " record added</h3>")
end if
conn.close
%>
</body>
</html>
Customerdata.html
<html>
<body>
<form method="post" action="cust_process.asp">
<table border="0" width="40%">
<tr>
<td>CustomerID:</td>
<td><input type="text" name="custid"></td>
</tr><tr>
<td>Company Name:</td>
<td><input type="text" name="compname"></td>
</tr><tr>
<td>Contact Name:</td>
<td><input
type="text"
name="contname"></td>
</tr><tr>
<td>Address:</td>
<td><input type="text"
name="address"></td>
</tr><tr>
<td>City:</td>
<td><input type="text"
name="city"></td>
</tr><tr>
<td>Postal Code:</td>
<td><input type="text"
name="postcode"></td>
</tr><tr>
<td>Country:</td>
<td><input type="text" name="country"></td>
</tr>
</table>
<br /><br />
<input type="submit" value="Add New">
<input type="reset" value="Cancel">
</form>
</body>
</html>
cust_process.asp
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/marks/booking.mdb"
sql="INSERT INTO
book(customerID,companyname,contactname,address,city,postalcode,country)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("custid") & "',"
sql=sql & "'" & Request.Form("compname") & "',"
sql=sql & "'" & Request.Form("contname") & "',"
sql=sql & "'" & Request.Form("address") & "',"
sql=sql & "'" & Request.Form("city") & "',"
sql=sql & "'" & Request.Form("postcode") & "',"
sql=sql & "'" & Request.Form("country") & "')"
on error resume next
conn.Execute sql,recaffected
if err<>0 then
Response.Write("No update permissions!")
else
Response.Write("<h3>" & recaffected & " record added</h3>")
end if
conn.close
%>
</body>
</html>