Sideway BICK BlogSideway BICK BLOG from Sideway

A Sideway to Sideway Home

Link:http://output.to/sideway/default.asp?qno=120200035

Lock / Unlock

Application Object

One of the key functions of Application Object is the application control on the server.

Application.Lock Method

Application.Lock method locks the properties of Application object by only one client at a time for preventing other clients from modification..

Syntax:

Lock(
)

 Or in an ASP file. Imply

<% Application.Lock %>

Parameters:

void

This method has no parameters

Return Values:

void

This method has no parameters

Remarks:

After calling the Application.Lock method, if the Application.Unlock method is not called explicitly, the server unlocks the locked Application object when the ASP file processing ends or times out. Therefore a lock on the Application object only persists for a very short time.

However, if one page locks the application object and a second page tries to locks the same application object while the first page still has it locked, the second page will wait for the first to finish, or until the Server.ScriptTimeout limit is reached.

If the Server.ScriptTimeout limit is reached, the following ASP error is returned which cannot be captured:

HTTP 500.100 - Internal Server Error - ASP error
Internet Information Services

Active Server Pages, ASP 0113 (0x80004005)
The maximum amount of time for a script to execute was exceeded.
You can change this limit by specifying a new value for the property
Server.ScriptTimeout or by changing the value in the IIS administration tools

To reduce the possibility of having an ASP page time out, write a COM+ object that uses the COM+ ObjectContext object to update the Application collection, and then call the COM+ object from an ASP. COM+ objects execute slightly faster than ASP pages.

The application collection can be modified without locking the application object to edit. Therefore two pages can modify the same application without causing any error, however, the modification will become inconsistent.

Examples:

  • Use Application.Lock to prevent more than on client accessing and altering the application variable simultaneously

    ASP script command:

    <%
    Application.Lock
    Application("CountNo") = Application("CountNo") + 1
    Application.Unlock
    %>

Application.Unlock Method

Application.UnlLock method unlocks or releases the properties of Application object by the only one client at a time for allowing other clients to modify.

Syntax:

Unlock(
)

 Or in an ASP file. Imply

<% Application.Unlock %>

Parameters:

void

This method has no parameters

Parameters:

void

This method has no parameters

Remarks:

The Application.Lock method is cumulative, therefore for the same script, the number of Lock method calls should be equal to the number of Unlock method calls in order to fully release the application, otherwise the application remains Locked.

After calling the Application.Lock method , if the Application.Unlock method is not called explicitly, the server unlocks the locked Application object when the ASP file processing ends or times out. Therefore a lock on the Application object only persists for a very short time.

However, if one page locks the application object and a second page tries to locks the same application object while the first page still has it locked, the second page will wait for the first to finish, or until the Server.ScriptTimeout limit is reached.

If the Server.ScriptTimeout limit is reached, the following ASP error is returned which cannot be captured:

HTTP 500.100 - Internal Server Error - ASP error
Internet Information Services

Active Server Pages, ASP 0113 (0x80004005)
The maximum amount of time for a script to execute was exceeded.
You can change this limit by specifying a new value for the property
Server.ScriptTimeout or by changing the value in the IIS administration tools

To reduce the possibility of having an ASP page time out, write a COM+ object that uses the COM+ ObjectContext object to update the Application collection, and then call the COM+ object from an ASP. COM+ objects execute slightly faster than ASP pages.

 The application collection can be modified without locking the application object to edit. Therefore two pages can modify the same application without causing any error, however, the modification will become inconsistent.

Examples:

  • Use same number of Application.Unlock to release the application for other clients accessing and altering the application variable simultaneously

    ASP script command:

    <% 
    Application.Lock
    Application.Lock
    Application("CountNo") = Application("CountNo") + 1
    Application.Unlock
    Application.Unlock
    %>

Link:http://output.to/sideway/default.asp?qno=120200030

HTMLEncode / URLEncode

Server Object

Server object also provides some utility functions on the server.

Server.HTMLEncode Method

Server.HTMLEncode Method encodes and converts the specified string according to the HTML encoding rules.

Syntax:

HTMLEncode(
    string
)

 Or in an ASP file. Imply

<% Server.HTMLEncode(string) %>

Parameters:

string

The paramete "string" specifies the string to encode.

Return Values:

The HTMLEncoded string.

Remarks:

The Server.HTMLEncode Method can be used to encode data for converting potentially unsafe characters to their HTML-encoded equivalent.

If the string to be encode is not double-byte character set (DBCS), HTMLEncode converts characters as following:

  • Less-than character (<) to "&lt;"

  • Greater-than character (>) to "&gt;"

  • Ampersand character (&) to "&amp;"

  • Double-quote character (") to "&quot;"

  • ASCII code character <number> greater-than or equal to 0x80 to &#<number>, where <number> is the ASCII character value.

If the string to be encode is double-byte character set (DBCS), HTMLEncode converts characters as following:

  • All extended character are converted

  • ASCII code character <number> greater-than or equal to 0x80 to &#<number>, where <number> is the ASCII character value.

  • Half-width Katakana characters in the Japanese code page are not converted.

The encoded code will be displayed by a Web browser. And the encoded HTML page can be displayed when opened the page file as a text file.

Examples:

  • To display a "HTML tag" on a page by encode the HTML tag using Server.HTMLEncode

    ASP script command:

    <%
    Response.Write Server.HTMLEncode("<p>Encoded tag.</p>")
    Response.Write ("<p>Normal tag.</p>")
    %> %>

    HTTP response output:

    &lt;p&gt;This is Output&lt;/p&gt;
    <p>Normal tag.</p>

    HTML web page ouput

    <p>This is Output</p>
    Normal tag.

Server.URLEncode Method

Server.URLEncode Method encodes and converts the specified string according to the URL encoding rules including escape characters .

Syntax:

URLEncode(
    string
)

 Or in an ASP file. Imply

<% Server.URLEncode(string) %>

Parameters:

string

The paramete "string" specifies the string to encode.

Return Values:

The URLEncoded string.

Remarks:

The Server.URLEncode Method can be used to encode data for converting characters as following:

  • Space character ( ) to plus sign (+)

  • Non-alphanumeric character are escaped to their hexadecimal representation.

Examples:

  • To display a encoded HTTP request on a page by using Server.URLEncode

    ASP script command:

    <%
    Response.Write Server.URLEncode("http://www.adomainname.com")
    %>

    HTTP response output:

    http%3A%2F%2Fwww%2Eadomainname%2Ecom

    HTML web page ouput

    http%3A%2F%2Fwww%2Eadomainname%2Ecom

Link:http://output.to/sideway/default.asp?qno=120200031

MapPath / GetLastError

Server Object

Server object also provides some utility functions on the server.

Server.MapPath Method

Server.MapPath Method maps and converts the specified path including virtual path on the server, or relative path relative to the page into the corresponding physical path..

Syntax:

MapPath(
    Path
)

 Or in an ASP file. Imply

<% Server.MapPath(path) %>

Parameters:

Path

The paramete "Path" specifies the relative or virtual path to map to a physical directory. For Path start with either a forword (/) or backward slash (\), the Mappath method returns a path as if Path were a full, wirtual path. For Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the ASP file being processed.

Return Values:

The mapped path string.

Remarks:

For Path start with either a forword (/) or backward slash (\), the Mappath method returns a path as if Path were a full, virtual path. For Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the ASP file being processed.

The Server.MapPath Method returns the ASP 0173 error, "Invalid Path Character", if the parameter Path contains any of the following characters:

  • Asterisk (*)

  • Question mark (?)

  • Angle brackets (< or >)

  • Comma (,}

  • Colon or semi-colon (: or :)

  • Single-quote or double-quote (' or ")

  • Right square bracket (])

  • Double slashes (// or \\)

The Server.MapPath method does not check whether the path it retruns is valid or exists on the server. Because the Server.MapPath method maps a path regardless of whether the specified directories currently exist,  the Server.MapPath method to map a path to a physical directory structure, and then pass that to a component that creates the specified directory or file on the server.

Server.MapPath is not available to the Session_OnEnd and the Application_OnEnd events.

For security reason, the AspEnableParentPaths property has a default value set to FALSE. Scripts will not have access to the physical directory structure unless AspEnableParentPaths is set to True

Examples:

  • To map the PATH_INFO to the physical path by Server.MapPath

    ASP script command:

    <% 'run at c:\webroot\sideway\default.asp
    Response.Write _
    Server.MapPath(Request.ServerVariables("PATH_INFO")) & "<br />"
    Response.Write (Request.ServerVariables("PATH_INFO"))
    %>

    HTTP response output:

    C:\webroot\sideway\default.asp<br />
    /sideway/default.asp

    HTML web page ouput

    C:\webroot\sideway\default.asp
    /sideway/default.asp

  • To map the PATH_INFO to the physical path by Server.MapPath

    ASP script command:

    <% 'run at c:\webroot\sideway\default.asp
    Response.Write Server.MapPath("./../sideway/default.asp") & "<br />"
    Response.Write Server.MapPath("../../sideway/default.asp") & "<br />"
    Response.Write Server.MapPath("/../sideway/default.asp") & "<br />"
    Response.Write Server.MapPath("./sideway/default.asp") & "<br />"
    Response.Write Server.MapPath("../sideway/default.asp") & "<br />"
    Response.Write Server.MapPath("/sideway/default.asp") & "<br />"
    Response.Write Server.MapPath("sideway/default.asp") & "<br />"
    Response.Write Server.MapPath("./default.asp") & "<br />"
    Response.Write Server.MapPath("../default.asp") & "<br />"
    Response.Write Server.MapPath("/default.asp") & "<br />"
    Response.Write Server.MapPath("default.asp") & "<br />"
    Response.Write Server.MapPath("./") & "<br />"
    Response.Write Server.MapPath("../") & "<br />"
    Response.Write Server.MapPath("/") & "<br />"
    Response.Write Server.MapPath(".") & "<br />"
    Response.Write Server.MapPath("..") & "<br />"
    %>

    HTTP response output:

    C:\webroot\sideway\default.asp<br />
    C:\sideway\default.asp<br />
    C:\webroot\sideway\default.asp<br />
    C:\webroot\sideway\sideway\default.asp<br />
    C:\webroot\sideway\default.asp<br />
    C:\webroot\sideway\default.asp<br />
    C:\webroot\sideway\sideway\default.asp<br />
    C:\webroot\sideway\default.asp<br />
    C:\webroot\default.asp<br />
    C:\webroot\default.asp<br />
    C:\webroot\sideway\default.asp<br />
    C:\webroot\sideway<br />
    C:\webroot<br />
    C:\webroot<br />
    C:\webroot\sideway<br />
    C:\webroot<br />

    HTML web page ouput

    C:\webroot\sideway\default.asp
    C:\sideway\default.asp
    C:\webroot\sideway\default.asp
    C:\webroot\sideway\sideway\default.asp
    C:\webroot\sideway\default.asp
    C:\webroot\sideway\default.asp
    C:\webroot\sideway\sideway\default.asp
    C:\webroot\sideway\default.asp
    C:\webroot\default.asp
    C:\webroot\default.asp
    C:\webroot\sideway\default.asp
    C:\webroot\sideway
    C:\webroot
    C:\webroot
    C:\webroot\sideway
    C:\webroot

Server.GetLastError Method

Server.GetLastError Method gets the last error from the ASPError that describes the error condition of processing the script in an ASP file.

Syntax:

GetLastError(
)

 Or in an ASP file. Imply

<% Server.GetLastError() %>

Parameters:

void

This method has no parameters..

Return Values:

Server.GetLastError Method returns the last instance of ASPError.

Remarks:

This method is available only before the .asp file has sent any content to the client. 

A 500;100 custom error will be generated if IIS encounters an error while processing either an .asp file or the application's Global.asa file.

If a 500:100 custom error has been defined for an ASP application, it may refer to an ASP file. In this case, when an error occurs during the running of an ASP file within the application, the server automatically transfers to this ASP page via the Server.Transfer method. All of the state information from the executing ASP application will be available to the ASP file that is handlingthe error. In addition, the ASPError Object will be available, so that the properties of the error can be exposed through the ASP file that can be set up for handling the error.

The Default Web sit is configured to use the file \iishelp\common\500-100.asp. Either this file or a self created file can be used for processing ASP errors. The file for processing the 500:100 custom errors can be changed through the IIS Manager.

Examples:

  • To obtain the last instance of the ASPError by Server.GetLastError and display the ASPError properties

    ASP script command:

    <%
    Set ErrInfo = Server.GetLastError
    Response.Write("ASPCodeof ASPError = " & ErrInfo.ASPCode)
    Response.Write("ASPDescriptionof ASPError = " & ErrInfo.ASPDescription)
    Response.Write("Categoryof ASPError = " & ErrInfo.Category)
    Response.Write("Columnof ASPError = " & ErrInfo.Column)
    Response.Write "Descriptionof ASPError = " & _
    ErrInfo.Description)
    Response.Write("Fileof ASPError = " & ErrInfo.File)
    Response.Write("Lineof ASPError = " & ErrInfo.Line)
    Response.Write("Numberof ASPError = " & ErrInfo.Number)
    Response.Write("Sourceof ASPError = " & ErrInfo.Source)
    %>

  • The three common types of error that generate a 500:100 custom error are:

    ASP script command:

    <%
    ' Preprocessing errors of the include statement
    <!--#include filo=inc.h-->
    for i=0 to 10
    a=100/i
    next
    %>

    ASP script command:

    <%
    'Script compiling errors of the For ... Next loop
    <!--#include file=inc.h-->
    for i=0 to 10
    a=100/i
    noxt
    %>

    ASP script command:

    <%
    ' Run-time errors of the a=100/i statement attempt to divide by 0
    <!--#include fole=inc.h-->
    for i=10 to 0 step -1
    a=100/i
    next
    %>

Previous Month  FEB  2012  Next Month
SMTWTFS
1234
567891011
12131415161718
19202122232425
26272829

Previous Month  JUN  2014  Next Month
SMTWTFS
1234567
891011121314
15161718192021
22232425262728
2930

Sideway BICK Blog

10/02


Copyright © 2000-2020 Sideway . All rights reserved Disclaimerslast modified on 26 January 2013