Infolink

 

Search This Blog

Aug 15, 2012

Server Object

..Server.

The Server object provides access to the utility functions of the server.

Properties

ScriptTimeout

The ScriptTimeout property is the amount of runtime in seconds for a script before it terminates.

The ScriptTimeout property specifies the amount of time the web server will continue processing your script before it times out , in seconds for a script before it terminates. The default value is 90 seconds.

Examples

Code:

<% Server.ScriptTimeout = 150 %>

Explanation:

The following code causes a script to timeout if the script takes more than 150 seconds to complete. The value of the ScriptTimeout property can be retrieved as well

Methods

CreateObject

Syntax:

Server.CreateObject(ObjectID)

ObjectID

The ObjectID argument specifies the type of object to be created.

The CreateObject method creates an instance of an object to use in an ASP Page.

The CreateObject method is probably the most widely used and the most important method available through the Built-in ASP Objects. It allows you to instantiate the components of a script, or in different terms, create an instance of other objects. As a direct consequence, you can use and access any collections, events, methods, and properties associated with these objects.

There is one mandatory argument.

Examples

Code:

<%
Set myconn = Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
%>

Explanation:

The code creates a connection to an Access database

Execute

Version: 3.0

Syntax:

Server.Execute(Path)

Path

The Path argument is a string specifying either the absolute or relative path of the ASP page being called. The file name must be included in the path. The entire Path must be enclosed inside a pair of quotes. The Path argument cannot include a query string, however, any query string that was available to the calling ASP page will be available to the called ASP page.


The Execute method allows you to call another ASP page from inside an ASP page. When the called ASP page completes its tasks, you are then returned to the calling ASP page.

The Execute method allows you to call another ASP page from inside an ASP page. When the called ASP page completes its tasks, you are then returned to the calling ASP page. The overall effect is very similar to a function or subroutine call. Any text or output from the called ASP page will be displayed on the calling ASP page. The Execute method is a more useful alternative to using server-side includes.

In contrast, the Transfer method allows you to transfer from one ASP page to another without returning to the calling ASP page.

There is one mandatory argument.

Examples

Code:

----------CallingAsp.asp----------
<HTML>
<BODY>

How now <%Server.Execute("CalledAsp.asp")%> cow?

</BODY>
</HTML>

----------CalledAsp.asp----------
<%
Response.Write "pink"
%>

Output:

How now pink cow?

Transfer

Version: 3.0

Syntax:

Server.Transfer (Path)

Path

The Path argument is a string specifying either the absolute or relative path of the ASP page being called. The file name must be included in the path. The entire Path must be enclosed inside a pair of quotes.


The Transfer method allows you to transfer all of the state information for all of the built-in objects from one ASP page to another. Unlike the Execute method, when the ASP page that you have transferred to is finished, you do not return the original ASP page.

The Transfer method allows you to transfer from inside one ASP page to another ASP page. All of the state information that has been created for the first (calling) ASP page will be transferred to the second (called) ASP page. This transfered information includes all objects and variables that have been given a value in an Application or Session scope, and all items in the Request collections. For example, the second ASP page will have the same SessionID as the first ASP page.

When the second (called) ASP page completes its tasks, you do not return to the first (calling) ASP page.

In contrast, the Execute method allows you to call another ASP page, and when the called page has completed its tasks, you return to the calling ASP page.

There is one mandatory argument.

Examples

Code:
Code:

----------CallingAsp.asp----------
<HTML>
<BODY>

How now <%Server.Transfer("CalledAsp.asp")%> cow?

</BODY>
</HTML>

----------CalledAsp.asp----------
<%
Response.Write "pink"
%>

Output:

How now pink

Explanation:

In this example, the first ASP page (CallingAsp.asp) has no output.

MapPath

Syntax:

Server.MapPath(Path)

Path

The Path argument is the path to be mapped.

The MapPath method maps a relative or virtual path to a physical path.

The MapPath method maps a relative or virtual path to a physical path. This method does not check for the validity or the existence of the physical path. If the path starts with a forward or backward slash, the method returns the path as if the path is a full virtual path. If the path does not start with a slash, then the method returns the path relative to the directory of the ASP file being processed.

There is one mandatory argument.

Examples

Code:

<HTML>
<HEAD>
</HEAD>
<BODY>
The path of this file is <% Response.Write Server.MapPath("test.asp")
%>
The path of the file1 is <% Response.Write Server.MapPath("\test.asp")
%>
The path of the file2 is <% Response.Write Server.MapPath("test\test.asp")
%>
The path of the file3 is <% Response.Write Server.MapPath("\")
%>
</BODY>
</HTML>

Output:

The path of this file is D:\Inetpub\wwwroot\test.asp
The path of the file1 is D:\Inetpub\wwwroot\test.asp
The path of the file2 is D:\Inetpub\wwwroot\test\test.asp
The path of the file3 is D:\Inetpub\wwwroot

HTMLEncode

Syntax:

Server. HTMLEncode (String)

String

The String argument is the string to be encoded.

The HTMLEncode method applies HTML syntax to a specified string of ASCII characters.

HTMLEncode method of the server object allows you to encode the HTML String so that when it displayed I the browser,browser does not simply interpret the HTML as instruction for test layout

The HTMLEncode method applies HTML encoding syntax to a specified string of ASCII characters. For example, this allows you to display a HTML tag on a web page and not have it treated as an actual tag.

There is one mandatory argument.

Examples

Code:

<%

Str=”<TABLE><TR><TD>”

StrNatural=Server.HTMLEncode(str)

‘The variable strNatural holds the following code

‘&lt;TABLE&gt;&lt;TR&gt; ;&lt;TD&gt;

‘but it will displayed on client machine as <TABLE><TR><TD>

‘seen only if you view the source code on the client

Response.Write strNatural

%>

<% Response.Write Server.HTMLEncode("The tag for a table is: <Table>") %>

Output:

The tag for a table is: &lt;Table&gt;

Explanation:

In a browser the output would look like this: The tag for a table is: <Table>

URLEncode

Syntax:

Server.URLEncode(String)

String

The String argument is the string to be encoded.

The URLEncode method applies URL rules to a specified string of ASCII characters.

The URLEncode method takes a string and converts it into a URL-encoded format. For example, you can use URLEncode to ensure that hyperlinks in your ASP are in the correct format.

There is one mandatory argument.

Examples

Code:

<% Response.Write Server.URLEncode("http://www.issi.net") %>

Output:

http%3A%2F%2Fwww%2Eissi%2Enet

Session

The first time a user makes a document request, a Session object is assigned to the user and it is used to store, share, and retrieve information concerning the user session. By default, the Session object is destroyed after a session has been idle for twenty minutes.

Properties

SessionID

The SessionID property returns the unique identifier for a user session.

The SessionID property returns the unique session identifier for the current user. The identifier is generated by the server and is a Long data type.

Examples

Code:

<% Response.Write "Your Session ID is " & Session.SessionID %>

Output:

Your Session ID is 465107831

Timeout

The Timeout property is the length of time in minutes before an idle session is terminated.

The Timeout property specifies the number of minutes that can elapse while a session is idle before it is terminated. The default is 20 minutes. If the user does not refresh the page or request another page within the allotted time, the session is automatically terminated and the resources are released.

The following code causes the session to terminate if the session is idle for more than 35 minutes.

Examples

Code:

<% Session.Timeout = 35 %>

Methods

Abandon

Syntax:

Session.Abandon

The Abandon method terminates a user session, destroys all data and objects in the current Session object, and releases its resources.

The Abandon method terminates a user session, destroys all the objects in the current Session object, and releases its resources. However, this deletion will not occur until all of the script is processed for the current page. When the session ends, the OnEnd event handler is called. By default, even if you do not call Abandon, the Session object is terminated after twenty minutes of idle time.

Examples

Code:

----------------File1.asp-------------------
<%
Response.Write "Your SessionID is " & Session.SessionID
Session("Application") = "Site"
Session.Abandon
Response.Write "The Application name is " & Session("Application")
%>

----------------File2.asp---------------------
<%
Response.Write "Your SessionID is " & Session.SessionID
%>

Output:

------------File1.asp-------------------------
Your SessionID is 465107831
The Application name is Site

-----------File2.asp--------------------------
Your SessionID is 465107831

Contents.Remove

Version: 3.0

Syntax:

Session.Contents.Remove (Name|Integer)

Name

The Name argument is the name of the item to be deleted. It must be enclosed in a pair of quotes.

Integer

The Integer argument is the position number of the item in the collection to be deleted. The numbering sequence for a collection starts at one, not zero.

The Contents.Remove method is used to remove a single item from a Contents collection.

The Contents.Remove method is used to delete one specified item in the Session.Contents collection.

You may use only one of the two possible choices for the mandatory argument.

Examples

Code:

<%
Session("name") = "Session Maker"
Session("publishdate") = "05/15/01"
Session("author") = "Dept"

Session.Contents.Remove(1)
Session.Contents.Remove("publishdate")

Output:

author=Dept

Contents.RemoveAll

Version: 3.0

Syntax:

Session.Contents.RemoveAll

The Contents.RemoveAll method is used to remove all items from a Contents collection.

The Contents.RemoveAll method deletes all items that are in the Session.Contents collection.

Remember to include the pair of empty ( ).

Examples

Code:

<%
Session.Contents.RemoveAll( )
%>

Collections

Contents

Syntax:

Session.Contents(Key)

Key

The key argument is the name of the item to retrieve.

The Contents collection property contains all of the items that have been created and added to the Session object through script commands, rather than usjing the HTML <OBJECT> tag.

The Contents collection property contains a list of all of the items that has been created and added to the Session object through script commands, rather than by using the HTML <OBJECT> tag.

You can iterate through a collection using a For Each item in ... Next loop.

There is one mandatory argument.

Examples

Code:

<%
Session("name") = "Application Maker"
Session("publishdate") = "05/01/99"
Session("author") = "ISSI"


For Each Item in Session.Contents
Response.Write Item & "=" & Session.Contents(Item) & "<BR>"
Next
%>

Output:

NAME=Application Maker
PUBLISHDATE=05/01/99
AUTHOR=ISSI

StaticObjects

Syntax:

Session.StaticObjects(Key)

Key

The Key argument is the name of the item to retrieve.

The StaticObjects collection property contains all of the items created in the Global.asa file using the HTML <OBJECT> tag within the scope of the Session object.

The StaticObjects collection property contains a list of all of the items that have been created and added to the Session object using the HTML <OBJECT> tag.

You can iterate through a collection using a For Each item in ... Next loop.

There is one mandatory argument.

Examples

Code:

----------Global.asa-------------
<OBJECT RUNAT=Server SCOPE=Session ID=MyConnection PROGID="ADODB.Connection">
</OBJECT>

<OBJECT RUNAT=Server SCOPE=Session ID=MyADRot PROGID="MSWC.ADRotator">
</OBJECT>

-----------File.asp-----------------
<%
For Each Item In Session.StaticObjects
Response.Write Item & "<BR>"
Next
%>

Output:

MyInfo
MyConnection
MyADRot

Events

OnEnd

The OnEnd event occurs when the Session quits. The signal of this event will run a handler script in the Global.asa file, if the script exists.

The Session_OnEnd event occurs when the Session ends. Normally, a Session is ended by being timed-out, either by default or by using Session.TimeOut, or by being abandoned by using Session.Abandon. The Session_OnEnd event is simply a subroutine with a reserved name that is placed in the Global.asa file. The code in the event cannot affect the user because he has quit the site.

In the example, there is no output because the user is already gone. The first line must specify the script language.

Note, only the Application, Server, and Session built-in objects are available from within the OnEnd event handler.

Examples

Code:

-------------------Global.asa--------------------------
<script Language="VBScript" RUNAT=Server>
Sub Application_OnEnd()
End Sub

Sub Application_OnStart()
Application("NumSession") = 0
Application("NumVisited") = 0
End Sub

Sub Session_OnEnd()
Application("NumSession") = Application("NumSession") - 1
End Sub

Sub Session_OnStart()
Application("NumSession") = Application("NumSession") + 1
Application("NumVisited") = Application("NumVisited") + 1
End Sub
</script>

-------------------File1.asp----------------------------
Response.Write "You are " & Application("NumSession") & " of " & Application("NumVisited") & " users."

OnStart

The OnStart event occurs before the start of any new Session by a user. The signal of this event will run a handler script in the Global.asa file, if the script exists.

The Session_OnStart event occurs just before the server creates the Session object. The Session_OnStart event is simply a subroutine with a reserved name that is placed in the Global.asa file. You can place any code you desire inside this subroutine. All of the built-in ASP objects are available from within the Session_OnStart event handler.

Examples

Code:

-------------------Global.asa--------------------------
<script Language="VBScript" RUNAT=Server>
Sub Application_OnEnd()
End Sub

Sub Application_OnStart()
Application("NumSession") = 0
Application("NumVisited") = 0
End Sub

Sub Session_OnEnd()
Application("NumSession") = Application("NumSession") - 1
End Sub

Sub Session_OnStart()
Application("NumSession") = Application("NumSession") + 1
Application("NumVisited") = Application("NumVisited") + 1
End Sub
</script>

-------------------File1.asp----------------------------
Response.Write "You are " & Application("NumSession") & " of " & Application("NumVisited") & " users."

Output:

You are 1 of 3 users.

Application

Version: Modified in version 3.0

The Application object shares, stores, and retrieves information in response to requests from users to the application.

Methods

Contents.Remove

Version: 3.0

Syntax:

Application.Contents.Remove (Name|Integer)

Name

The Name argument is the name of the item to be deleted. It must be enclosed in a pair of quotes.

Integer

The Integer argument is the position number of the item in the collection to be deleted. The numbering sequence for a collection starts at one, not zero.

The Contents.Remove method is used to remove a single item from a Contents collection.

The Contents.Remove method is used to delete one specified item in the Application.Contents collection.

You may use only one of the two possible choices for the mandatory argument.

Examples

Code:

<%
Application("name") = "Application Maker"
Application("publishdate") = "05/15/01"
Application("author") = "DevGuru"
Set Application("Obj1") = Server.CreateObject("ADODB.Connection")

Application.Contents.Remove(1)
Application.Contents.Remove("publishdate")

Output:

author=DevGuru
Obj1 is an object.

Lock

Syntax:

Application.Lock

The Lock method prevents all other users from making changes in the Application object.

The Lock method prevents all other users from changing any of the variables in the Contents collection of the Application object.

Application objects are designed to be shared among an unlimited number of users. Therefore, you need the ability to allow only one user at a time to make changes and you do this by locking everybody else out.

You can use the Unlock method to explicitly remove the Lock placed upon the Application object. Remember, to completely unlock an object, you must call Unlock the same number of times you have called Lock. Fortunately, the server will automatically unlock all locks placed on the object when the script times out or the .asp file ends.

Examples

Code:

<%
Application.Lock
%>

Unlock

Syntax:

Application.Unlock

The Unlock method allows any user to have access to any of the Application object properties in order to make changes.

The Unlock method is used to explicitly unlock the variables in the Contents collection of the Application object.

In contrast, the Lock method prevents all other users from changing any of the variables in the Contents collection of the Application object. This ability to lock is required since Application objects are designed to be shared among an unlimited number of users. Therefore, you need the ability to allow only one user at a time to make changes and you do this by locking everybody else out.

Remember, to completely unlock an object, you must call Unlock the same number of times you have called Lock. Fortunately, the server will automatically unlock all locks placed on the object when the script times out or the .asp file ends.

Examples

Code:

<%
Application.Unlock
%>

Events

OnEnd

The OnEnd event occurs when the Application quits. This should not occur unless all user sessions are over. The signal of this event will run a handler script in the Global.asa file, if the script exist.

The Application_OnEnd event occurs when the Application ends. This should only happen when the web server http://images.intellitxt.com/ast/adTypes/mag-glass_10x10.gif is stopped by the operating system in a normal manner. The Application_OnEnd event is simply a subroutine with a reserved name that is placed within the Global.asa file. It can contain any script that you wish to run after all user sessions are finished. For example, you may wish to compute and store statistics on user sessions for future reference.

Note: The only built-in ASP objects available from within the OnEnd event handler are Server and Application.

Examples

Code:

-------------------Global.asa--------------------------
<script Language="VBScript" RUNAT=Server>
Sub Application_OnEnd()
Calculate_Stats()
End Sub

Sub Application_OnStart()
Application("NumSession") = 0
Application("NumVisited") = 0
End Sub

Sub Session_OnEnd()
Application("NumSession") = Application("NumSession") - 1
End Sub

Sub Session_OnStart()
Application("NumSession") = Application("NumSession") + 1
Application("NumVisited") = Application("NumVisited") + 1
End Sub
</script>

-------------------File1.asp----------------------------
Response.Write "You are " & Application("NumSession") & " of " & Application("NumVisited") & " users."

OnStart

The OnStart event occurs before the start of any new session by a user (i.e., before the Application object is first referenced). The signal of this event will run a handler script in the Global.asa file, if the script exist.

The Application_OnStart event occurs before the beginning of any user session. The only built-in ASP objects available from within the OnStart event handler are Server and Application. The Application_OnStart event is simply a subroutine with a reserved name that is placed within the Global.asa file. It contains any script that you wish to run before the first user session. For example, you could use the Session.Timeout property to automatically end an idle user session after 10 minutes.

Note that you must declare the scripting language used to code the event script on the first line of the Global.asa file.

Examples

Code:

-------------------Global.asa--------------------------
<script Language="VBScript" RUNAT=Server>
Sub Application_OnEnd()
End Sub

Sub Application_OnStart()
Application("NumSession") = 0
Application("NumVisited") = 0
Session.Timeout = 10
End Sub

Sub Session_OnEnd()
Application("NumSession") = Application("NumSession") - 1
End Sub

Sub Session_OnStart()
Application("NumSession") = Application("NumSession") + 1
Application("NumVisited") = Application("NumVisited") + 1
End Sub
</script>

-------------------File1.asp----------------------------
Response.Write "You are " & Application("NumSession") & " of " & Application("NumVisited") & " users."

Output:

You are 1 of 1 users.


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...