ASP Quick Reference
ASP Quick Reference from Example Tutorial. Print it out and keep it in your pocket for use at any time.
Basic Syntax
ASP scripts are surrounded by <% and %>. To write output to the browser:
<html>
<body>
<%
response.write("Hello World!")
%>
</body>
</html>
The default language in ASP is VBScript. To use another scripting language, insert a language declaration at the top of the ASP page:
<%@ language="javascript" %>
<html>
<body>
<%
....
%>
Forms and User Input
Request.QueryString is used to collect values from a form with method="get". Information sent from a form using the GET method is visible to all users (it appears in the browser's address bar) and has limits on the amount of information that can be sent.
Request.Form is used to collect values from a form using method="post". Information sent from a form using the POST method is invisible to users and has no limits on the amount of information that can be sent.
ASP Cookies
Cookies are commonly used to identify users. A cookie is a small file that the server leaves on the user's computer. Whenever the same computer requests a page through a browser, it will send the cookie.
The Response.Cookies command is used to create a cookie:
<%
Response.Cookies("firstname")="Alex"
Response.Cookies("firstname").Expires="May 10,2002"
%>
Note:The Response.Cookies command must appear before the <html> tag!
The "Request.Cookies" command is used to retrieve the cookie value:
<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>
Including Files
By using the #include directive, you can insert the contents of another ASP file into an ASP file before the server executes it. The #include directive is used to create functions, headers, footers, or other elements that need to be reused on multiple pages.
Syntax:
<!--#include virtual="somefile.inc"-->
Or
<!--#include file ="somefile.inc"-->
Use the virtual keyword to indicate a path that begins with a virtual directory. If a file named "header.inc" is located in the virtual directory /html, the following line of code will insert the contents of the "header.inc" file:
<!-- #include virtual ="/html/header.inc" -->
Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the referencing file. If you have a file in the html directory, and the "header.inc" file is located in the html directory, the following line of code will insert the contents of the "header.inc" file into your file:
<!-- #include file ="headersheader.inc" -->
Use the file keyword with the syntax (..) to reference a file in a higher-level directory.
Global.asa
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.
Note:The Global.asa file must be stored 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 Directive
Application and Session Events
In Global.asa, you can tell the application and session objects what to do when the application/session starts and what to do when the application/session ends. The code that accomplishes this task is placed in event handlers.Note:Since we cannot use ASP script delimiters (<% and %>) to insert scripts in the Global.asa file, we need to place the subroutines inside HTML <script> tags:
<script language="vbscript" runat="server">
sub Application_OnStart
' some code
end sub
sub Application_OnEnd
' some code
end sub
sub Session_OnStart
' some code
end sub
sub Session_OnEnd
' some code
end sub
</script>
<object> Declarations
Objects with session or application scope can be created in the Global.asa file by using the <object> tag.Note:The <object> tag should be placed outside the <script> tag!
Syntax:
<object runat="server" scope="scope" id="id"
{progid="progID"|classid="classID"}>
.......
</object>
TypeLibrary Declaration
TypeLibrary (type library) is a container that holds DLL files corresponding to COM objects. By including a call to the TypeLibrary in the Global.asa file, you can access the constants of COM objects, and ASP code can also report errors better. If your Web application depends on COM objects whose data types are already declared in a type library, you can declare the type library in Global.asa.
Syntax:
<!--METADATA TYPE="TypeLib"
file="filename"
uuid="typelibraryuuid"
version="versionnumber"
lcid="localeid"
-->
Session Object
The Session object is used to store information about a user session, or to change user session settings. Variables stored in the Session object store information about a single user and are available to all pages in an application.
Collections
- Contents - Contains all items appended to the session through script commands.
- StaticObjects - Contains all objects 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 used when displaying dynamic content.
- LCID - Sets the locale identifier used to display dynamic content.
- SessionID - Returns the session ID.
- Timeout - Sets or returns the timeout period for the session.
Methods
- Abandon - Destroys all objects 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 stores information that will be used by many pages in an application (such as database connection information).
Collections
- Contents - Contains all items appended to the application through script commands.
- StaticObjects - Contains all objects 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 modifying properties in the Application object.
- Unlock - Allows users to modify properties in 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, creates the cookie and sets the specified value.
Properties
- Buffer - Specifies whether to buffer 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. If you want to set this property, it should be placed before the <html> tag in the ASP file.
- CacheControl - Sets whether proxy servers can cache the output generated by ASP. If set to Public, proxy servers will cache the page.
- Charset(charset_name) - Appends the name of the character set (such as "ISO8859-1") to the content-type header in the Response object.
- ContentType - Sets the HTTP content type of the Response object (such as "text/html", "image/gif", "image/jpeg", "text/plain"). The default is "text/html".
- Expires - Sets the browser cache time (in minutes) before the page expires.
- ExpiresAbsolute - Sets the date and time when the page cached in the browser expires.
- IsClientConnected - Indicates whether the client has disconnected from the server.
- Pics(pics_label) - Appends a value to the PICS tag in 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.
- AppendToLog string - Appends a string to the end of the server log entry.
- BinaryWrite(data_to_write) - Writes data directly to the output without any character conversion.
- Clear - Clears the buffered output. Use this method to handle errors. If Response.Buffer is not set to true, this method will produce a run-time error.
- End - Stops processing the script and returns the current result.
- Flush - Sends buffered output immediately. If Response.Buffer is not set to true, this method will produce a run-time error.
- Redirect(url) - Redirects the user to another URL.
- Write(data_to_write) - Writes text to the user.
Request Object
When the browser requests a page from the server, this behavior is called a request. The Request object is used to obtain information from the user.
Collections
- ClientCertificate - Contains all field values stored in the client certificate.
- Cookies(name) - Contains all cookie values sent in the HTTP request.
- Form(element_name) - Contains all form (input) values sent by a form using 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 request body.
Methods
- BinaryRead - Retrieves 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 amount of time, in seconds, a script can run before it is terminated.
Methods
- CreateObject(type_of_object) - Creates an instance of an object.
- Execute(path) - Executes another ASP file from inside an ASP file. After the called ASP file finishes executing, control returns to the original ASP file.
- GetLastError() - Returns an ASPError object that describes the error condition 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. After the transfer, program control does not return to the original ASP file.
- URLEncode(string) - Applies URL encoding rules to a string.