Sei sulla pagina 1di 18

ASP Response Object

The ASP Response object is used to send output to the user from the server.

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.

Set the name of the character set


This example demonstrates how to specify the name of the character set.

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 Sets a cookie value. If the cookie does not exist, it will be created, and take
the value that is specified

Properties

Property Description
Buffer Specifies whether to buffer the page output or not
CacheControl Sets whether a proxy server can cache the output generated by ASP or not
Charset Appends the name of a character-set to the content-type header in the
Response object
ContentType Sets the HTTP content type for the Response object
Expires Sets how long (in minutes) a page will be cached on a browser before it
expires
ExpiresAbsolute Sets a date and time when a page cached on a browser will expire
IsClientConnected Indicates if the client has disconnected from the server
Pics Appends a value to the PICS label response header
Status Specifies the value of the status line returned by the server

Methods

Method Description
AddHeader Adds a new HTTP header and a value to the HTTP response
AppendToLog Adds a string to the end of the server log entry
BinaryWrite Writes data directly to the output without any character conversion
Clear Clears any buffered HTML output
End Stops processing a script, and returns the current result
Flush Sends buffered HTML output immediately
Redirect Redirects the user to a different URL
Write Writes a specified string to the output

ASP Request Object

The ASP Request object is used to get information from the user.

QueryString Collection Examples

Send query information when a user clicks on a link


This example demonstrates how to send some extra query information to a page within a link, and
retrieve that information on the destination page (which is, in this example, the same page).

A QueryString collection in its simplest use


This example demonstrates how the QueryString collection retrieves the values from a form. The
form uses the GET method, which means that the information sent is visible to everybody (in the
address field). The GET method also limits the amount of information that can be sent.

How to use information from forms


This example demonstrates how to use the values retrieved from a form. We use the QueryString
collection. The form uses the get method.
More information from a form
This example demonstrates what the QueryString contains if several input fields 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 get method.

Form Collection Examples

A form collection in its simplest use


This example demonstrates how the Form collection retrieves the values from a form. The form
uses the POST method, which means that the information sent is invisible to others, and it has no
limits (you can send a large amount of information).

How to use information from forms


This example demonstrates how to use the values retrieved from a form. We use the Form
collection. The form uses the post method.

More information from a form


This example demonstrates what the Form collection contains if several input fields 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 Contains all the field values stored in 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
ServerVariables Contains all the server variable values

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

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.

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 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

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

Event Description
Application_OnEnd Occurs when all user sessions are over, and the application ends
Application_OnStart Occurs before the first new session is created (when the Application
object is first referenced)

ASP 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.

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 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

Properties

Property Description
CodePage Specifies the character set that will be used when displaying dynamic
content
LCID 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
SessionID Returns a unique id for each user. The unique id is generated by the
server
Timeout Sets or returns the timeout period (in minutes) for the Session
object in this application

Methods

Method Description
Abandon Destroys a user session
Contents.Remove Deletes an item from the Contents collection
Contents.RemoveAll() Deletes all items from the Contents collection

Events

Event Description
Session_OnEnd Occurs when a session ends
Session_OnStart Occurs when a session starts

ASP Server Object

The ASP Server object is used to access properties and methods on the server.

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 Sets or returns the maximum number of seconds a script can run before it
is terminated

Methods

Method Description
CreateObject Creates an instance of an object
Execute Executes an ASP file from inside another ASP file
GetLastError() Returns an ASPError object that describes the error condition that occurred
HTMLEncode Applies HTML encoding to a specified string
MapPath Maps a specified path to a physical path
Transfer Sends (transfers) all the information created in one ASP file to a second ASP
file
URLEncode Applies URL encoding rules to a specified string

ASP ASPError Object

The ASPError object is used to display detailed information of any error that occurs in
scripts in an ASP page.

The ASPError Object

The ASPError object was implemented in ASP 3.0 and is available in IIS5 and later.

The ASPError object is used to display detailed 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.

The ASPError object's properties are described below (all properties are read-only):

Note: The properties below can only be accessed through the Server.GetLastError() method.

Properties

Property Description
ASPCode Returns an error code generated by IIS
ASPDescription Returns a detailed description of the error (if the error is ASP-related)
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 Returns a short description of the error
File Returns the name of the ASP file that generated the error
Line Returns the line number where the error was detected
Number Returns the standard COM error code for the error
Source Returns the actual source code of the line where the error occurred

ASP FileSystemObject Object

The FileSystemObject object is used to access the file system on the server.

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.

The FileSystemObject Object


The FileSystemObject object is used to access the file system on the server. This object can
manipulate files, folders, and directory paths. It is also possible to retrieve file system information
with this object.

The following code creates a text file (c:\test.txt) and then writes some text to the file:

<%
dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile("c:\test.txt",true)
fname.WriteLine("Hello World!")
fname.Close
set fname=nothing
set fs=nothing
%>

The FileSystemObject object's properties and methods are described below:

Properties

Property Description
Drives Returns a collection of all Drive objects on the computer

Methods

Method Description
BuildPath Appends a name to an existing path
CopyFile Copies one or more files from one location to another
CopyFolder Copies one or more folders from one location to another
CreateFolder Creates a new folder
CreateTextFile Creates a text file and returns a TextStream object that can be used
to read from, or write to the file
DeleteFile Deletes one or more specified files
DeleteFolder Deletes one or more specified folders
DriveExists Checks if a specified drive exists
FileExists Checks if a specified file exists
FolderExists Checks if a specified folder exists
GetAbsolutePathName Returns the complete path from the root of the drive for the
specified path
GetBaseName Returns the base name of a specified file or folder
GetDrive Returns a Drive object corresponding to the drive in a specified path
GetDriveName Returns the drive name of a specified path
GetExtensionName Returns the file extension name for the last component in a specified
path
GetFile Returns a File object for a specified path
GetFileName Returns the file name or folder name for the last component in a
specified path
GetFolder Returns a Folder object for a specified path
GetParentFolderName Returns the name of the parent folder of the last component in a
specified path
GetSpecialFolder Returns the path to some of Windows' special folders
GetTempName Returns a randomly generated temporary file or folder
MoveFile Moves one or more files from one location to another
MoveFolder Moves one or more folders from one location to another
OpenTextFile Opens a file and returns a TextStream object that can be used to
access the file

ASP TextStream Object

The TextStream object is used to access the contents of a text file.

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.

Skip a part of a textfile


This example demonstrates how to skip a specified number of characters when reading the
TextStream file.

Skip a line of a textfile


This example demonstrates how to skip a line when reading the TextStream file.

Return line-number
This example demonstrates how to return the current line number in a TextStream file.

Get column number


This example demonstrates how to get the column number of the current character in a file.

The TextStream Object

The TextStream object is used to access the contents of text files.

The following code creates a text file (c:\test.txt) and then writes some text to the file (the variable
f is an instance of the TextStream object):

<%
dim fs, f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateTextFile("c:\test.txt",true)
f.WriteLine("Hello World!")
f.Close
set f=nothing
set fs=nothing
%>

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 Returns true if the file pointer is positioned immediately before the end-of-
line marker in a TextStream file, and false if not
AtEndOfStream Returns true if the file pointer is at the end of a TextStream file, and false if
not
Column Returns the column number of the current character position in an input
stream
Line Returns the current line number in a TextStream file

Methods

Method Description
Close Closes an open TextStream file
Read Reads a specified number of characters from a TextStream file and returns
the result
ReadAll Reads an entire TextStream file and returns the result
ReadLine Reads one line from a TextStream file and returns the result
Skip Skips a specified number of characters when reading a TextStream file
SkipLine Skips the next line when reading a TextStream file
Write Writes a specified text to a TextStream file
WriteLine Writes a specified text and a new-line character to a TextStream file
WriteBlankLines Writes a specified number of new-line character to a TextStream file

ASP Drive Object

The Drive object is used to return information about a local disk drive or a network share.

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.

Get the drive letter of a specified drive


This example demonstrates how to use the DriveLetter property to get the drive letter of a specified
drive.

Get the drive type of a specified drive


This example demonstrates how to use the DriveType property to get the drive type of a specified
drive.

Get the file system of a specified drive


This example demonstrates how to use the FileSystem property to get the file system of a specified
drive.

Is the drive ready?


This example demonstrates how to use the IsReady property to check whether a specified drive is
ready.

Get the path of a specified drive


This example demonstrates how to use the Path property to get the path of a specified drive.

Get the root folder of a specified drive


This example demonstrates how to use the RootFolder property to get the root folder of a specified
drive.

Get the serialnumber of a specified drive


This example demonstrates how to use the Serialnumber property to get the serialnumber of a
specified drive.

The Drive Object

The Drive object is used to return information about a local disk drive or a network share. The Drive
object can return information about a drive's type of file system, free space, serial number, volume
name, and more.

Note: You cannot return information about a drive's content with the Drive object. For this purpose
you will have to use the Folder object.

To work with the properties of the Drive object, you will have to create an instance of the Drive
object through the FileSystemObject object. First; create a FileSystemObject object and then
instantiate the Drive object through the GetDrive method or the Drives property of the
FileSystemObject object.

The following example uses the GetDrive method of the FileSystemObject object to instantiate the
Drive object and the TotalSize property to return the total size in bytes of the specified drive (c:):

<%
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 Returns the amount of available space to a user on a specified drive or
network share
DriveLetter Returns one uppercase letter that identifies the local drive or a network
share
DriveType Returns the type of a specified drive
FileSystem Returns the file system in use for a specified drive
FreeSpace Returns the amount of free space to a user on a specified drive or network
share
IsReady Returns true if the specified drive is ready and false if not
Path Returns an uppercase letter followed by a colon that indicates the path
name for a specified drive
RootFolder Returns a Folder object that represents the root folder of a specified drive
SerialNumber Returns the serial number of a specified drive
ShareName Returns the network share name for a specified drive
TotalSize Returns the total size of a specified drive or network share
VolumeName Sets or returns the volume name of a specified drive

ASP File Object

The File object is used to return information about a specified file.

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.
The File Object

The File object is used to return information about a specified file.

To work with the properties and methods of the File object, you will have to create an instance of
the File object through the FileSystemObject object. First; create a FileSystemObject object and
then instantiate the File object through the GetFile method of the FileSystemObject object or
through the Files property of the Folder object.

The following code uses the GetFile method of the FileSystemObject object to instantiate the File
object and the DateCreated property to return the date when the specified file was created:

<%
Dim fs,f
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.GetFile("c:\test.txt")
Response.Write("File created: " & f.DateCreated)
set f=nothing
set fs=nothing
%>

Output:

File created: 9/19/2001 10:01:19 AM

The File object's properties and methods are described below:

Properties

Property Description
Attributes Sets or returns the attributes of a specified file
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 Returns the drive letter of the drive where a specified file or folder
resides
Name Sets or returns the name of a specified file
ParentFolder Returns the folder object for the parent of the specified file
Path Returns the path for a specified file
ShortName Returns the short name of a specified file (the 8.3 naming convention)
ShortPath Returns the short path of a specified file (the 8.3 naming convention)
Size Returns the size, in bytes, of a specified file
Type Returns the type of a specified file

Methods

Method Description
Copy Copies a specified file from one location to another
Delete Deletes a specified file
Move Moves a specified file from one location to another
OpenAsTextStream Opens a specified file and returns a TextStream object to access the file
ASP Folder Object

The Folder Object is used to return information about a specified folder.

The Folder Object

The Folder object is used to return information about a specified folder.

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 Returns a collection of all the files in a specified folder
SubFolders Returns a collection of all subfolders in a specified folder

Properties

Property Description
Attributes Sets or returns the attributes of a specified folder
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 Returns true if a folder is the root folder and false if not
Name Sets or returns the name of a specified folder
ParentFolder Returns the parent folder of a specified folder
Path Returns the path for a specified folder
ShortName Returns the short name of a specified folder (the 8.3 naming convention)
ShortPath Returns the short path of a specified folder (the 8.3 naming convention)
Size Returns the size of a specified folder
Type Returns the type of a specified folder

Methods

Method Description
Copy Copies a specified folder from one location to another
Delete Deletes a specified folder
Move Moves a specified folder from one location to another
CreateTextFile Creates a new text file in the specified folder and returns a TextStream
object to access the file

ASP Dictionary Object

The Dictionary object is used to store information in name/value pairs (referred to as key
and item)

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 Dictionary Object

The Dictionary object is used to store information in name/value pairs (referred to as key and item).
The Dictionary object might seem similar to Arrays, however, the Dictionary object is a more
desirable solution to manipulate related data.

Comparing Dictionaries and Arrays:

• Keys are used to identify the items in a Dictionary object


• You do not have to call ReDim to change the size of the Dictionary object
• When deleting an item from a Dictionary, the remaining items will automatically shift up
• Dictionaries cannot be multidimensional, Arrays can
• Dictionaries have more built-in functions than Arrays
• Dictionaries work better than arrays on accessing random elements frequently
• Dictionaries work better than arrays on locating items by their content

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

The Dictionary object's properties and methods are described below:

Properties

Property Description
CompareMode Sets or returns the comparison mode for comparing keys in a Dictionary object
Count Returns the number of key/item pairs in a Dictionary object
Item Sets or returns the value of an item in a Dictionary object
Key Sets a new key value for an existing key value in a Dictionary object

Methods

Method Description
Add Adds a new key/item pair to a Dictionary object
Exists Returns a Boolean value that indicates whether a specified key exists in the
Dictionary object
Items Returns an array of all the items in a Dictionary object
Keys Returns an array of all the keys in a Dictionary object
Remove Removes one specified key/item pair from the Dictionary object
RemoveAll Removes all the key/item pairs in the Dictionary object

Introduction to ADO

ADO can be used to access databases from your web pages.


Accessing a Database from an ASP Page

The common way to access a database from inside an ASP page is to:

1. Create an ADO connection to a database


2. Open the database connection
3. Create an ADO recordset
4. Open the recordset
5. Extract the data you need from the recordset
6. Close the recordset
7. Close the connection

What is ADO?

• ADO is a Microsoft technology


• ADO stands for ActiveX Data Objects
• ADO is a Microsoft Active-X component
• ADO is automatically installed with Microsoft IIS
• ADO is a programming interface to access data in a database

Where to go next?

If you want to study more ADO, read our ADO tutorial.

Potrebbero piacerti anche