Sunteți pe pagina 1din 81

Active Server Pages

What Is ASP

• ASP is a powerful tool for making dynamic


and interactive Web pages.
• ASP file can contain:HTML / XHTML
A scripting language like JavaScript or
VBScript
• Server-side scripting Language
Simple program

• <html><body>
<%
response.write("My first ASP script!")
%>
</body></html>

• <html><body>
<%="Hello World!"%>
</body></html>
Using VBScript in ASP

• <html>
<body>
<%
response.write("Hello World!")
%>
</body>
</html>
Using JavaScript in ASP

• <%@ language="javascript"%>
<html>
<body>
<%
Response.Write("Hello World!")
%>
</body>
</html>
Lifetime of Variables
• To declare variables accessible to more than
one ASP file, declare them as session variables
or application variables.
• Session Variables
• Session variables are used to store information
about ONE single user, and are available to all
pages in one application. Typically information
stored in session variables are name, id, and
preferences.
Lifetime of Variables

• Application Variables
• Application variables are also available to
all pages in one application. Application
variables are used to store information
about ALL users in one specific application
Procedures in VBScript

• <html>
<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head>
<body>
<p>Result: <%call vbproc(3,4)%></p>
</body>
</html>
Procedure in javascript
• <%@ language="javascript" %>
<html>
<head>
<%
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
%>
</head>
<body>
<p>Result: <%jsproc(3,4)%></p>
</body>
</html>
Response Object

• The ASP Response object is used to send


output to the user from the server.
Collections
Collection Description

Cookies Sets a cookie value. If the cookie does


not exist, it will be created, and take
the value that is specified
What is a Cookie?

• How to Create a Cookie?


• <%
Response.Cookies("firstname")="Alex"
%>
• How to Retrieve a Cookie Value?
• <%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
Response Object Methods
• Methods
• AddHeaderAdds a new HTTP header and a value to the
HTTP response
• AppendToLogAdds a string to the end of the server log
entry
• BinaryWriteWrites data directly to the output without
any character conversion
• ClearClears any buffered HTML output
• EndStops processing a script, and returns the current
result
• FlushSends buffered HTML output immediatel
• yRedirectRedirects the user to a different URL
• WriteWrites a specified string to the output
Examples
• <%
response.Buffer=true
%>

• <%
response.Clear
%>
• <%
Response.Redirect "http://www.w3schools.com"
%>
ASP Request Object
• The Request object is used to get
information from a visitor.
Collections
Collection Description
ClientCertific Contains all the field values stored in
ate the client certificate
Cookies Contains all the cookie values sent in a
HTTP request
Form Contains all the form (input) values from
a form that uses the post method
QueryString Contains all the variable values in a
HTTP query string
ServerVariable Contains all the server variable values
s
Request.QueryString

• <body>
Welcome
<%
response.write(request.querystring("fnam
e"))
response.write(" " &
request.querystring("lname"))
%>
</body>
Request.Form

• <body>
Welcome
<%
response.write(request.form("fname"))
response.write(" " &
request.form("lname"))
%>
</body>
What is a Cookie?

• How to Create a Cookie?


• <%
Response.Cookies("firstname")="Alex"
%>
• How to Retrieve a Cookie Value?
• <%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
ASP Session Object

• A Session object stores information about,


or change settings for a user session.
Session Object

• Collections

Collection Description
Contents Contains all the items
appended to the session
through a script command
StaticObjects Contains all the objects
appended to the session
with the HTML <object> tag
Session object methods

• AbandonDestroys a user session


• Contents.RemoveDeletes an item from the
Contents collection
• Contents.RemoveAll()Deletes all items
from the Contents collection
Example

• <%
Session("name")="Hege"
Session.Abandon
Response.Write(Session("name"))
%>
• <%
Session.Contents.RemoveAll()
%>
When does a Session Start?

• A new user requests an ASP file, and the


Global.asa file includes a Session_OnStart
procedure
• A value is stored in a Session variable
• A user requests an ASP file, and the
Global.asa file uses the <object> tag to
instantiate an object with session scope
Example
• <%
Session("test1")=("First test")
Session("test2")=("Second test")
Session("test3")=("Third test")
Session.Contents.Remove("test2")
for each x in Session.Contents
  Response.Write(x & "=" & Session.Contents(x) & "<br />")
next
%>
Output:
test1=First test
test3=Third test
When does a Session End?

 If you want to set a timeout interval that is


shorter or longer than the default, use the
Timeout property.
 <%Session.Timeout=5%>
 Use the Abandon method to end a session
immediately:
 <%Session.Abandon%>
Store and Retrieve Session
Variables

• <%
Session("username")="Donald Duck"
Session("age")=50
%>
• Welcome <
%Response.Write(Session("username"))
%>
ASP Application Object

• A group of ASP files that work together to


perform some purpose is called an
application.
Application Object

• Collections
Collection Description
Contents Contains all the items
appended to the application
through a script command
StaticObjects Contains all the objects
appended to the application
with the HTML <object> tag
Application object methods

• Contents.RemoveDeletes an item from the


Contents collection
• Contents.RemoveAll()Deletes all items from the
Contents collection
• LockPrevents other users from modifying the
variables in the Application object
• UnlockEnables other users to modify the
variables in the Application object (after it has
been locked using the Lock method)
Example

• <%
Application("test1")=("First test")
Application("test2")=("Second test")
Application("test3")=("Third test")
Application.Contents.Remove("test2")
for each x in Application.Contents
  Response.Write(x & "=" & Application.Contents(x) & "<br />")
next
%>
Output:
test1=First test
test3=Third test
Example

• <%
Application.Lock
Application("visits")=Application("visits")+1
Application.Unlock
%>

This page has been visited


<%=Application("visits")%> times!
Store and Retrieve
Application Variables

• <script language="vbscript" runat="server">


Sub Application_OnStart
application("vartime")=""
application("users")=1
End Sub
</script>
• There are
<%
Response.Write(Application("users"))
%>
ASP The Global.asa file

• The Global.asa file is an optional file that


can contain declarations of objects,
variables, and methods that can be
accessed by every page in an ASP
application
ASP Application

• Methods:
Method Description
Contents.Remove Deletes an item from the Contents collection

Contents.RemoveAll() Deletes all items from the Contents collection

Lock Prevents other users from modifying the variables


in the Application object
Unlock Enables other users to modify the variables in the
Application object (after it has been locked using
the Lock method)
Events in Global.asa
• Application_OnStart - Occurs when the FIRST user
calls the first page in an ASP application. This event
occurs after the Web server is restarted or after the
Global.asa file is edited. The "Session_OnStart" event
occurs immediately after this event. Session_OnStart
- This event occurs EVERY time a NEW user requests his
or her first page in the ASP application.
• Session_OnEnd - This event occurs EVERY time a user
ends a session. A user-session ends after a page has not
been requested by the user for a specified time (by
default this is 20 minutes).
Events in Global.asa
• Application_OnEnd - This event occurs after the LAST
user has ended the session. Typically, this event occurs
when a Web server stops. This procedure is used to
clean up settings after the Application stops, like delete
records or write information to text files.

• Application_OnEnd - This event occurs after the LAST


user has ended the session. Typically, this event occurs
when a Web server stops. This procedure is used to
clean up settings after the Application stops, like delete
records or write information to text files.
Global.asa Example
• <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>
To display the number of
current visitors in an ASP file:

• <html>
<head>
</head>
<body>
<p>There are <
%response.write(Application("visitors"))
%> online now!</p>
</body>
</html>
What is ASP?

• Active Server Pages


• A series of objects and components that
are executed on the web server
• Uses a suite of technologies that allows
dynamically-generated content
• Control of how content is generated from
the server to the browsers
HTML Page Execution
You Request an HTML Page
www.ocstc.org/resumes.htm

Server finds and downloads the page

Browser displays the page


ASP Page Execution
You Request an ASP Page
www.ocstc.org/default.asp

Server finds the page

Server executes ASP commands, converts ASP


to HTML/XML as needed, processes HTML/XML

Browser displays the page


ASP Compared to Other
Scripting Languages

• ASP and /PHP/Perl/CGI execution are


roughly equivalent in execution sequence
• Compared to VBScript, ASP does not have
to worry about browser incompatibility
• Compared to JavaScript, don’t have to
worry about browser versions or disabling
• Compared to Java, don’t have to worry
about whether JRE (or MVM) is installed
ASP and XML

• ASP can control the XML experience from


beginning to end
 Controls the output for conflicting browsers
 Generate XML data
 Logically connect XML and standalone databases
 Control data output formatting that XML or HTML
cannot control
• Sample Application Later
ASP and XML

• For more information


 “My XML, Your Browser,” by Charlie
Heinemann, Microsoft Corporation.
http://msdn.microsoft.com/library/en-
us/dnexxml/html/xml011199.asp
 “ASP Technology and the XML DOM,” by Alan
McBee, Microsoft Corporation.
http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnexxml/html/xml092099.asp
ASP in the Microsoft
Language Family

• The ASP commands...


Visual Basic

VBScript ASP

• But there’s more to ASP than just code...


ASP in the Microsoft
Language Family

• Some characterize ASP as “more or less


server-side VBScript”
• Arguments supporting this statement
• Arguments against this statement
ASP in the Microsoft
Language Family

• ASP can also interact with other Microsoft


languages or applications:
 Execute SQL commands
 Open and close databases
 Create and Modify database tables
 Control applications or COM objects written or
compiled in other languages (VB, C++, Java)
 Extract content from Excel or Word
 Control an XML document
ASP Object Model

• ASP itself is not Object-Oriented. ASP can


use objects but cannot define new objects
• Composed of
 5 objects
 5 components
ASP Objects

• Request
• Response
• Server
• Application
• Session
ASP Components

• Scripting Objects Component


• ADO (ActiveX Data Objects) Component
• Ad Rotator Component
• Browser Capabilities Component
• Content Linking Component
ASP Object Model
Server
Request ASP.DLL
YOU (if it finds <%%>, invokes
Scripting Objects Component)

Response

Application
Session
What Does ASP Look Like
in Action?
What Does ASP Look Like
in Action?

• What HTML “looks like”


<p>Analytical Ultracentrifugation Workshop (May 21-23, 2001)
and Symposium (May 24, 2001) at the National Analytical
Ultracentrifugation Facility, Storrs, Conn. For additional
information</p>
<ul>
<li><a
href="http://www.ucc.uconn.edu/~wwwbiotc/99wkshp.html"
target="_blank">
National Analytical Ultracentrifugation
Facility</a></li>
<li><a href="naufworkshop.asp">Analytical
Ultracentrifugation Workshop and Symposium Description and
Registration (PDF Format)</a></li>
</ul>
What Does ASP Look Like
in Action?

• What the “real code” looks like*


<% If Today < CDate("5/25/01") Then %>
<p>Analytical Ultracentrifugation Workshop (May 21-23, 2001)
and Symposium (May 24, 2001) at the National Analytical
Ultracentrifugation Facility, Storrs, Conn. For additional
information</p>
<ul><li><a
href="http://www.ucc.uconn.edu/~wwwbiotc/99wkshp.html"
target="_blank">National Analytical Ultracentrifugation
Facility</a></li>
<li><a href="naufworkshop.asp">Analytical
Ultracentrifugation Workshop and Symposium Description and
Registration (PDF Format)</a></li>
</ul>
<% End If %>

* Coloring from Microsoft FrontPage


Tools You Need to Create
ASP Applications

• Web Server that Supports ASP


 Microsoft Internet Information Server (IIS)—
Web services of Windows NT Server 4.0 and
Windows 2000 Server
 Microsoft Personal Web Server (PWS)—
Creates a fully functional subset of IIS 4.0
that can run on all other Windows OS
PWS is an add-on for Windows NT, 95/98/Me
Built-in to Windows 2000/XP PROFESSIONAL
• Fundamental knowledge of Visual Basic
Microsoft Personal Web Server
http://www.microsoft.com/ntserver/nts/downloads/recommended/NT4OptPk/default.asp
Tools You Need to Create
ASP Applications

• If you are working with database


storage/retrieval
 Knowledge of Access, SQL Server, etc. (to
create databases and tables)
 Knowledge of SQL commands (or at least
how to use Access to generate SQL
statements)
 Knowledge of the VB style of data handling
• Authoring tool(s)
ASP Application
Development Tools

• Personal Preferences
 Microsoft FrontPage
 Allaire HomeSite
• Other Tools
 Microsoft Access
 Visual Basic
 Microsoft Visual InterDev
 Visual Studio .NET
 Other Non-Microsoft Tools (never used them)
ASP Reference Tools

• Microsoft Web Site (www.microsoft.com)


 Knowledge Base for different Microsoft
products and Web developer issues
 Search Capability for programming language
keywords and their syntax
 Online versions of Reference Books
 MSDN (Microsoft Developer Network) Web
site for subscribers and/or owners of VStudio
• MSDN Subscription on CD or DVD
ASP Reference Tools—
Microsoft Site Search
ASP Reference Tools—
Microsoft Site Search
ASP Reference Tools—
Microsoft Site Search
ASP Reference Tools

 ASP Reference Books


 ASP Developer’s Guide, by Greg Buczek
ISBN: 0-07-212274-3
 Beginning Active Server Pages, by Francis et al
ISBN: 1-861001-34-7*
 Professional Active Server Pages, by Francis et al
ISBN: 1-861001-26-6*

* Online version of this book is on the Microsoft Web site


http://www.microsoft.com/TechNet/iis/books.asp
ASP Reference Tools

• ASP Reference Books (continued)


 Using Active Server Pages, by Scot Johnson
ISBN: 0-7897-1389-6
• VBScript
 VBScript Unleashed, by Petrousos, Schongar et al
ISBN: 1-57521-124-6
Major Sites Using ASP
• Microsoft
www.microsoft.com
• NASDAQ
www.nasdaq.com
• Beckman Coulter
www.beckmancoulter.com
• PC-Mac Connection
www.pcconnection.com
• Ticketmaster
www.ticketmaster.com
• State Farm and Allstate Ins. Companies
www.statefarm.com / www.allstate.com
“ASP Page” vs.
“ASP Application”

• An ASP page
 Needs the .ASP file extension to indicate to
the server that there is code the server must
interpret
 A series of commands on one page
• An ASP application is a series of pages
that are linked together through code and
the content linking component to function
as a logical unit
“ASP Page” vs.
“ASP Application”

• How the information is handled


 Main communication device between pages
and the server is by Forms
 Information can be transmitted or transferred
from page to page in an application by
“Submitting” a form
Sending a “URL-encoded” string
www/ocstc.org/meetingadmin/edit.asp?ID=5
Storing data as a variable
Myths/Criticisms of ASP

• ASP is Microsoft/Windows centric


• ASP doesn’t work with Netscape
• ASP pages are not recognized by Search
Engines
• ASP is “slow” because it’s interpreted (or
because it’s Microsoft)
• ASP doesn’t work with client-side scripting
languages, especially JavaScript
Criticism of ASP—
Microsoft/Windows Centric
Criticisms of ASP—
Netscape
Criticisms of ASP—
Search Engines
ASP is “slow”

• ObjectWatch Newsletter
J2EE versus .NET--The Latest Benchmark
http://www.objectwatch.com/issue_42.htm
• Conclusion: ASP/.NET technology
 Hardware/Software infrastructure is LESS
 Development time is SHORTER
 Development cost is LESS
 Administrative cost is LESS
 Application performance is FASTER and can
absorb HIGHER TRAFFIC
ASP Doesn’t Work with
Client-Side Scripting

• JavaScript commands will be processed


first, then ASP
• ASP can be used in conjunction with
JavaScript especially when verifying form
data and where JavaScript verification can
be difficult to program
• You can use ASP to assign values back
into JavaScript code
ASP Samples

• ASP Pages
 Chapter Contacts
 OCSTC Home Page
• ASP Applications
 Chapter Meetings
 Job Information
• ASP and XML
 XML assignment
Conclusion: ASP and the
Technical Communicator

• ASP is another tool in your arsenal to


control information
• ASP tests your self-esteem
• ASP may lead you to the “dark side”—
programming
• Future of ASP… “What Lies Beneath”—
Microsoft FrameWork.NET, a successor to
Visual Studio; compiled ASP
Questions?
NOT The End…

• Presentation Link
http://www.ocstc.org/meeting_archive.asp
• Sample XML/ASP application
 ASP Position Paper
http://www.ocstc.org/Jeff_XMLWork/XMLandASP.pdf
 Output Page
http://www.ocstc.org/Jeff_XMLWork/default.asp
 XML Data Page
http://www.ocstc.org/Jeff_XMLWork/products.asp
A Final Thought—1...

• Consider this
Private Sub Madness ()
Call Method
End Sub
• Translation?
“There’s a Method in the Madness”
A Final Thought—2...

• “…In the world of the Web, you have to


choose between an organization [W3C] that
wants to tell you how to design your pages
and an organization [Microsoft] that wants
you to design sites their way. I pray you
have the wisdom to choose wisely.”
—Vincent Flanders

S-ar putea să vă placă și