Cari di JavaScript 
    JavaScript Manual
Daftar Isi
(Sebelumnya) Chapter 11. Session Management ...Chapter 12. Utilities, File, S ... (Berikutnya)

client

Contains data specific to an individual client.

Server-side object

Implemented in

LiveWire 1.0

Created by

The JavaScript runtime engine on the server automatically creates a client object for each client/application pair.

Description

The JavaScript runtime engine on the server constructs a client object for every client/application pair. A browser client connected to one application has a different client object than the same browser client connected to a different application. The runtime engine constructs a new client object each time a user accesses an application; there can be hundreds or thousands of client objects active at the same time.

You cannot use the client object on your application's initial page. This page is run when the application is started on the server. At this time, there is not a client request, so there is no available client object.

The runtime engine constructs and destroys the client object for each client request. However, at the end of a request, the runtime engine saves the names and values of the client object's properties so that when the same user returns to the application with a subsequent request, the runtime engine can construct a new client object with the saved data. Thus, conceptually you can think of the client object as remaining for the duration of a client's session with the application. There are several different ways to maintain client property values; for more information, see Writing Server-Side JavaScript Applications.

All requests by one client use the same client object, as long as those requests occur within the lifetime of that client object. By default, a client object persists until the associated client has been inactive for 10 minutes. You can use the expiration method to change this default lifetime or the destroy method to explicitly destroy the client object.

Use the client object to maintain data that is specific to an individual client. Although many clients can access an application simultaneously, the individual client objects keep their data separate. Each client object can track the progress of an individual client across multiple requests to the same application.

Method Summary

destroy
Destroys a client object.

expiration
Specifies the duration of a client object.

Examples

Example 1. This example dynamically assigns a customer ID number that is used for the lifetime of an application session. The assignId function creates an ID based on the user's IP address, and the customerId property saves the ID.

<SERVER>client.customerId = assignId(request.ip)</SERVER>
See also the examples for the project object for a way to sequentially assign a customer ID.

Example 2. This example creates a customerId property to store a customer ID that a user enters into a form. The form is defined as follows:

<FORM NAME="getCustomerInfo" METHOD="post">
<P>Enter your customer ID:
   <INPUT TYPE="text" NAME="customerNumber">
</FORM>
The following code assigns the value entered in the customerNumber field from the temporary request.clientNumber to the more permanent client.customerId:

<SERVER>client.customerId=request.customerNumber</SERVER>

See also

project, request, server

Properties

The client object has no predefined properties. You create custom properties to contain any client-specific data that is required by an application. The runtime engine does not save client objects that have no property values.

You can create a property for the client object by assigning it a name and a value. For example, you can create a client property to store a customer ID at the beginning of an application so a user does not have to enter it with each request.

Because of the techniques used to maintain client properties across multiple client requests, there is one major restriction on client property values. The JavaScript runtime engine on the server converts the values of all of the client object's properties to strings.

The runtime engine cannot convert an object to a string. For this reason, you cannot assign an object as the value of a client property. If a client property value represents another data type, such as a number, you must convert the value from a string before using it. The core JavaScript parseInt and parseFloat functions are useful for converting to integer and floating point values.

Methods

destroy

Destroys a client object.

Method of

client

Implemented in

LiveWire 1.0

Syntax

destroy()

Description

The destroy method explicitly destroys the client object that issues it and removes all properties from the client object. If you do not explicitly issue a destroy method, the JavaScript runtime engine on the server automatically destroys the client object when its lifetime expires. The expiration method sets the lifetime of a client object; by default, the lifetime is 10 minutes.

If you are using client-cookies to maintain the client object, destroy eliminates all client property values, but it does not affect what is stored in Navigator cookie file. Use expiration with an argument of 0 seconds to remove all client properties stored in the cookie file.

When using client URL encoding to maintain the client object, destroy removes all client properties after the method call. However, any links in a page before the call to destroy retain properties in their URLs. Therefore, you should generally call destroy either at the top or bottom of the page when using client URL maintenance.

Examples

The following method destroys the client object that calls it:

<server>client.destroy()</server>

See also

client.expiration

expiration

Specifies the duration of a client object.

Method of

client

Implemented in

LiveWire 1.0

Syntax

expiration(seconds)

Parameters

seconds
An integer representing the number of seconds of client inactivity before the client object expires.

Description

By default, the JavaScript runtime engine on the server destroys the client object after the client has been inactive for 10 minutes. This default lifetime lets the runtime engine clean up client objects that are no longer necessary.

Use the expiration method to explicitly control the expiration of a client object, making it longer or shorter than the default. You must use expiration in each page of an application for which you want a client expiration other than the default. Any page that does not specify an expiration will use the default of 10 minutes.

Client expiration does not apply if using client URL encoding to maintain the client object. In this case, client properties are stored solely in URLs on HTML pages. The runtime engine cannot remove those properties.

Examples

The following example extends the amount of client inactivity before expiration to 1 hour. This code is issued when an application is first launched.

<SERVER>client.expiration(3600)</SERVER>

See also

client.destroy


project

Contains data for an entire application.

Server-side object

Implemented in

LiveWire 1.0

Created by

The JavaScript runtime engine on the server automatically creates a project object for each application running on the server.

Description

The JavaScript runtime engine on the server creates a project object when an application starts and destroys the project object when the application or server stops. The typical project object lifetime is days or weeks.

Each client accessing the same application shares the same project object. Use the project object to maintain global data for an entire application. Many clients can access an application simultaneously, and the project object lets these clients share information.

The runtime engine creates a set of project objects for each distinct Netscape HTTPD process running on the server. Because several server HTTPD processes may be running on different port numbers, the runtime engine creates a set of project objects for each process.

You can lock the project object to ensure that different clients do not change its properties simultaneously. When one client locks the project object, other clients must wait before they can lock it. See Lock for more information about locking the project object.

Method Summary

lock
Obtains the lock.

unlock
Releases the lock.

Examples

Example 1. This example creates the lastID property and assigns a value to it by incrementing an existing value.

project.lastID = 1 + parseInt(project.lastID, 10)
Example 2. This example increments the value of the lastID property and uses it to assign a value to the customerID property.

project.lock()
project.lastID = 1 + parseInt(project.lastID, 10);
client.customerID = project.lastID;
project.unlock();
In the previous example, notice that the project object is locked while the customerID property is assigned, so no other client can attempt to change the lastID property at the same time.

See also

client, request, server

Properties

The project object has no predefined properties. You create custom properties to contain project-specific data that is required by an application.

You can create a property for the project object by assigning it a name and a value. For example, you can create a project object property to keep track of the next available Customer ID. Any client that accesses the application without a Customer ID is sequentially assigned one, and the value of the ID is incremented for each initial access.

Methods

lock

Obtains the lock. If another thread has the lock, this method waits until it can get the lock.

Method of

project

Implemented in

LiveWire 1.0

Syntax

lock()

Parameters

None.

Returns

Nothing.

Description

You can obtain a lock for an object to ensure that different clients do not access a critical section of code simultaneously. When an application locks an object, other client requests must wait before they can lock the object.

Note that this mechanism requires voluntary compliance by asking for the lock in the first place.

See also

Lock, project.unlock

unlock

Releases the lock.

Method of

project

Implemented in

LiveWire 1.0

Syntax

unlock()

Parameters

None.

Returns

False if it fails; otherwise, true. Failure indicates an internal JavaScript error or that you attempted to unlock a lock that you don't own.

Description

If you unlock a lock that is unlocked, the resulting behavior is undefined.

See also

Lock, project.lock


server

Contains global data for the entire server.

Server-side object

Implemented in

LiveWire 1.0

Created by

The JavaScript runtime engine on the server automatically creates a single server object to store information common to all JavaScript applications running on the web server.

Description

The JavaScript runtime engine on the server creates a server object when the server starts and destroys it when the server stops. Every application on a server shares the same server object. Use the server object to maintain global data for the entire server. Many applications can run on a server simultaneously, and the server object lets them share information.

The runtime engine creates a server object for each distinct Netscape HTTPD process running on the server.

The properties listed below are read-only properties that are initialized automatically when a server object is created. These properties provide information about the server process. In addition to these predefined properties, you can create custom properties.

You can lock the server object to ensure that different applications do not change its properties simultaneously. When one application locks the server object, other applications must wait before they can lock it.

Property Summary

host
String specifying the server name, subdomain, and domain name.

hostname
String containing the full hostname of the server, including the server name, subdomain, domain, and port number.

port
String indicating the port number used for the server.

protocol
String indicating the communication protocol used by the server.

Method Summary

lock
Obtains the lock.

unlock
Releases the lock.

Examples

The following example displays the values of the predefined server object properties:

<P>server.host = <SERVER>write(server.host);</SERVER>
<BR>server.hostname = <SERVER>write(server.hostname);</SERVER>
<BR>server.protocol = <SERVER>write(server.protocol);</SERVER>
<BR>server.port = <SERVER>write(server.port);</SERVER>
The preceding code displays information such as the following:

server.host = www.myWorld.com
server.hostname = www.myWorld.com:85
server.protocol = http:
server.port = 85

See also

client, project, request

Properties

host

A string specifying the server name, subdomain, and domain name.

Property of

server

Read-only

Implemented in

LiveWire 1.0

Description

The host property specifies a portion of a URL. The host property is a substring of the hostname property. The hostname property is the concatenation of the host and port properties, separated by a colon. When the port property is 80 (the default), the host property is the same as the hostname property.

See Section 3.1 of RFC 1738 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html) for complete information about the hostname and port.

See also

server.hostname, server.port, server.protocol

hostname

A string containing the full hostname of the server, including the server name, subdomain, domain, and port number.

Property of

server

Read-only

Implemented in

LiveWire 1.0

Description

The hostname property specifies a portion of a URL. The hostname property is the concatenation of the host and port properties, separated by a colon. When the port property is 80 (the default), the host property is the same as the hostname property.

See Section 3.1 of RFC 1738 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html) for complete information about the hostname and port.

See also

server.host, server.port, server.protocol

port

A string indicating the port number used for the server.

Property of

server

Read-only

Implemented in

LiveWire 1.0

Description

The port property specifies a portion of the URL. The port property is a substring of the hostname property. The hostname property is the concatenation of the host and port properties, separated by a colon.

The default value of the port property is 80. When the port property is set to the default, the values of the host and hostname properties are the same.

See Section 3.1 of RFC 1738 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html) for complete information about the port.

See also

server.host, server.hostname, server.protocol

protocol

A string indicating the communication protocol used by the server.

Property of

server

Read-only

Implemented in

LiveWire 1.0

Description

The protocol property specifies the beginning of the URL, up to and including the first colon. The protocol indicates the access method of the URL. For example, a protocol of "http:" specifies HyperText Transfer Protocol.

The protocol property represents the scheme name of the URL. See Section 2.1 of RFC 1738 (http://www.cis.ohio-state.edu/htbin/rfc/rfc1738.html) for complete information about the protocol.

See also

server.host, server.hostname, server.port

Methods

lock

Obtains the lock. If another thread has the lock, this method waits until it can get the lock.

Method of

server

Implemented in

LiveWire 1.0

Syntax

lock()

Parameters

None

Returns

Nothing.

Description

You can obtain a lock for an object to ensure that different clients do not access a critical section of code simultaneously. When an application locks an object, other client requests must wait before they can lock the object.

Note that this mechanism requires voluntary compliance by asking for the lock in the first place.

See also

Lock, server.lock

unlock

Releases the lock.

Method of

server

Implemented in

LiveWire 1.0

Syntax

unlock()

Parameters

None.

Returns

False if it fails; otherwise, true. Failure indicates an internal JavaScript error or that you attempted to unlock a lock that you don't own.

Description

If you unlock a lock that is unlocked, the resulting behavior is undefined.

See also

Lock, server.unlock


Lock

Provides a way to lock a critical section of code.

Server-side object

Implemented in

Netscape Server 3.0

Created by

The Lock constructor:

Lock(); 

Parameters

None.

Failure to construct a new Lock object indicates an internal JavaScript error, such as out of memory.

Method Summary

lock
Obtains the lock.

isValid
Verifies that this Lock object was properly constructed.

unlock
Releases the lock.

See also

project.lock, project.unlock, server.lock, server.unlock

Methods

Syntax

lock

Obtains the lock. If someone else has the lock, this method blocks until it can get the lock, the specified timeout period has elapsed, or an error occurs.

Method of

Lock

Implemented in

Netscape Server 3.0

Syntax

lock(timeout)

Parameters

timeout
An integer indicating the number of seconds to wait for the lock. If 0, there is no timeout; that is, the method waits indefinitely to obtain the lock. The default value is 0, so if you do not specify a value, the method waits indefinitely.

Returns

True if it succeeds in obtaining the lock within the specified timeout. False if it did not obtain the lock.

Description

You can obtain a lock for an object to ensure that different clients do not access a critical section of code simultaneously. When an application locks an object, other client requests must wait before they can lock the object.

Note that this mechanism requires voluntary compliance by asking for the lock in the first place.

See also

Lock.unlock, Lock.isValid, project.lock, server.lock

isValid

Verifies that this Lock object was properly constructed.

Method of

Lock

Implemented in

Netscape Server 3.0

Syntax

isValid()

Parameters

None.

Returns

True, if this object was properly constructed; otherwise, false.

Description

It is very rare that your Lock object would not be properly constructed. This happens only if the runtime engine runs out of system resources while creating the object.

Examples

This code creates a Lock object and verifies that nothing went wrong creating it:

// construct a new Lock and save in project 
project.ordersLock = new Lock();
if (! project.ordersLock.isValid()) {
   // Unable to create a Lock. Redirect to error page
   ...
}

See also

Lock.lock, Lock.unlock

unlock

Releases the lock.

Method of

Lock

Implemented in

Netscape Server 3.0

Syntax

unlock()

Parameters

None.

Returns

False if it fails; otherwise, true. Failure indicates an internal JavaScript error or that you attempted to unlock a lock that you don't own.

Description

If you unlock a lock that is unlocked, the resulting behavior is undefined.

See also

Lock.lock, Lock.isValid, project.unlock, server.unlock

(Sebelumnya) Chapter 11. Session Management ...Chapter 12. Utilities, File, S ... (Berikutnya)