The ASP Server object allows access to resources of the web server.

Basic Usage

A component can be instantiated by having it set to an object variable. Once instantiated, the properties, methods, and events of the object can be accessed. EG: This creates and uses a File System Object:

Set fso = CreateObject("Scripting.FileSystemObject")
Set myFile = fso.CreateTextFile("c:\Inetpub\wwwroot\test.htm", True)
myFile.WriteLine("<html><head><title>hi</title></head>")
myFile.WriteLine("<body><p>hi</p></body></html>")
myFile.Close
Set fso = Nothing
Set myFile = Nothing

Another ASP page can be executed from within another.

Response.Write "Heres stuff from another ASP page:"
Server.Execute "another.asp"

The Server object offers two methods that encode a string;

Response.Write(Server.HTMLEncode ("<br />"))
'Returns: &lt;br /&gt;
<% Response.Write(Server.URLEncode ("Hello world!")) %>
'Returns: Hello+world%21

Server.MapPath can take a real or imagined relative or virtual path and converts it to a command line path.

Server.MapPath "index.htm"
'Returns: c:\inetpub\wwwroot\sub\index.htm

Properties

ScriptTimeout

Sets number of seconds that a ASP process can run on the server before it terminates. The default value is 90 seconds.

Methods

CreateObject(ProgID)

Creates instance of a server component, including ActiveX components.

Here is the format for ProgID:

[Vendor.]Component[.Version]

The created object can be pointed to by a variable in any ASP page. If the object is to have session or application scope, then either:

Execute(PathToASP)

Calls another ASP file and executes it. Sort of like an SSI (Server-Side Include) and sort of like a procedure call.

GetLastError()

Returns an ASP ASPError object. This method must be used before any content has been sent to the client.

If a 500;100 custom error has occured, then IIS automatically transfers from the erring page to the default error handling error page located at \iishelp\common\500-100.asp.

HTMLEncode(string)

Returns a string where the following characters are encoded:

MapPath(path)

Translates the relative or virtual path to its command line path on the server. In these example, assume that the file making the calls is c:\inetpub\wwwroot\sub\me.asp

If path does not start with a forward (/) or backward slash (\), then the returned path is relative to the file making the call to Server.MapPath:

Server.MapPath "fake.htm"
'Returns: c:\inetpub\wwwroot\sub\fake.htm
Server.MapPath "marine"
'Returns: c:\inetpub\wwwroot\sub\marine
Server.MapPath "marine/fake.htm"
'Returns: c:\inetpub\wwwroot\sub\marine\fake.htm
Server.MapPath "../marine
'Returns: c:\inetpub\wwwroot\marine
Server.MapPath "../marine/fake.htm"
'Returns: c:\inetpub\wwwroot\marine\fake.htm

If path does start with a forward (/) or backward slash (\), then the returned path is path  appended to the virtual path (i.e. the root path of the site):

Server.MapPath "\fake.htm"
'Returns: c:\inetpub\wwwroot\fake.htm
Server.MapPath "/marine"
'Returns: c:\inetpub\wwwroot\marine
Server.MapPath "/marine/fake.htm"
'Returns: c:\inetpub\wwwroot\marine\fake.htm

Here are two particular "tricks":

Server.MapPath Request.ServerVariables("PATH_INFO")
'Returns: c:\inetpub\wwwroot\sub\me.asp 'i.e. of the current file
Server.MapPath "\"
'Returns: c:\inetpub\wwwroot 'i.e. of the virtual root

Transfer(PathToASP[, PreserveForm])

New with IIS 5.0. Sort of like Response.Redirect(URL) except that execution is transferred to another file on the server, instead of going back to the clients browser and having them execute a new request. PathToASP cannot contain a query string.

Server.Transfer() can only transfers to the same server, whereas Response.Redirect() can redirect to sites not on the same server. The target URL will also think that its URL is the URL where the Server.Transfer() method was used.

If PreserveForm is set to True, than the target URL has the query string and form info available to it.

Also all the state information of the built-in objects are also transferred to the new page. This includes the following:

URLEncode(URI)

Returns a string encoding certain characters (including spaces) in a string (usually a URI) with a 2 digit escape sequence (EG: escape("&") returns %26) representing the ISO Latin 1 encoding of the character.

The following characters are not encoded: * @ - _ + . /, plus ASCII alphanumeric.

This is equivalent to the escape() function built into JavaScript and the Escape() function built into VBS. ASP does not have an equivalent to the JS unescape() or the VBS Unescape() function.

Page Modified: (Hand noted: 2005-03-29 18:52:48Z) (Auto noted: 2010-12-24 22:46:36Z)