Introduction To ASP
Introduction To ASP
HTML
HTML
ASP
ASP
5
3
C O M O b je c t " A B C "
< % 2 P u b lic F u n c t io n G e t D a t a ( p a r m A s I n t ) A s S t r in g
o b j = C r e a te O b je c t( " A B C " ) D im R s A s A D O D B . R e c o r d S e t
d a ta = o b j.G e tD a ta (p a rm ) ...
% > R s = C o n n .E x e c u te ("S E L E C T * F R O M ....")
< bo dy>
M y A S P P ag e 4 G e tD a ta = " < ta b le > ...< /ta b le > "
...
< % = d a ta % >
....
< /b o d y >
1
S e r v e r F ile S y s t e m
How to Install IIS on Windows XP and Windows 2000?
the page:
<%@ language="javascript"%> before <html> tag.
ASP Variables
A variable is used to store information.
Session Variables :
Session variables are used to store information about ONE
and preferences.
Application Variables :
Application variables are also available to all pages in one
application.
Application variables are used to store information about ALL
<%
dim name
name="Priya"
response.write("My name is: " & name)
%>
Iterative Statement
Example :
<%
dim i
for i=1 to 6
response.write("<h" & i & ">Header " & i &
"</h" & i & ">")
next
%>
Arrays
Example
Example:
http://www.yahoo.com/simpleform.asp
Assume that the ASP file "simpleform.asp" contains the
following script:
<body>Welcome <%
response.write(request.form("fname"))
response.write(" " & request.form("lname"))%>
</body>
Output : Welcome Priya Nair
What is a Cookie?
Example :
we will create a cookie collection named "user".
user:
<%
Response.Cookies("user")("firstname")="John"
Response.Cookies("user")("lastname")="Shah"
Response.Cookies("user")("country")="India"
Response.Cookies("user")("age")="25"
%>
Read all Cookies
Assume that your server has sent all the cookies above to a user.
The example below shows how to read all the cookies sent to a
user
Note: The code below checks if a cookie has Keys with the
HasKeys property
<% dim x,y
for each x in Request.Cookies
response.write("<p>")
if Request.Cookies(x).HasKeys then
for each y in Request.Cookies(x)
response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
response.write("<br />")
next
else
Response.Write(x & "=" & Request.Cookies(x) & "<br />") end if
response.write "</p>“
next%>
What if a Browser Does NOT Support Cookies?
If your application deals with browsers that do not support cookies, use
other methods to pass information from one page to another in your
application.
There are two ways of doing this:
1. Add parameters to a URL
2. Use a form
You can add parameters to a URL:
<a href="welcome.asp?fname=John&lname=Smith">Go to Welcome
Page</a>
And retrieve the values in the "welcome.asp" file like this:
<% fname=Request.querystring("fname")
lname=Request.querystring("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>
Use a form
The form passes the user input to "welcome.asp" when the user
clicks on the Submit button:
Retrieve the values using Request.form command.
Note :
• When you request a Web page with a URL contains a "?",
everything after "?" in the URL will be received by the
server as the query string.
• The query string will be store in the server variable called
"QUERY_STRING". It will also be split into pairs of
names and values and stored in "QueryString" collection.
• "&" is used in the query string as delimiters.
• Special characters in the query string need to be encoded.
For example, " " is encoded as "+". I believe ASP offers
both URL encoding and decoding methods.
Server Variable
The ServerVariables object contains a lot of information, including remote
host IP address, URL, translated path name, etc.
Few Server Variables are:
APPL_PHYSICAL_PATH = c:\inetpub\wwwroot\
AUTH_PASSWORD =
LOCAL_ADDR = 127.0.0.1
REMOTE_HOST = 127.0.0.1
REQUEST_METHOD = GET
SCRIPT_NAME = /request_test.asp
SERVER_NAME = localhost
SERVER_PORT = 80
SERVER_PROTOCOL = HTTP/1.1
SERVER_SOFTWARE = Microsoft-IIS/5.0
HTTP_ACCEPT_LANGUAGE = en-us
HTTP_CONNECTION = Keep-Alive
HTTP_HOST = localhost
HTTP_USER_AGENT = Mozilla/4.0 (compatible; MSIE 6.0; MSNIA;
Windows NT...
Response Object
o This object is used to send information to the user ( i.e. to the
browser)
o The most used methods of Response object are:
o Write - Used to send information to be displayed on the
browser.
o Redirect - Used to send the user to a new Page.
o The syntax to use these method is Response.MethodName
o Eg: <% Response.Redirect("newpage.html") %>
o Eg: <% Response.Write("Write Method") %>
Properties, Methods of Response object
Cookies: A collections of cookie objects the server wants to
send to the browser.
Buffer: A boolean property indicating whether to buffer the
page until the contents are completed.
Charset: A property representing the character set name in
HTTP response header.
ContentType: A property representing the content type name
in the HTTP reponse header.
Expires: A property representing the period of time before the
page cached on a proxy server expires.
IsClientConnected: A boolean property indicating whether
the browser has disconnected from the server.
Status: A property representing the status in the HTTP
response header.
Properties, Methods of Response object
AddHeader name, value: A method to set or change the
value of a variable in the HTTP response header.
AppendToLog(string): A method to append the specified
string to the log file of the Web server.
BinaryWrite(data): A method to send binary data to the
browser.
Clear(): A method to clear any buffered HTML output.
End(): A method to stop add data to the HTML output from
both script statements and static HTML data.
Flush(): A method to sends buffered HTML output
immediately to the browser.
Redirect(url): A method to redirect the browser to another
URL.
Write(string): A method to write the specified string to the
HTML output.
Session Object
This object is used to store information with a scope to that
user session.
The information stored are maintained even when the user
A user requests an ASP file, and the Global.asa file uses the
to client.
TimeOut - To set Session timeout period.
<% Response.Write(Session("username"))%>
You can also store user preferences in the Session object, and
Remove method.
Remove Session Variables
stored in it:
<% Session("username")="Priya Nair“
Session("age")=30
dim I
For Each i in Session.Contents
Response.Write(i & "<br />")
Next
%>
Output :
username
age
Loop Through the Contents Collection
If you do not know the number of items in the Contents
collection, you can use the Count property:
<% dim I
dim jj=Session.Contents.Count
Response.Write("Session variables: " & j)
For i=1 to j
Response.Write(Session.Contents(i) & "<br />")
Next
%>
Result:
Session variables: 2
Priya Nair
30
Application Object
Property Result
Browser Type Netscape
Version 4.00
Frames True
Tables True
Cookies True
Javascript True
VBScript False
ActiveX unknown
Example
<% Set myb= Server.CreateObject("MSWC.BrowserType")%>
<table border=2 bordercolor="#000080">
<THEAD>
<tr><th>Property</th><th>Result</th></tr>
</THEAD>
<TBODY>
<tr><td>BrowserType</td>
<td><%=myb.Browser%></td></tr>
<tr><td>Version</td><td><%=myb.Version%></td></tr>
<tr><td>Frames</td><td><%=myb.Frames%></td></tr>
<tr><td>Tables</td><td><%=myb.Tables%></td></tr>
<tr><td>Cookies</td><td><%=myb.Cookies%></td></tr>
<tr><td>JavaScript</td><td><%=myb.Javascript%></td>
</tr>
<tr><td>VBScript</td><td><%=myb.VBScript%></td></tr>
<tr><td>ActiveX</td><td><%=myb.ActiveX%></td></tr>
</tbody></table>
Example
Mappath( ):
<% response.write(Server.MapPath("test.asp") & "<br />") %>
Output: c:\inetpub\wwwroot\script\test.asp
Execute( ): File1.asp:
<% response.write("I am in File 1!<br />")
Server.Execute("file2.asp")
response.write("I am back in File 1!") %>
File2.asp:
<% response.write("I am in File 2!<br />") %>
Output:
I am in File 1!
I am in File 2!
I am back in File 1!
What is ADO?
You can insert the content of one ASP file into another ASP
file before the server executes it, with the #include directive.
The #include directive is used to create functions, headers,
footers, or elements that will be reused on multiple pages.
How to Use the #include Directive
Here is a file called "mypage.asp":
<html> <body> <h3>Words of Wisdom:</h3><p>
<!--#include file="wisdom.inc"--></p>
<h3>The time is:</h3><p>
<!--#include file="time.inc"--></p>
</body> </html>
Example : Counting the no of current Visitors
<script language="vbscript" runat="server">
Sub Application_OnStart
Application("visitors")=0
End Sub
Sub Session_OnStart
Application.Lock
Application("visitors")=Application("visitors")+1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors")=Application("visitors")-1
Application.UnLock
End Sub
</script>
Display it using : <p>There are <%response.write(Application("visitors"))%>
online now!</p>
Note : Script delimiters are not allowed in Global.asa file.