ASP Quick Reference
ASP Quick Reference from tutorialpro.org. Print it out, put it in your pocket, for quick access anytime.
Basic Syntax
ASP scripts are enclosed by <%
and %>
. Writing output to the browser:
<html>
The default language in ASP is VBScript. To use another scripting language, insert a language directive at the top of the ASP page:
<%@ language="javascript" %>
<%
Forms and User Input
Request.QueryString
is used to collect values from forms 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.
Request.Form
is used to collect values from forms 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.
ASP Cookies
Cookies are often used to identify users. 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.
The Response.Cookies
command is used to create a cookie:
<%
Note: The Response.Cookies
command must appear before the <html>
tag!
The Request.Cookies
command is used to retrieve a cookie value:
<%
Including Files
By using the #include directive, you can insert the content of another ASP file into the ASP file before the server executes it. The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.
Syntax:
<!--#include virtual="somefile.inc"-->
Use the keyword virtual
to indicate a path starting with a virtual directory. If a file named "header.inc" is located in the virtual directory /html, the following line will insert the content of "header.inc":
<!-- #include virtual ="/html/header.inc" -->
Use the keyword file
to indicate a relative path. The relative path starts with the directory containing the referencing file. If you have a file in the html directory and "header.inc" is located in the html headers, the following line will insert the content of "header.inc" into your file:
<!-- #include file ="headersheader.inc" -->
Use the keyword file
with the syntax (..) to reference files in higher-level directories.
Global.asa
The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that are accessible to all pages in an ASP application.
Note: The Global.asa file must be located in the root directory of the ASP application, and each application can have only one Global.asa file.
The Global.asa file can only contain the following:
- Application events
- Session events
<object>
declarations- TypeLibrary declarations
- #include directives
Application and Session Events
In Global.asa, you can instruct the application and session objects what to do when an application/session starts and ends. The code to accomplish this is placed in event handlers. Note: Since we cannot use ASP script delimiters (<% and %>) in the Global.asa file to insert scripts, we need to place subroutines inside the HTML <script>
tags:
<script language="vbscript" runat="server">
<object> Declaration
You can create objects with session or application scope in the Global.asa file using the <object>
tag. Note: The <object>
tag should be placed outside the <script>
tags!
Syntax:
<object runat="server" scope="scope" id="id"
TypeLibrary Declaration
A TypeLibrary is a container for the DLL files that correspond to COM objects. By including a call to the TypeLibrary in the Global.asa file, you can access the constants of the COM object, and the ASP code can report errors better. If your web application relies on COM objects that have data types declared in the type library, you can declare the type library in Global.asa.
Syntax:
<!--METADATA TYPE="TypeLib"
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.
Collections
- Contents - Contains all items that have been appended to the session via scripting commands.
- StaticObjects - Contains all objects that have been appended to the session using the HTML
<object>
tag. - Contents.Remove(item/index) - Removes an item from the Contents collection.
- Contents.RemoveAll() - Removes all items from the Contents collection.
Properties
- CodePage - Specifies the character set to be used for displaying dynamic content.
- LCID - Sets the locale identifier for displaying dynamic content.
- SessionID - Returns the session ID.
- Timeout - Sets or returns the timeout period for the session.
Methods
- Abandon - Destroys all objects stored in the Session object.
Application Object
A group of ASP files that work together to perform a task is called an application. The Application object is used to tie these files together. All users share one Application object. The Application object holds information that will be used by many pages in the application (like database connection information).
Collections
- Contents - Contains all items that have been appended to the application via scripting commands.
- StaticObjects - Contains all objects that have been appended to the application using the HTML
<object>
tag. - Contents.Remove - Removes an item from the Contents collection.
- Contents.RemoveAll - Removes all items from the Contents collection.
Methods
- Lock - Prevents users from changing the properties of the Application object.
- Unlock - Allows users to change the properties of the Application object.
Response Object
The Response object is used to send output from the server to the user.
Collections
- Cookies(name) - Sets the value of a cookie. If the cookie does not exist, it creates the cookie with the specified value.
Properties
- Buffer - Specifies whether to buffer the output. When output is buffered, the server blocks the response to the browser until all server scripts have been processed, or until the script calls the Flush or End method. This property should be set before the
<html>
tag in the ASP file. - CacheControl - Sets whether proxy servers can cache the output generated by ASP. If set to Public, the proxy server can cache the page.
- Charset(charset_name) - Appends a character set name (like "ISO8859-1") to the content type header in the Response object.
- ContentType - Sets the HTTP content type for the Response object (like "text/html", "image/gif", "image/jpeg", "text/plain"). The default is "text/html".
- Expires - Sets the time (in minutes) a page should be cached before expiring in the browser.
- ExpiresAbsolute - Sets the date and time a page cached in the browser should expire.
- IsClientConnected - Indicates whether the client has disconnected from the server.
- Pics(pics_label) - Appends a value to the PICS label of the response header.
- Status - Specifies the value of the status line returned by the server.
Methods
- AddHeader(name, value) - Adds a new HTTP header and value to the HTTP response.
- AppendToLogstring - Adds a string to the end of the server log entry.
- BinaryWrite(data_to_write) - Writes data to the output directly without any character conversion.
- Clear - Clears the buffered output. Use this method to handle errors. It will cause a run-time error if Response.Buffer is not set to true.
- End - Stops processing the script and returns the current result.
- Flush - Sends the buffered output immediately. It will cause a run-time error if Response.Buffer is not set to true.
- Redirect(url) - Redirects the user to another URL.
- Write(data_to_write) - Writes text to the user.
Request Object
When a browser requests a page from a server, this action is called a request. The Request object is used to get information from the user.
Collections
- ClientCertificate - Contains all field values in the client certificate.
- Cookies(name) - Contains all cookie values sent in the HTTP request.
- Form(element_name) - Contains all form (input) values sent from the form with the post method.
- QueryString(variable_name) - Contains all variable values in the HTTP query string.
- ServerVariables(server_variable) - Contains all server variable values.
Properties
- TotalBytes - Returns the total number of bytes sent by the client in the body of the request.
Methods
- BinaryRead - Retrieves the data sent from the client to the server as part of a post request.
Server Object
The Server object is used to access properties and methods on the server.
Properties
- ScriptTimeout - Sets or returns the maximum number of seconds a script is allowed to run before it is terminated.
Methods
- CreateObject(type_of_object) - Creates an instance of an object.
- Execute(path) - Executes another ASP file from within an ASP file. Control returns to the original ASP file after the called ASP file has finished executing.
- GetLastError() - Returns an ASPError object that describes the error state that has occurred.
- HTMLEncode(string) - Applies HTML encoding to a string.
- MapPath(path) - Maps a relative or virtual path to a physical path.
- Transfer(path) - Sends all state information to another file for processing. Control is not returned to the original ASP file after the transfer.
- URLEncode(string) - Applies URL encoding rules to a string.