Active Server Page (ASP)
What is ASP ?
Getting Started
The tools that you need to get started:
(1) Response Object
You can use the Response object to send output to the client.
Response.collection|property|method
Response Method
Response.Write |
Writes a variable to the current HTTP output as a string. |
Response.Redirect |
Sends a redirect message to the browser, causing it to attempt to connect to a different URL. |
Response.End |
Stops processing the .asp file and returns the current result |
Response.Clear |
Erases any buffered HTML output. |
Response.Flush |
Sends buffered output immediately. |
Response.BinaryWrite |
Writes the given information to the current HTTP output without any character-set conversion. |
Response.AppendToLog |
Adds a string to the end of the Web server log entry for this request |
Response.AddHeader |
Sets the HTML header name to value |
(2) Response Object
The Request object retrieves the values that the client browser passed to the server during an HTTP request
Request[.collection|property|method](variable)
Request Collection
Request.Form |
The values of form elements in the HTTP request body. (Post method) |
Request.Querystring |
The values of variables in the HTTP query string. (Get Method) |
Request.ServerVariables |
The values of predetermined environment variables. |
Request.Cookies |
The values of cookies sent in the HTTP request. |
Request.ClientCertificate |
The values of fields stored in the client certificate that is sent in the HTTP request. |
* Note : Please refer to MSDN Library Visual Studio 6.0 for further details.
Example 1 : Client-Server Application
Form.htm (Client Side)
<HTML>
<HEAD><TITLE>User Form</TITLE>
<script language="Javascript">
function VerifyInput(){
if(document.Form.name.value==""||document.Form.name.value==null){
alert("Please enter your name.");
document.Form.name.focus();
document.Form.name.select();
return false;
}
if(document.Form.address.value==""||document.Form.address.value==null){
alert("Please enter your address.");
document.Form.address.focus();
document.Form.address.select();
return false;
}
}
</script>
</HEAD>
<BODY>
<CENTER>
<H1>User Form</H1>
<HR>
<FORM NAME="Form" ACTION="Proces.asp" METHOD="POST" onSubmit="return VerifyInput()">
Name:<INPUT TYPE=TEXT NAME="name">
<BR>
Address:<INPUT TYPE=TEXT NAME="address">
<HR>
<INPUT TYPE=SUBMIT VALUE="Submit">
<INPUT TYPE=RESET VALUE="Clear">
</FORM>
</CENTER>
</BODY>
</HTML>
Proces.asp (Server Side)
<HTML>
<HEAD><TITLE>Proses</TITLE></HEAD>
<BODY>
<CENTER>
<H1>Result</H1>
<HR>
Your particular has been submited.
<BR>
<%
'****** Get form data *******************
name=Request.Form("name")
address=Request.Form("address")
'********** Write to client document **********
Response.Write ("Name=" & name)
Response.Write ("<BR>")
Response.Write ("Address=" & address)
%>
</CENTER>
</BODY>
</HTML>
Type : Http://localhost/Example/Form.htm
(3) FileSystemObject Object
Provides access to a computer's file system.
You need to edit Proces.asp .
<HTML>
<HEAD><TITLE>Proces</TITLE></HEAD>
<BODY>
<CENTER>
<H1>Result</H1>
<HR>
Your particular has been submited. Clik the button below to view the list.
<FORM ACTION="List.asp" METHOD="POST">
<INPUT TYPE=SUBMIT VALUE="View List">
</FORM>
<%
'****** Get form data *******************
name=Request.Form("name")
address=Request.Form("address")
userform = name & "," & address
'********** open text file & append into it ********************************
Set fso = CreateObject("Scripting.FileSystemObject")
Set a = fso.OpenTextFile("c:\webshare\wwwroot\Example1\testfile.txt", 8, true)
a.WriteLine(userform)
a.Close
%>
</CENTER>
</BODY>
</HTML>
List.asp
<HTML>
<HEAD><TITLE>List</TITLE></HEAD>
<BODY>
<CENTER>
<H1>List</H1>
<HR>
<%
'******** Open text file ************************
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile("c:\webshare\wwwroot\Example1\testfile.txt", 1, false)
do while not a.AtEndOfStream
list=a.readline
Response.Write(list & "<BR>")
loop
a.Close
set a=nothing
set fs=nothing
%>
</CENTER>
</BODY>
</HTML>
(4) Database
ASP used Active Data Object (ADO) to retrieve database. You need to set up ODBC (Open Database Connectivity) in order to access your database.
Example 3: Collect user input into a database and then retrieve it again.
User.mdb
Open MS Access and set up a database with 3 fields.
UserID |
Auto number (key) |
Name |
Text |
Address |
Text |
UserForm.htm
* Note : This form is used to collect information from the user.
<HTML>
<HEAD>
<TITLE>User Form</TITLE>
</HEAD>
<BODY>
<CENTER>
<H2><B>Please fill the form </B></H2>
<FORM Action="ProcessDB.asp" Method="post">
<table>
<tr>
<td>Name : </td>
<td><input name="Name" size=28 ></td>
</tr>
<tr>
<td>Address : </td>
<td><TEXTAREA name=Address rows=5></TEXTAREA> </td>
</tr>
</table>
<input type=submit value="Send">
</FORM>
</CENTER>
</BODY>
</HTML>
ProcessDB.asp
<HTML>
<HEAD>
</HEAD>
<BODY>
<CENTER>
<%
'********* Get Form Data ***************
VName=Request.Form ("Name")
VAddress=Request.Form ("Address")
'******** Insert into Database *********
Set db=Server.CreateObject ("ADODB.Connection")
db.Open "MyUser"
VarQuery="INSERT INTO UserInfor (Name,Address) VALUES ('"
VarQuery=VarQuery & VName & "','"
VarQuery=VarQuery & VAddress & "')"
db.Execute (VarQuery)
db.Close
'***** Inform user data has been successfully update into database ********
Response.Write ("Your data has been successfully updated.")
%>
<br>
<A HREF="ListDB.asp">|List Database</A>
<A HREF="UserForm.htm">|Back to UserForm|</A>
</CENTER>
</BODY>
</HTML>
ListDB.asp
<HTML>
<HEAD>
</HEAD>
<BODY>
<CENTER>
<%
'************ Open Database ***********************
Set db=Server.CreateObject ("ADODB.Connection")
db.Open "MyUser"
VarQuery="SELECT Name, Address FROM UserInfor"
Set rs=db.Execute (VarQuery)
%>
<!-- List All Data in the Database -->
<table BORDER=1>
<tr>
<td BGCOLOR=ORANGE>Name</td><td BGCOLOR=ORANGE>Address</td>
</tr>
<%Do While not rs.EOF%>
<tr>
<td><%= rs("Name") %></td>
<td><%= rs("Address") %></td>
</tr>
<%
rs.movenext
loop
rs.close
%>
</table>
<A HREF="UserForm.htm">|Back to UserForm|</A>
</CENTER>
</BODY>
</HTML>
(5) 3rd Party ASP Component
To add more features into your application, sometimes you need to create a custom component using VB or C++. This is more difficult to do if you do not have any programming background on creating a DLL file. This problem can be solve by using 3rd party ASP component which is written by some Guru Programmer !!
Example 4: Using Flick's ASPMail to send mail using web pages.
You need to download download Flick's ASPMail and register it.
Go to Start à Run à regsvr32 ASPmail.ocx
SendMail.htm
<HTML>
<HEAD>
<TITLE>Send Mail</TITLE>
</HEAD>
<BODY>
<center>
<h2>Send Mail</h2>
<table>
<Form action="SendMail.asp" Method=Post>
<tr>
<td>To :</td>
<td><input type=text name="To" size=28></td>
</tr>
<tr>
<td>From :</td>
<td><input type=text name="From" size=28></td>
<tr>
<tr>
<td>Subject :</td>
<td><input type=text name="Subject" size=28></td>
</tr>
<tr>
<td>Message :</td>
<td><TEXTAREA name="Message" cols=20 rows=5></TEXTAREA></td>
</tr>
</table>
<input type=submit value="Send Mail">
</form>
</center>
</BODY>
</HTML>
SendMail.asp
<HTML>
<HEAD>
</HEAD>
<BODY>
<CENTER>
<%
'************ Get Form Data **************
recipient=Request("To")
sender=Request("From")
subject=Request("Subject")
message=Request("Message")
'********** Create Mail Object ***************
Set Mailer=CreateObject("ASPMail.ASPMailCtrl.1")
mailserver = "intelliquis.com.my"
result = mailer.SendMail(mailserver, recipient, sender, subject, message)
%>
<% If "" = result Then %>
Your mail has been sent.
<% Else %>
Mail was not sent, error message is
<H2>
<%= result %>
</H2>
<% End If %>
</CENTER>
</BODY>
</HTML>
ASP Reference :
Download Flicks @ :
http://tw.uum.edu.my/Suki/lainlain/Utiliti.html
Prepare by Kucing
1/11/1999