www.tamertolba.4t.com

 

Chapter 6

The Server Object

 

This object's name suggests that it gives access to the Web server itself, which is not quite true. The properties and methods are more related to the Web server than the other built-in objects but are really just general utility functions.We will discuss below several properties and methods

 

6.1- ScriptTimeout:

This property controls how long an ASP script can execute on the server before it is considered to have timed out and an error message is returned to the browser. The default value for this property, which is contained in the Registry, is usually 90 seconds. This property can be used only to increase that value, and if you set the timeout to a value less than the default, the default will still be used.

Syntax :

               Server.ScriptTimeout = time(in seconds)

 

6.2- CreateObject:

This method of the Server object is the most powerful aspect of ASP,providing the capability to create instances of COM objects. Using the VBScript Set command, you can create an instance of a component and assign it to a variable for use in your pages. You can also take these instances and store them into the Application or Session objects to allow those objects to be used across

your ASP application.

Syntax:

               Server.CreateObject( progID )

 

progID specifies the type of object to create. The format for progID is [Vendor.]Component[.Version].

 

 

6.3- HTMLEncode:
Certain characters can't be written out directly to the browser. These characters, including < and >, as well as both double and single quotes, have special meaning to the browser and will not be displayed as you would expect. If you are entering text into a Web page editor, such as FrontPage, then these characters are automatically converted into special character combinations (such as &lt for <) for you. If, as is more often the case in ASP, you are pulling text out of a database or other external source, then you need to perform this conversion yourself (at run time). It is possible to write a VBScript routine to do this or to create an external object in VB or VC++ for this purpose, but you don't have to do so. Server.HTMLEncode will take any string as a parameter and output the converted value for use in your page.

Syntax :

 

               Server.HTMLEncode( string ( the string to encode))

 

The following  asp example  (HTMLENCODE.ASP )  produces the HTML output (HTMLRESULTS.HTM)

 

<HTML>

<BODY BGCOLOR="#FFFFFF">

<H1 align="center">

<%=Server.HTMLEncode("Welcome to Ahmed & Mohammed's Place")%>

</H1>

<H2 align="center">

<%=Server.HTMLEncode("Where it's <COOL> to use tags!")%>

</H2>

<P><%=Server.HTMLEncode("Ahmed specializes in " & _

"creating Web pages. Using only the <BODY>")%><BR>

<%=Server.HTMLEncode("and </BODY> tags he is able to " & _

"generate a complete site in minutes.")%></P>

<HR>

<P><%=Server.HTMLEncode("Mohammed prefers to create" & _

" graphics.  Wherever you have an <IMG> tag,")%><BR>

<%=Server.HTMLEncode("you could use Mohammed's " & _

"""" & "Special Touch" & """")%></P>

</BODY>

</HTML>

 

 HTMLRESULTS.HTM--Results of HTML Encode

 

<HTML>

<BODY BGCOLOR="#FFFFFF">

<H1 align="center">Welcome to Ahmed &amp; Mohammed's Place</H1>

<H2 align="center">Where it's &lt;COOL&gt; to use tags!</H2>

<P>Ahmed specializes in creating Web pages.

 Using only the &lt;BODY&gt;<BR>

and &lt;/BODY&gt; tags he is able to generate

a complete site in minutes.</P>

<HR>

<P> Mohammed prefers to create graphics.

Wherever you have an &lt;IMG&gt; tag,<BR>

you could use Mohammed's &quot;Special Touch&quot;</P>

</BODY>

</HTML>

 

The HTML output, now properly encoded, is returned to the browser and produces the desired output

 

6.4- URLEncode:
Similar to the encoding just described for text inside an HTML page, text attached to an URL (as a parameter, for instance) also cannot use certain characters. In an URL, characters such as a space or a backslash take on other meanings. URLEncode takes any string and, using special escape characters, converts it into a string suitable for use in an URL.

Synatx :

 

               Server.URLEncode( string (the string to encode))

 

For example, see the following line of scripting code:

 

<A HREF="collections.asp?text="

<%=Server.URLEncode("Why do we need to encode things?")%>

">Test</A>

 

results in the following line of text in the resulting page:

 

<A HREF=

"collections.asp?text=Why+do+we+need+to+encode+things%3F"

>Test</A>

 

6.5- MapPath:
The paths used to request a page are usually not the same as the physical path to that page on the Web server. The Server Variable

PATH_TRANSLATED provides the corresponding physical path for the current page. This Server method, MapPath, performs that translation on any virtual (such as a request) path, whether it is the current page or not. Many objects and IIS add-ons (such as Index Server) require physical paths to function correctly. This function enables you to provide those paths without having to hard code them into your page. If you look back at the example provided for Create Object, you will see this method in use to translate the current directory from a virtual path to a physical one.

 

If path starts with either a forward or backward slash, either (/) or (\), the MapPath method returns a path as if path is a full virtual path. If path doesn’t start with a slash, the MapPath method returns a path relative to the directory of the .asp file being processed.

 

Note :  The path parameter can contain relative paths (../../Scripts/, for example) if the EnableParentPath registry setting is set to TRUE. This is the default setting.

 

The MapPath method does not check whether the path it returns is valid or

exists on the server.Because the MapPath method maps a path regardless of whether the specified directories currently exist, you can use the MapPath method to map a path to a physical directory structure, and then pass that path to a component that creates the specified directory or file on the server.

 

Syntax :

 

               Server.MapPath( path )

 

For the examples below, the file Data.txt is located in the directory,

C:\Inetpub\Wwwroot\Script, as is the Test.asp file that contains the following

scripts. The C:\Inetpub\Wwwroot directory is set as the server's home directory.

The following example uses the server variable PATH_INFO to map the physical

path of the current file. The following:

 

<%= server.mappath(Request.ServerVariables("PATH_INFO"))%><BR>

produces the output:

 

c:\inetpub\wwwroot\script\test.asp<BR>

 

Because the path parameters in the following examples do not start with a slash

character, they are mapped relative to the current directory, in this case

C:\Inetpub\Wwwroot\Script. The following scripts:

 

<%= server.mappath("data.txt")%><BR>

<%= server.mappath("script/data.txt")%><BR>

 

produce the following output:

 

c:\inetpub\wwwroot\script\data.txt<BR>

c:\inetpub\wwwroot\script\script\data.txt<BR>

 

The next two examples use the slash characters to specify that the path

returned should be looked up as complete virtual paths on the server. The

following scripts:

 

<%= server.mappath("/script/data.txt")%><BR>

<%= server.mappath("\script")%><BR>

 

produce the following output:

 

c:\inetpub\script\data.txt<BR>

c:\inetpub\script<BR>

 

The following examples demonstrate how you can use either a forward slash (/)

or a backslash (\) to return the physical path to the home directory. The

following scripts:

 

<%= server.mappath("/")%><BR>

<%= server.mappath("\")%><BR>

 

produce the following output:

 

c:\inetpub\wwwroot<BR>

c:\inetpub\wwwroot<BR>