JSON logoJSON-RPC-Java

JavaScript to Java AJAX communications library.

Development of the JSON-RPC-Java project has now moved to jabsorb.org

JSON-RPC-Java FAQ

This document answers a few of the more frequently asked questions. If you can't find an answer here, in the manual, or in the Javadocs, then please consider looking through the mail archives or posting to the mailing list.

Table of Contents


Q1. Is JSON-RPC-Java an AJAX technology?

Yes, JSON-RPC-Java is an AJAX technology. It uses the more compact JSON format for messages instead of XML however it does use the XMLHttpRequest object as a transport. This still falls under the original definition of AJAX:

Here is an extract from the original article that coined the new term AJAX.

"Q. Why did you feel the need to give this a name?

A. I needed something shorter than 'Asynchronous JavaScript+CSS+DOM+XMLHttpRequest' to use when discussing this approach with clients."

JSON-RPC-Java is intended of course to be used in applications that use CSS and DOM.



Q2. When should I use synchronous vs asynchronous calls?

Alyways use asynchronous calls.

You should almost never use synchronous calls. Due to implementation problems with the XMLHttpRequest object, synchronous calls effectively hang the browser. If there are large network or application delays, this can appear similar to the browser crashing.



Q3. How do I access the HTTPServletRequest object from within a method call?

You can just add an HttpServletRequest argument to your Java method and it will get filled in by the server. It get's removed from the exported method signature. It is outlined in the manual.



Q4. Can JSON-RPC-Java work without using HttpSession?

Yes, you can do so by exporting your classes and objects in the global bridge. The global bridge singleton is accessed by JSONRPCBridge.getGlobalBridge(). Classes and objects are exported to all clients when registered with the global bridge.

Note:. In version 1.0 you need to also disable the automatic creation of a JSONRPCBridge in the session by placing the XML below into your web.xml inside the <servlet> element. If you do this, you can still add a JSONRPCBridge instance to the session yourself. If it is disabled, and you have not added one to the session, only the global bridge will be available.

<init-param>
    <param-name>auto-session-bridge</param-name>
    <param-value>0</param-value>
 </init-param>

The current CVS version now by default does not create a JSONRPCBridge instance in the session. You must do this yourself with a scope="session" usebean clause in JSP or associated Java code if using servlets.



Q5. Does JSON-RPC-Java support clustering?

Yes, if you register objects or classes in a JSONRPCBridge instance in the session, this information can be clustered if your container supports it. All data stored by JSON-RPC-Java in the session is Serializable.

Note: If you register your own classes as References, Callable References, or Callbacks, against a session bridge instance, then your classes need to implement Serializable aswell.



Q6. Does JSON-RPC-Java require the use of JSP?

No, it does not. It just happens that the demo code incljuded with the distribution happens to be JSP. You should be able to use JSON-RPC-Java with any web framework.



Q7. How do I create a session bridge from a servlet?

You just construct and place an JSONRPCBridge instance in the session under the key "JSONRPCBridge". See the tutorial example "Using the JSONRPCBridge in a Servlet" for example code.



Q8. In JavaScript code, how do I access variables related to a specific RPC call from within its async callback function?

To achieve this in the JavaScript way, you can take advantage of closures.

When a function is defined within a function in JavaScript, this creates a closure, where all variables in the scope of the outer function are available in the defined function when it is called later.

An example:

function selectUser(username)
{
    // some data we want available in scope of callback
    var row = lookupRow(username);

    var cb = function(result, ex) {
      if(ex) throw ex;

      // The row and username variables are visible
      // here later when this callback function is called
      updateRow(row, username, result);
    };

    jsonserver.userDb.fetchUser(cb, username);
}


Q9. How do I enable debugging?

JSON-RPC-Java uses java.util.logging to log debug messages (but only when a debug flag is set on the bridge instance). You need add bridge.setDebug(true); somewhere in your code.

Note: JSON-RPC-Java logs debug messages with the FINE level and this depending on your logging configuration will not be visible. One way to change this is to add the following to your logging.properties file:

com.metaparadigm.jsonrpc = FINE

You can usually find logging.properties in the JAVA_HOME/jre/lib/ directory.