ASP Object
ASP Object
Examples
Write text with ASP
This example demonstrates how to write text with ASP.
Format text with HTML tags in ASP
This example demonstrates how to combine text and HTML tags with ASP.
Redirect the user to a different URL
This example demonstrates how to redirect the user to a different URL.
Show a random link
This example demonstrates a link, each time you load the page, it will display one of
two links: W3Schools.com! OR Refsnesdata.no! There is a 50% chance for each of
them.
Controlling the buffer
This example demonstrates how you can control the buffer.
Clear the buffer
This example demonstrates how you can clear the buffer.
End a script in the middle of processing and return the result
This example demonstrates how to end a script in the middle of processing.
Set how many minutes a page will be cached in a browser before it expires
This example demonstrates how to specify how many minutes a page will be cached
in a browser before it expires.
Set a date/time when a page cached in a browser will expire
This example demonstrates how to specify a date/time a page cached in a browser will
expire.
Check if the user is still connected to the server
This example demonstrates how to check if a user is disconnected from the server.
Set the type of content
This example demonstrates how to specify the type of content.
Response Object
The ASP Response object is used to send output to the user from the server. Its
collections, properties, and methods are described below:
Collections
Collection
Description
Cookies
Properties
Property
Description
Buffer
CacheControl
Charset
ContentType
Expires
ExpiresAbsolute
IsClientConnected
Pics
Status
Methods
Method
Description
AddHeader
AppendToLog
BinaryWrite
Clear
End
Flush
Redirect
Write
have the same name. It shows how to separate input fields with equal names from
each other. It also shows how to use the Count keyword to count the "name" property.
The form uses the post method.
A form with radio buttons
This example demonstrates how to interact with the user through radio buttons, with
the Form collection. The form uses the post method.
A form with checkboxes
This example demonstrates how to interact with the user through checkboxes, with
the Form collection. The form uses the post method.
Other Examples
Get the server variables
This example demonstrates how to find out the visitors (yours) browser type, IP
address, and more with the ServerVariables collection.
Create a welcome cookie
This example demonstrates how to create a Welcome Cookie with the Cookies
Collection.
Find the total number of bytes the user sent
This example demonstrates how to use the TotalBytes property to find out the total
number of bytes the user sent in the Request object.
Request Object
When a browser asks for a page from a server, it is called a request. The ASP Request
object is used to get information from the user. Its collections, properties, and methods
are described below:
Collections
Collection
Description
ClientCertificate
Cookies
Form
Contains all the form (input) values from a form that uses the
post method
QueryString
ServerVariables
Properties
Property
Description
TotalBytes
Returns the total number of bytes the client sent in the body of
the request
Methods
Method
Description
BinaryRead
Retrieves the data sent to the server from the client as part of a
post request and stores it in a safe array
Application Object
An application on the Web may be a group of ASP files. The ASP 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 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.
The Application object should hold information that will be used by many pages in
the application (like database connection information). This means that you can
access the information from any page. It also means that you can change the
information in one place and the changes will automatically be reflected on all pages.
The Application object's collections, methods, and events are described below:
Collections
Collection
Description
Contents
StaticObjects
Methods
Method
Description
Contents.Remove
Contents.RemoveAll()
Lock
Unlock
Events
Event
Description
Application_OnEnd
Occurs when all user sessions are over, and the application
ends
Application_OnStart
Examples
Set and return the LCID
This example demonstrates the "LCID" property. This property sets or returns an
integer that specifies a location or region. Contents like date, time, and currency will
be displayed according to that location or region.
Return the SessionID
This example demonstrates the "SessionID" property. This property returns a unique
id for each user. The id is generated by the server.
A session's timeout
This example demonstrates the "Timeout" property. This example sets and returns the
timeout (in minutes) for the session.
Session Object
When you are working with an 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 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.
The Session object is used to store information about, or change settings for a user
session. Variables stored in the Session object hold information about one single user,
and are available to all pages in one application. Common information stored in
session variables are name, id, and preferences. The server creates a new Session
object for each new user, and destroys the Session object when the session expires.
The Session object's collections, properties, methods, and events are described below:
Collections
Collection
Description
Contents
StaticObjects
Properties
Property
Description
CodePage
LCID
SessionID
Timeout
Methods
Method
Description
Abandon
Contents.Remove
Contents.RemoveAll()
Events
Event
Description
Session_OnEnd
Session_OnStart
Examples
When was a file last modified?
Checks when this file was last modified.
Open a text file for reading
This example opens the file "Textfile.txt" for reading.
Homemade hit counter
This example reads a number from a file, adds 1 to the number, and writes the number
back to the file.
Server Object
The ASP Server object is used to access properties and methods on the server. Its
properties and methods are described below:
Properties
Property
Description
ScriptTimeout
Methods
Method
Description
CreateObject
Execute
GetLastError()
HTMLEncode
MapPath
Transfer
URLEncode
Properties
Property
Description
ASPCode
ASPDescription
Category
Returns the source of the error (was the error generated by ASP?
By a scripting language? By an object?)
Column
Returns the column position within the file that generated the
error
Description
File
Returns the name of the ASP file that generated the error
Line
Number
Source
Returns the actual source code of the line where the error
occurred
Examples
Does a specified file exist?
This example demonstrates how to first create a FileSystemObject Object, and then
use the FileExists method to check if the file exists.
Does a specified folder exist?
This example demonstrates how to use the FolderExists method to check if a folder
exists.
Does a specified drive exist?
This example demonstrates how to use the DriveExists method to check if a drive
exists.
Get the name of a specified drive
This example demonstrates how to use the GetDriveName method to get the name of
a specified drive.
Get the name of the parent folder of a specified path
This example demonstrates how to use the GetParentFolderName method to get the
name of the parent folder of a specified path.
Get the file extension
This example demonstrates how to use the GetExtensionName method to get the file
extension of the last component in a specified path.
Get file name
This example demonstrates how to use the GetFileName method to get the file name
of the last component in a specified path.
Get the base name of a file or folder
This example demonstrates how to use the GetBaseName method to return the base
name of the file or folder, in a specified path.
Properties
Property
Description
Drives
Methods
Method
Description
BuildPath
CopyFile
CopyFolder
CreateFolder
CreateTextFile
DeleteFile
DeleteFolder
DriveExists
FileExists
FolderExists
GetAbsolutePathName
Returns the complete path from the root of the drive for
the specified path
GetBaseName
GetDrive
GetDriveName
GetExtensionName
GetFile
GetFileName
GetFolder
GetParentFolderName
GetSpecialFolder
GetTempName
MoveFile
MoveFolder
OpenTextFile
Examples
Read textfile
This example demonstrates how to use the OpenTextFile method of the
FileSystemObject to create a TextStream Object. The ReadAll method of the
TextStream Object reads from the opened text file.
Read only a part of a textfile
This example demonstrates how to only read a part of a TextStream file.
Read one line of a textfile
This example demonstrates how to read one line from a TextStream file.
Read all lines from a textfile
This example demonstrates how to read all the lines from a TextStream file.
To create an instance of the TextStream object you can use the CreateTextFile or
OpenTextFile methods of the FileSystemObject object, or you can use the
OpenAsTextStream method of the File object.
The TextStream object's properties and methods are described below:
Properties
Property
Description
AtEndOfLine
AtEndOfStream
Column
Line
Methods
Method
Description
Close
Read
ReadAll
ReadLine
Reads one line from a TextStream file and returns the result
Skip
SkipLine
Write
WriteLine
WriteBlankLines
Examples
Get the available space of a specified drive
This example demonstrates how to first create a FileSystemObject object, and then
use the AvailableSpace property to get the available space of a specified drive.
Get the free space of a specified drive
This example demonstrates how to use the FreeSpace property to get the free space of
a specified drive.
Get the total size of a specified drive
This example demonstrates how to use the TotalSize property to get the total size of a
specified drive.
Dim fs,d
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set d=fs.GetDrive("c:")
Response.Write("Drive " & d & ":")
Response.Write("Total size in bytes: " & d.TotalSize)
set d=nothing
set fs=nothing
%>
Output:
Drive c: Total size in bytes: 4293563392
The Drive object's properties are described below:
Properties
Property
Description
AvailableSpace
DriveLetter
DriveType
FileSystem
FreeSpace
IsReady
Path
RootFolder
SerialNumber
ShareName
TotalSize
VolumeName
Examples
When was the file created?
This example demonstrates how to first create a FileSystemObject object, and then
use the DateCreated property of the File object to get the date and time a specified file
was created.
When was the file last modified?
This example demonstrates how to use the DateLastModified property to get the date
and time a specified file was last modified.
When was the file last accessed?
This example demonstrates how to use the DateLastAccessed property to get the date
and time a specified file was last accessed.
Return the attributes of a specified file
This example demonstrates how to use the Attributes property to return the attributes
of a specified file.
Output:
File created: 9/19/2001 10:01:19 AM
Properties
Property
Description
Attributes
DateCreated
Returns the date and time when a specified file was created
DateLastAccessed
Returns the date and time when a specified file was last
accessed
DateLastModified
Returns the date and time when a specified file was last
modified
Drive
Name
ParentFolder
Returns the folder object for the parent of the specified file
Path
ShortName
ShortPath
Size
Type
Methods
Method
Description
Copy
Delete
Move
OpenAsTextStream
To work with the properties and methods of the Folder object, you will have to create
an instance of the Folder object through the FileSystemObject object. First; create a
FileSystemObject object and then instantiate the Folder object through the GetFolder
method of the FileSystemObject object.
The following code uses the GetFolder method of the FileSystemObject object to
instantiate the Folder object and the DateCreated property to return the date when the
specified folder was created:
<%
Dim fs,fo
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set fo=fs.GetFolder("c:\test")
Response.Write("Folder created: " & fo.DateCreated)
set fo=nothing
set fs=nothing
%>
Output:
Folder created: 10/22/2001 10:01:19 AM
The Folder object's collections, properties, and methods are described below:
Collections
Collection
Description
Files
SubFolders
Properties
Property
Description
Attributes
DateCreated
Returns the date and time when a specified folder was created
DateLastAccessed
Returns the date and time when a specified folder was last
accessed
DateLastModified
Returns the date and time when a specified folder was last
modified
Drive
Returns the drive letter of the drive where the specified folder
resides
IsRootFolder
Name
ParentFolder
Path
ShortName
ShortPath
Size
Type
Methods
Method
Description
Copy
Delete
Move
CreateTextFile
Examples
Does a specified key exist?
This example demonstrates how to first create a Dictionary object, and then use the
Exists method to check if a specified key exists.
Return an array of all items
This example demonstrates how to use the Items method to return an array of all the
items.
Return an array of all keys
This example demonstrates how to use the Keys method to return an array of all the
keys.
Return the value of an item
This example demonstrates how to use the Item property to return the value of an
item.
Set a key
This example demonstrates how to use the Key property to set a key in a Dictionary
object.
Return the number of key/item pairs
This example demonstrates how to use the Count property to return the number of
key/item pairs.
The following example creates a Dictionary object, adds some key/item pairs to it,
and retrieves the item value for the key gr:
<%
Dim d
Set d=Server.CreateObject("Scripting.Dictionary")
d.Add "re","Red"
d.Add "gr","Green"
d.Add "bl","Blue"
d.Add "pi","Pink"
Response.Write("The value of key gr is: " & d.Item("gr"))
%>
Output:
The value of key gr is: Green
Properties
Property
Description
CompareMode
Dictionary object
Count
Item
Key
Methods
Method
Description
Add
Exists
Items
Keys
Remove
RemoveAll