ASP - The Server Object

Properties

  • ScriptTimeout

The Server object is designed to help you carry out various tasks on the server; it exposes its functionality with methods. It has a single property, ScriptTimeout.

ScriptTimeout

This is the Server object's single property and it specifies the amount of time for which the server will process the script. If this time interval is exceeded and the script is still executing, the server will abort the execution of the script.

Syntax:

The syntax of the ScriptTimeout property is:

Server.ScriptTimeout = nSeconds

nSeconds is the maximum number of seconds you allocate to the execution of the script. Its default value is 90 seconds, which is plenty of time for a typical script. If your script is unusually complicated or it executes very complicated queries against a database, you may have to increase the value of this property.


Methods

  • CreateObject
  • Execute
  • HTMLEncode
  • GetLastError
  • MapPath

The Server ofject exposes several methods, the most important of them bejing the createObject and GetLastError methods. The CreateObject lets you call external components. The GetLastError retrieves information about the last error. The GetLastMethod returns an ASPError object, which is described in the last section of this chapter.

CreateObject

The CreateObject method creates an instnce of an object (a component like a Class, an application, or a scripting object like the File Access component) and returns an object variable that represents the object.

Syntax:

The syntax of the CreateObject method is:

Set objectName = Server.CreateObject(progID)

objectName is the name of the object variable that will reference the object instantiated with the CreateObject method.

Execute

The Execute method interrupts the execution of the current script and temproarily transsfers control to another script. After the script being called completes its executionm control is automatically returned to the calling script, which resumes with the statement following the call of the Execute method.

Syntax:

The syntax of the Execute method is simply Server.Execute url.url is the address of the page to which you want to transfer control.

HTMLEncode

Have you ever tried to display HTML code on a Web page? It's a very complicated process, because all HTML tags must be carefully coded in hexadecimal notation. If not, the browser will interpret them, instead of displaying them. To transfer a string with HTML tags to the client and prevent the browser from processing them, you must encode all HTML tags. You can either do so by hand or use the HTMLEncode method.

Syntax:

The syntax of the HTMLEncode method is:

Server. HTMLEncode HTMLstring

HTMLstring is the string to be HTML encoded and, presumably, it contains HTML tags. Use the HTMLEncode method to display the source code of HTML pages, or the results of queries that may contain special symbols.

GetLastError

ASP 3 provides better error-handling capabilities, but it still can't trap errors with a statement like On Error Goto. The only way to trap an error is to insert the On Error Resume Next statement in the code and test the Err.Number property after ech statement that may fail. The problem with this approach is that you must read the error information immediately ater the error has occurred. If another statement is executed successfully, the Err object is rest to reflect the successful execution of the statement.

Syntax:

The syntax of the GetLastError method is:

Set objError = Server.GetLastError()

objError is an ASPError object variable that exposes a number of properties with information about the last error. To find out the description of the error, use the Description property of the objError variable:

Response.Write objError.Description

MapPath

In processing a script on the server, you may need to know the absolute path of a virtual directory. The FileSystemObject component, for example, doesn't recognize the virtual folders. To retrieve the absolute path of a file in a virtual directory, use the MapPath method.

Syntax:

The syntax of the MapPath method is:

absPath = Server.MapPath(relPath)

relPath is the relative path of a file and the MapPath method returns the file's absolute path. If you call the MapPath method from within the ProcessFiles.asp script, which resides in the SCRIPTS folder under the Web's root foler, the method will return the SCRIPTS folder's absolute path:

<%
Response.Write Server.MapPath("ProcessFiles.asp")
%>