Professional Documents
Culture Documents
Session 1 -
Introduction to ASP.
ASP Syntax, Variables & Procedures.
Include Files.
Session 2 -
HTTP Protocol.
Introduction to ASP Objects.
Request Object.
Response Object.
Session 3 -
Application Object.
Session Object.
Global.asa
Session 4 Session 5 -
Server Object.
ObjectContext Object.
Error Object.
CSC India
Introduction to ASP
What is ASP?
ASP stands for Active Server Pages.
ASP is a program that runs inside IIS.
IIS stands for Internet Information Services.
ASP is Microsofts solution to building advanced Web sites.
CSC India
Introduction to ASP
How Does ASP Differ from HTML?
HTML file generates static pages, but ASP file generates dynamic pages.
HTML file has the extension .html/.htm, but ASP file has the extension
.asp
When a browser sends a requests to the web server for an HTML file the
server returns the file as it is to the browser, but when a browser sends a
request to the web server for an ASP file, IIS passes the request to the ASP
engine having a special program ASP.dll. This ASP file is processed line by
line and executes the server side scripts(<% %>) in the file. Finally, the
ASP file is returned to the browser as plain HTML.
CSC India
Introduction to ASP
Processing of an HTML Page
Browser
HTML File
Request
Web Server
Memory-HTML file
CSC India
Introduction to ASP
Processing of an ASP Page
Browser
HTML File
Request
Web Server
CSC India
Introduction to ASP
What can ASP do for you?
ASP file generates dynamic web pages displaying different contents for
different users and at different times of the day.
Responds to users queries or data submitted from HTML forms.
Accesses any data or databases and return the results to a browser.
Customizes 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, speed and it minimizes the network traffic.
CSC India
Introduction to ASP
ASP page can consists of the following:
HTML tags.
Scripting Language (JavaScript/VBScript).
ASP Built-In Objects.
ActiveX Components eg. : ADO ActiveX Data Objects.
CSC India
ASP Syntax
The Basic Syntax Rule
An ASP file normally contains HTML tags, just as a standard HTML file.
In addition, an ASP file can contain server side scripts, surrounded by the
delimiters <% and %>. Server side scripts are executed on the server,
and can contain any expressions, statements, procedures, or operators that
are valid for the scripting language you use.
CSC India
10
ASP Syntax
Scripts
Script is nothing but a set of commands that are written to perform a
specific task
These commands are VBScript or JavaScript commands
There are two types of Scripts:
Client-Side Script : Runs On Browser (default : JavaScript)
Server-Side Script : Runs On Server (default : VBScript)
Scripts in an ASP file are executed on the server.
CSC India
11
ASP Syntax
Scripts
Client-Side Script is embedded into the HTML file using tags:
<script language=JavaScript/VbScript>
{JavaScript/Vbscript Code}
</script>
Server-Side Script is embedded into the ASP file using tags:
<script language=Vbscript/JavaScript RunAt=SERVER>
{Vbscript/JavaScript Code}
</Script>
OR
<%@ Language = VBScript/JavaScript %>
<% {VBScript/JavaScript Code} %>
CSC India
12
ASP Syntax
Scripts
Difference Between using <Script> Tag and <% %> delimiters
<Script> tag is executed immediately no matter where it appears.
By using <Script> tag it is possible to mix multiple scripting languages
within single ASP page.
Before using <% %> delimiters, this line of code is mandatory <%@
Language = VBScript/JavaScript %> which specifies the language being
used.
CSC India
13
ASP Syntax
Example
<%@ Language=VBScript %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript" RunAT="SERVER">
function sayhello()
{
response.write("Welcome")
}
</SCRIPT>
<% For i=1 to 10
sayhello()
Next
%>
</BODY>
</HTML>
CSC India
14
ASP Variables
Variables
Variables are used to store information
This example demonstrates how to create a variable, assign a value to it,
and insert the variable value into a text.
<html>
<body>
<%
Dim name
name=Tripti Arora"
Response.Write("My name is: " & name)
%>
</body>
</html>
CSC India
15
ASP Variables
Arrays
Arrays are used to store a series of related data items.
This example demonstrates how you can make an array that stores names.
<%
Dim name(5)
name(0) = "Jan Egil"
name(1) = "Tove"
name(2) = "Hege"
name(3) = "Stale"
name(4) = "Kai Jim"
name(5) = "Borge"
For i = 0 to 5
Response.Write(name(i) & "<br />")
Next
%>
</body>
</html>
CSC India
16
ASP Variables
Lifetime of Variables
A variable declared outside a procedure(subroutine or a function) can be
accessed and changed by any script in the ASP page in which it is declared
A variable declared inside a procedure is created and destroyed every time
the procedure is executed. No scripts outside that specific procedure can
access or change that variable
To make a variable accessible to several ASP pages, declare it either as a
session variable or as an application variable
CSC India
17
ASP Variables
Session Variables
Session variables store information about one single user, and are
available to all pages in one application. Common information stored in
session variables are Username and UserID. To create a session variable,
store it in a Session Object
Application Variables
Application variables are also available to all pages in one application.
Application variables are used to hold information about all users in a
specific application. To create an application variable, store it in an
Application Object
CSC India
18
ASP Procedures
Calling a Procedure
When calling a VBScript procedure from an ASP page, you can use the
"call" keyword followed by the procedure name.
If a procedure requires parameters, the parameter list must be enclosed in
parentheses when using the "call" keyword.
If you omit the "call" keyword, the parameter list must not be enclosed in
parentheses.
If the procedure has no parameters, the parentheses are optional.
When calling a JavaScript procedure from an ASP page, always use
parentheses after the procedure name.
CSC India
19
ASP Procedures
Example
<html>
<head>
<%
Sub vbProc(num1,num2)
Response.Write(num1*num2)
End Sub
%>
</head>
<body>
The result of the calculation is: <%call vbProc(3,4)%>
</body>
</html>
CSC India
20
ASP Procedures
Example
Insert the <%@ language="language" %> line above the <html> tag to
write procedures or functions in a scripting language other than the default.
<%@ language="javascript" %>
<html>
<head>
<%
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
%>
</head>
<body>
The result of the calculation is: <%jsproc(3,4)%>
</body>
</html>
CSC India
21
Including Files
The #include Directive
It is possible to insert the content of another file into an ASP file before the
server executes it, with the server side #include directive.
The #include directive is used to create functions, headers, footers, or
elements that will be reused on multiple pages.
CSC India
22
Including Files
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>
Here is the "wisdom.inc" file:
"One should never increase, beyond what is necessary,
the number of entities required to explain anything."
Here is the "time.inc" file:
<%
Response.Write(Time)
%>
CSC India
23
Including Files
Source code in a browser, it will look something like this:
<html>
<body>
<h3>Words of Wisdom:</h3>
<p>"One should never increase, beyond what is
necessary, the number of entities required to explain
anything."</p>
<h3>The time is:</h3>
<p>11:33:42 AM</p>
</body>
</html>
CSC India
24
Including Files
Syntax for Including Files
To include a file into an ASP page, place the #include directive inside
comment tags: <!--#include file ="somefilename"-->
A file can be included in 2 ways :
<!-- #include file ="headers\header.inc" -->
OR
<!-- #include virtual ="/html/header.inc" -->
Using the File Keyword :
Use the file keyword to indicate the physical path. The file to be
included must be located either in the current directory/subdirectory or
higher-level directory where your ASP file is present.
For example, if you have an ASP file in the directory html, and the file
header.inc is in html\headers, the line above would insert header.inc in
your file.
CSC India
25
Including Files
Using the Virtual Keyword :
The Included file can have any name & any extension. By convention
the included files end with extension .inc, but ASP file can include files
with extensions .asp,.htm,.html or any other extension also.
CSC India
26
27
Response
Server
Browser
Request : The Browser/Client sends an input to the Web Server. This can
be used to gain access to any information that is passed with an HTTP
request. This includes parameters passed from an HTML form using either
the POST method or the GET method.
Response : Output is send back to the Browser from the Web Server.
CSC India
28
ASP Objects:
CSC India
29
ASP Objects
Request
Session
Response
Server
Application
ObjectContext
Error
CSC India
30
Request Object
Collections
QueryString
Form
ServerVariables
Properties
TotalBytes
CSC India
31
CSC India
32
CSC India
33
CSC India
34
35
CSC India
36
CSC India
37
CSC India
38
CSC India
39
CSC India
40
Counter = Request.TotalBytes
Counter - Specifies a variable to receive the total number of bytes that the
client sends in the request.
Example:
<% Dim bytecount
bytecount = Request.TotalBytes %>
CSC India
41
ASP Objects
Request
Session
Response
Server
Application
ObjectContext
Error
CSC India
42
Response Object
Response
It can be used to send output to the client.
Its method and properties control how the information is sent to the client.
CSC India
43
Response Object
Properties
Methods
Buffer
CacheControl
ContentType
Expires
ExpiresAbsolute
IsClientConnected
Status
AddHeader
AppendToLog
Clear
End
Flush
Redirect
Write
CSC India
44
45
CSC India
46
CSC India
47
CSC India
48
49
CSC India
50
CSC India
51
CSC India
52
CSC India
53
CSC India
54
CSC India
55
CSC India
56
57
58
ASP Objects
Request
Session
Response
Server
Application
ObjectContext
Error
CSC India
59
ASP Application
Application Object
An Application on the Web is a group of files(ASP/HTML,etc). The files
work together to perform some purpose. The Application object in ASP is
used to tie these files together.
The Application object is used to store variables and access variables
from any page (like database connection information). The Application
object is used to share information among all users of a given
application.This information can be changed in one page and automatically
gets reflected on all other pages.
They are similar to the Session object but the difference is that all users
share ONE Application object, while there is only one Session object
for each user.
CSC India
60
Application Object
ASP Application
ASP Application consists of:
Virtual directory on a Web Server.
All the folders and files within that Virtual directory.
Global.asa file in that Virtual directory.
The Application object is used to share information among all users of a
given application.
CSC India
61
ASP Application
Store and Retrieve Variable Values
Application variables can be accessed and changed by any page in the
application.
Creating Application variables:
<% Application(Greetings")=Welcome %>
Retrieving an Application variable:
<% Response.Write(Application("Greetings")) %>
Once an Application variable has been assigned a value, it retains that
value till the Web-Server shuts down.
CSC India
62
Application Object
Collections
Methods
Events
Contents
Lock
UnLock
OnStart
OnEnd
CSC India
63
CSC India
64
CSC India
65
66
CSC India
67
CSC India
68
CSC India
69
ASP Objects
Request
Session
Response
Server
Application
ObjectContext
Error
CSC India
70
ASP Session
The Session Object
When you are working with a window application, you open it, do some
changes and then you close it. This is much like a Session. The computer
knows who you are. It knows when you start the application and when you
end.
But on the internet there is one problem: The Web Server does not know
who you are and what you do because the HTTP Protocol is a stateless
Protocol and the address doesn't maintain state.
ASP solves this problem by creating a unique Cookie for each user. The
cookie is sent to the client and it contains information that identifies the
user. This interface is called the Session object.
CSC India
71
ASP Session
The Session Object
The Session object is used to store information about each user
entering the Web-Site and are available to all pages in one application.
Common information stored in session variables are users name, id, and
preferences.
The server creates a new Session object for each new user, and
destroys the Session object when the session expires or is abandoned
or the user logs out.
CSC India
72
ASP Session
Store and Retrieve Variable Values
The most important thing about the Session object is that you can store
variables in it, like this:
<% Session("username")=Tripti"
Session("age")=24 %>
When the value is stored in a session variable it can be reached from any
page in the ASP application by using: Session("username"):
Welcome <%Response.Write(Session("username"))%>
You can also store user preferences in the Session object, and then access
that preference to choose what page to return to the user.
CSC India
73
Session Object
Collections
Properties
Methods
Events
Contents
SessionID
TimeOut
Abandon
OnStart
OnEnd
CSC India
74
CSC India
75
ASP Session
Looping Through the Contents
You can loop through the Contents collection, to see what is stored in it:
<% dim i
For Each i in Session.Contents
Response.Write(i & "<br>")
Next %>
If you do not know the number of items in the Contents collection, you can
use the Count property:
<% dim i,j
j=Session.Contents.Count
Response.Write("Session variables:" & j)
For i=1 to j
Response.Write(Session.Contents(i) & "<br>")
Next %>
CSC India
76
77
CSC India
78
CSC India
79
CSC India
80
CSC India
81
Global.asa file
The Global.asa file
The Global.asa file must be stored in the root directory of the ASP
application which is identified as the virtual directory and each application
having only one Global.asa. This file is an optional file containing
declarations of objects, variables, and methods that can be accessed by
every page in an ASP application.
Global.asa files can contain only the following:
Application events
Session events
<object> declarations
TypeLibrary declarations
Changes to the Global.asa file require a restart of the server to recognize
them.
CSC India
82
Global.asa file
The Global.asa contains four types of Standard Events:
Application_OnStart - This event occurs when the FIRST user calls the
first page from an ASP application. This event occurs after Web server is
restarted or after the Global.asa file is edited as changes to the file require
a restart of the server to recognize them. Eg. DB Connectivity.
Application_OnEnd - This event occurs after the LAST user has ended
the session, typically when a Web Server stops. Eg. Delete records to
clean up settings after the Application stops or write information to log files.
Session_OnStart - This event occurs EVERY time a new user requests
the first page in the ASP application.Eg. Login page to be displayed first
time a user enters.
Session_OnEnd - This event occurs EVERY time a user ends a session.
A user ends a session after a page has not been requested by the user for
a specified time (by default this is 20 minutes). A session also ends if the
user closes the web browser, or goes to someone else's web page.
CSC India
83
Global.asa file
Standard format of Global.asa
Subroutines are created to handle these events in the Global.asa file:
<script language="vbscript"
sub Application_OnStart
......some vbscript
end sub
sub Application_OnEnd
......some vbscript
end sub
sub Session_OnStart
......some vbscript
end sub
sub Session_OnEnd
......some vbscript
end sub
</script>
runat="server">
code
code
code
code
CSC India
84
Global.asa file
Restrictions
Restrictions on what you can include in the Global.asa file:
Text cant be displayed in the Global.asa file. This file can't display
information or any output like HTML tags or Response.Write(). In fact the
file itself is never displayed.
You can not use the #include directive in Global.asa.
You can only use Server and Application objects in the Application_OnStart
and Application_OnEnd subroutines. In the Session_OnEnd subroutine,
you can use Server, Application, and Session objects. In the
Session_OnStart subroutine you can use any built-in object
CSC India
85
86
ASP Objects
Request
Session
Response
Server
Application
ObjectContext
Error
CSC India
87
Server Object
Server Object
The Server object provides access to methods and properties on the
Server
It enables to work with external Objects registered on the Server including
Components that are bundled with IIS.
CSC India
88
Server Object
Properties
Methods
ScriptTimeOut
CreateObject
Execute
HTMLEncode
MapPath
Transfer
URLEncode
CSC India
89
CSC India
90
%>
Destroying an object
<%Set MyObj = Nothing%>
CSC India
91
CSC India
92
CSC India
93
CSC India
94
CSC India
95
96
CSC India
97
98
ASP Objects
Request
Session
Response
Server
Application
ObjectContext
Error
CSC India
99
ObjectContext Object
ObjectContext Object
This object is used to either commit or abort a transaction
managed by Component Services like database transactions that
have been initiated by a script contained in an ASP page.
To use the ObjectContext object, the ASP file must contain the
@TRANSACTION directive.That means the page runs in a
transaction and does not finish processing until the transaction
either succeeds completely or fails.
If a script contains the @TRANSACTION directive, it must be the
very first line in the .asp file, otherwise an error is generated.
CSC India
100
ObjectContext Object
@TRANSACTION directive
Syntax :
<%@ TRANSACTION = value %>
Value : A string that indicates the type of transaction support
Possible values are:
Required
CSC India
101
ObjectContext Object
Methods
Events
SetAbort
SetComplete
OnTransactionAbort
OnTransactionCommit
CSC India
102
SetComplete Method
The SetComplete method declares that the script is not aware of
any reason for the transaction not to complete. But if all the
transactional components in script call SetComplete the transaction
will complete and the resources are updated.
Syntax:
ObjectContext.SetComplete
CSC India
103
104
ASP Objects
Request
Session
Response
Server
Application
ObjectContext
Error
CSC India
105
Error Object
ASPError Object
The ASPError object is implemented in ASP3.0 and it is only
available in IIS5.0
The ASPError object is used to get information of any error that
occurs in scripts in an ASP page.
The ASPError object is created when Server.GetLastError is
called, so the error information can only be accessed by using the
Server.GetLastError method.
CSC India
106
Error Object
ASPError Object
Syntax :
<% ASPError.property %>
Property is read-only giving information about the error.
ASPCode : Returns an error code generated by IIS.
Source
: Returns the actual source code of the line that caused the error.
File
Line
: Indicates the line within the .asp file that generated the error.
Column
: Indicates column position in the .asp file that generated the error.
Description
CSC India
107
THANK YOU
CSC India
108