Documente Academic
Documente Profesional
Documente Cultură
Server-side technologies
Dynamically creates Web pages
Use client information, server information and
information from the Internet
Active Server Pages (ASP)
Microsoft Server-side technology
Dynamically build documents in response to client
requests
Deliver dynamic Web content
How Active Server Pages Work
Active Server Pages
Processed by scripting engine
Server-side ActiveX control
.asp file extension
Can contain XHTML tags
Scripting written with VBScript
JavaScript also used
Others (Independent Software Vendors)
Communication with Server
Client HTTP request to server
Active server page processes request and returns results
ASP document is loaded into memory
asp.dll scripting engine on server
Parses (top to bottom)
Web Server
Need to run Web server to use Active Server Pages
IIS 5.0 (Internet Information Services) or higher
Create a virtual directory
Copy files to c:\InetPub\Wwwroot
The Basic Syntax Rule
An ASP file normally contains HTML tags,
just like an HTML file.
An ASP file can also contain server scripts,
surrounded by the delimiters <% and %>.
Server scripts are executed on the server, and
can contain any expressions, statements,
procedures, or operators valid for the scripting
language you prefer to use.
Write Output to a Browser
<html>
<body>
<% response.write("Hello World!") %>
</body>
</html>
<html>
<body>
<%="Hello World!"%>
</body> </html>
VBScript
In ASP the default scripting language is VBScript:
<html>
<body>
<% response.write("Hello World!") %> </body> </html>
JavaScript
Unlike VBScript - JavaScript is case sensitive.
<%@ language="javascript"%>
<html>
<body>
<% Response.Write("Hello World!") %> </body>
</html>
VARIABLES
Lifetime of Variables
A variable declared outside a procedure can be accessed and
changed by any script in the ASP file. To declare variables
accessible to more than one ASP file, declare them as session
variables or application variables.
Session Variables
<html>
<head>
<% sub vbproc(num1,num2)
response.write(num1*num2) end sub %> </head>
<body>
<p>Result: <%call vbproc(3,4)%></p>
</body></html>
<%@ language="javascript" %>
<html>
<head>
<% function jsproc(num1,num2)
{ Response.Write(num1*num2) } %> </head>
<body>
<p>Result: <%jsproc(3,4)%></p>
</body>
</html>
<html>
<head>
<%
sub vbproc(num1,num2)
Response.Write(num1*num2)
end sub
%>
<script language="javascript" runat="server">
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
</script>
</head>
<body>
<p>Result: <%call vbproc(3,4)%></p>
<p>Result: <%call jsproc(3,4)%></p>
</body>
</html>
ASP Forms
The Request.QueryString and Request.Form
commands may be used to retrieve information
from forms, like user input.
<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>
Request.QueryString - is used to collect values in
a form with method="get". Information sent from
a form with the GET method is visible to everyone
(it will be displayed in the browser's address bar)
and has limits on the amount of information to
send.
the URL sent to the server would look like this
http://www.w3schools.com/simpleform.asp?fname
=Bill&lname=Gates
<body> Welcome
<% response.write(request.querystring("fname"))
response.write(" " & request.querystring("lname"))
%> </body>
Welcome Bill Gates
Request.Form is used to collect values in a form
with method="post".
Information sent from a form with the POST
method is invisible to others and has no limits on
the amount of information to send.
http://www.w3schools.com/simpleform.asp
<body> Welcome
<% response.write(request.form("fname"))
response.write(" " & request.form("lname")) %>
</body>
Form Validation
A good way to validate a form on the server is to
post the form to itself, instead of jumping to a
different page. The user will then get the error
messages on the same page as the form.
<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"
value="Volvo">Volvo</input>
<br />
<input type="radio" name="cars"
value="Saab">Saab</input>
<br />
<input type="radio" name="cars"
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>
Saab
BMW Submit
Hello punitha
How are you today?
ASP Cookies
A cookie is often used to identify a user. A cookie is a
small file that the server embeds on the user's
computer.
Each time the same computer requests a page with a
browser, it will send the cookie too. With ASP, you
can both create and retrieve cookie values.
How to Create a Cookie?
The "Response.Cookies" command is used to create
cookies.
The Response.Cookies command must appear
BEFORE the <html> tag.
Ex. create a cookie named "firstname" and assign the
value "Alex" to it:
<% Response.Cookies("firstname")="Alex" %>
It is also possible to assign properties to a cookie, like
setting a date when the cookie should expire:
<%Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires=#May
10,2002# %>
How to Retrieve a Cookie Value?
cookie value.
In the example below, we retrieve the value of the
Session("age")=50 %>
When the value is stored in a session variable it can
<%Response.Write(Session("username"))%>
Remove Session Variables
<%
Response.Write(Session.SessionID)
%>
ASP Application Object
A group of ASP files that work together to
perform some purpose is called an application.
The Application object in ASP is used to tie these
files together.
The Application object is used to store and access
variables from any page, just like the Session
object. The difference is that ALL users share one
Application object, while with Sessions there is one
Session object for EACH user.
you can change the information in one place and
the changes will automatically be reflected on all
pages.
Store and Retrieve Application Variables
Application variables can be accessed and changed by
like this:
<script language="vbscript" runat="server">
Sub Application_OnStart
application("vartime")=""
application("users")=1
End Sub </script>
To retrieve
<% Response.Write(Application("users")) %>
The number of items in the Contents collection -use
Count property:
<% dim i dim j
j=Application.Contents.Count
For i=1 to j
Response.Write(Application.Contents(i) & "<br />") Next
%>
Session events
<object> declarations
TypeLibrary declarations
#include directive
<%=MyAd.GetAdvertisement("/banners/adrot.txt")%>
Application object
<%
response.write("<p style='color:#0000ff'>This text is
styled with the style attribute!</p>")
%>
</body>
</html>
ASP Request Object
The ASP Request object is used to get
information from the user.
QueryString Collection Examples
<html>
<body>
<a href="demo_simplequerystring.asp?color=green">
Example</a>
<%Response.Write(Request.QueryString)%>
</body> </html>
O/P Example color=green
<html><body>
<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 %>
</body> </html>
Your name:
Form Collection Examples
2. <%
Response.Write(Request.Form)
%>
fname=Donald&lname=Duck
4. <html>
<body>
<%
fruits=Request.Form("fruits")
%>
<form action="demo_checkboxes.asp" method="post">
<p>Which of these fruits do you prefer:</p>
<input type="checkbox" name="fruits"
value="Apples“>Apple <br>
<input type="checkbox" name="fruits"
value="Oranges“>Orange <br>
<input type="checkbox" name="fruits"
value="Bananas“>Banana <br>
<input type="submit" value="Submit">
</form>
<%
if fruits<>"" then%>
<p>You like: <%Response.Write(fruits)%></p>
<%end if
%>
</body>
</html>
<%Response.Write(Request.ServerVariables("request_method"))%></>
<p><b>The server's domain name:</b>
<%Response.Write(Request.ServerVariables("server_name"))%></p>
<p><b>The server's port:</b>
<%Response.Write(Request.ServerVariables("server_port"))%></p>
<p><b>The server's software:</b>
<%Response.Write(Request.ServerVariables("server_software"))%></>
</body></html>
Create a welcome cookie
<%
dim numvisits
response.cookies("NumVisits").Expires=date+365
numvisits=request.cookies("NumVisits")
if numvisits="" then
response.cookies("NumVisits")=1
response.write("Welcome! This is the first time you are
visiting this Web page.")
else
response.cookies("NumVisits")=numvisits+1
response.write("You have visited this ")
response.write("Web page " & numvisits)
if numvisits=1 then
response.write " time before!"
else
response.write " times before!"
end if
end if %>
<html>
<body>
</body></html>
Find the total number of bytes the user sent
<html>
<body>
<form action="demo_totalbytes.asp" method="post">
Please type something:
<input type="text" name="txt"><br><br>
<input type="submit" value="Submit">
</form>
<%
If Request.Form("txt")<>"" Then
Response.Write("You submitted: ")
Response.Write(Request.Form)
Response.Write("<br><br>")
Response.Write("Total bytes: ")
Response.Write(Request.Totalbytes)
End If
%>
</body>
</html>
Properties
TotalBytes -Returns the total number of bytes
the client sent in the body of the request
Method
BinaryRead Retrieves the data sent to the
server from the client as part of a post request
and stores it in a safe array.
<% dim a,b
a=Request.TotalBytes
b=Request.BinaryRead(a)
%>
ASP Server Object
The ASP Server object is used to access
properties and methods on the server.
When was a file last modified?
<html>
<body>
<%
Set fs =
Server.CreateObject("Scripting.FileSystemObject")
Set rs =
fs.GetFile(Server.MapPath("demo_lastmodified.asp"))
modified = rs.DateLastModified
%>
This file was last modified on:
<%response.write(modified)
Set rs = Nothing
Set fs = Nothing
%>
</body>
</html>
This file was last modified on: 9/12/2006 6:40:45 AM
Open a text file for reading
<html>
<body>
<%
Set FS = Server.CreateObject("Scripting.FileSystemObject")
Set RS = FS.OpenTextFile(Server.MapPath("text") &
"\TextFile.txt",1)
While not rs.AtEndOfStream
Response.Write RS.ReadLine
Response.Write("<br />")
Wend
%>
<p>
<a href="text/textfile.txt"><img border="0"
src="/images/btn_view_text.gif"></a>
</p>
</body>
</html>
Transfer Method
File1.asp:
<% response.write("Line 1 in File 1<br />")
Server.Transfer("file2.asp")
response.write("Line 2 in File 1<br />") %>
File2.asp:
<% response.write("Line 1 in File 2<br />")
response.write("Line 2 in File 2<br />") %> Output:
Line 1 in File 1
Line 1 in File 2
Line 2 in File 2
ASPError Object
The ASPError object is used to display
detailed information of any error that occurs in
scripts in an ASP page.
The properties below can only be accessed
set objErr=Server.GetLastError()
response.write("Line=" & objErr.Line)
ASP FileSystemObject Object
The FileSystemObject object is used to access
the file system on the server.
Does a specified file exist?
<html>
<body>
<%
Set
fs=Server.CreateObject("Scripting.FileSystemObject")
If (fs.FileExists("c:\winnt\cursors\3dgarro.cur"))=true
Then
Response.Write("File c:\winnt\cursors\3dgarro.cur
exists.")
Else
Response.Write("File c:\winnt\cursors\3dgarro.cur
does not exist.")
End If
set fs=nothing
%>
</body>
</html>
TextStream Object
The TextStream object is used to access the contents of a text
file.
<html>
<body>
<p>This is the first five characters from the text file:</p>
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("testread.txt"), 1)
Response.Write(f.Read(5))
f.Close
Set f=Nothing
Set fs=Nothing
%>
</body></html>
O/P
Folder Object
What is ADO?
ADO is a Microsoft technology
IIS
ADO is a programming interface to access
data in a database
Accessing a Database from an ASP Page
Create an ADO connection to a database
Recordset
Display
Query
Sort
Add
Update
Delete
set rs=Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname
FROM Customers WHERE CompanyName LIKE
'A%'“
rs.Open sql, conn
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", 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>
CustomerID = ALFKI
CompanyName = Alfreds Futterkiste
ContactName = Maria Anders
Address = Obere Str. 57
City = Berlin
PostalCode = 12209
Country = Germany
CustomerID = BERGS
CompanyName = Berglunds snabbköp
ContactName = Christina Berglund
Address = Berguvsvägen 8
City = Luleå
PostalCode = S-958 22
Country = Sweden
ASP AdRotator Component
The ASP AdRotator component creates an AdRotator
object that displays a different image each time a user
enters or refreshes a page. A text file includes
information about the images.
Example
Assume we have a file called "banners.asp". It looks
like this:
<html>
<body>
<%
set
adrotator=Server.CreateObject("MSWC.AdRotator")
response.write(adrotator.GetAdvertisement("ads.txt")
)
%>
</body>
The file "ads.txt" looks like this:
*
w3schools.gif
http://www.w3schools.com/
Visit W3Schools
80
microsoft.gif
http://www.microsoft.com/
Visit Microsoft
20
ASP Browser Capabilities Component
The ASP Browser Capabilities component