WebORB Presentation Server
AJAX Client Examples

Application Activation Example


Server responded:

Total hits:

Hits by browser type:

Instructions:
Click the buttons on the left to see numbers updated with every request. Open this page in a new browser. The numbers are still increasing which indicates the server continues to maintain its state for all connected clients.

Notes:
In this example, WebORB
Rich Client System makes a service invocation on an object with application activation. Since there is only one instance of the object, all the state and data contained in will be shared across the entire application. As a result, all connected clients (both AJAX and Flash) will get the same view onto the data.

Implementation:
When the page loads or user clicks one of the buttons on the right, the client creates a proxy to a server-side object with application activation and invokes the getVisitorInfo() method. The method returns a collection of statistics for all clients that had invoked that method. Method's implementation returns The collection as a hashtable. Rich Client System™ presents the hashtable to the JavaScript program as an anonymous JS object. The script then iterates over the fields in the object and dynamically populates table on the left.

Code review:

The client imports the JavaScript library of the Rich Client System™:

<script language="javascript" src="WebORB.js"></script>

The client uses WebORB™ object binding API to create a proxy to the server-side object. The method call takes 2 arguments: name of the class for which an object should be created, URL of the WebORB server:

proxy = webORB.bind(               "weborb.examples.activation.application.VisitorInfo", "http://localhost:8080/weborb?activate=application" );

Notice the "?activate=application" at the end of the server URL. The query parameter signals to WebORB that the instance of the VisitorInfo class must be obtained using the application activation mode. That means that only one instance of the class will be created in the application. Once it is created, any subsequent requests will be invoking methods on that specific instance. As a result, any state contained in the object will be shared for all clients of the application.

To issue an invocation, the client simply calls the method available in the VisitorInfo class. This example demonstrates both synchronous and asynchronous invocations. To invoke synchronously, just call the method. For the asynchronous invocation, make sure to pass an instance of the Async class, which contains the callback function. The callback function will be invoked when a response becomes available:

if( syncMode )
{
    responseObj = proxy.getVisitorsInfo();
    handleResponse( responseObj );
}
else
{
    proxy.getVisitorsInfo( new Async( handleResponse ) );
}

Server-side class:

Java:

// copyright 2003 - 2005 Midnight Coders, LLC
package weborb.examples.activation.application;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import weborb.util.ThreadContext;

public class VisitorInfo
{
  private int totalVisits = 0;
  private HashMap browserTypes = new HashMap();

  public HashMap getVisitorsInfo()
  {
    totalVisits++;

    HttpServletRequest request = ThreadContext.getHttpRequest();
    String userAgent = request.getHeader( "User-Agent" );

    if( !browserTypes.containsKey( userAgent ) )
      browserTypes.put( userAgent, new Integer( 0 ));

    Integer i = (Integer) browserTypes.get( userAgent );
    browserTypes.put( userAgent, new Integer( i.intValue() + 1 ) );

    HashMap result = new HashMap();
    result.put( "totalHits", new Integer( totalVisits ) );
    result.putAll( browserTypes );

    return result;
  }
}

.NET:

// copyright 2003 - 2005 Midnight Coders, LLC
using System;
using System.Web;
using System.Collections;
using Weborb.Util;

namespace weborb.examples.activation.application
{
  public class VisitorInfo
  {
    private int totalHits = 0;
    private Hashtable browserTypes = new Hashtable();

    public Hashtable getVisitorsInfo()
    {
      totalHits++;

      HttpRequest request = ThreadContext.currentRequest();

      if( !browserTypes.ContainsKey( request.UserAgent ) )
        browserTypes[ request.UserAgent ] = 0;

      int browserTypeHits = (int) browserTypes[ request.UserAgent ];
      browserTypes[ request.UserAgent ] = browserTypeHits + 1;

      Hashtable result = new Hashtable();
      result[ "totalHits" ] = totalHits;

      foreach( string browserType in browserTypes.Keys )
        result[ browserType ] = browserTypes[ browserType ];

      return result;
    }
  }
}

Copyright © Midnight Coders, LLC, 2005. All Rights Reserved