Monday, July 11, 2011

RPC in Javascript using JSON-RPC-Java


 RPC in Javascript using JSON-RPC-Java


Remote procedure call (RPC) in javascript is a great concept of creating rich web applications. First we will see some background about RPC using JavaScript Object Notation (JSON). See following quote from Wikipedia entry of JSON-RPC.
JSON-RPC is a remote procedure call protocol encoded in JSON. It is a very simple protocol (and very similar to XML-RPC), defining only a handful of data types and commands. In contrast to XML-RPC or SOAP, it allows for bidirectional communication between the service and the client, treating each more like peers and allowing peers to call one another or send notifications to one another. It also allows multiple calls to be sent to a peer which may be answered out of order.
A JSON invocation can be carried on an HTTP request where the content-type is application/json. Besides using HTTP for transport, one may use TCP/IP sockets. Using sockets, one can create much more responsive web applications with JSON-RPC, compared to polling data from a service with JSON-RPC over HTTP.
JSON-RPC-Java is a key piece of Java web application middleware that allows JavaScript DHTML web applications to call remote methods in a Java Application Server without the need for page reloading (now referred to as AJAX). It enables a new breed of fast and highly dynamic enterprise Web 2.0 applications.
For using JSON-RPC-Java in your code, you need to download following json-rpc-java-1.0.1.zip file and unzip it.
http://oss.metaparadigm.com/jsonrpc/download.html
The zip contains required jsonrpc-1.0.jar and jsonrpc.js files that we will use in our project.
Once the JAR file is in classpath of your project, modify your WEB.XML (deployment descriptor) file and make an entry for JSONRPCServlet as follow.
?
1
2
3
4
5
6
7
8
<servlet>
<servlet-name>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-name>
<servlet-class>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>com.metaparadigm.jsonrpc.JSONRPCServlet</servlet-name>
<url-pattern>/JSON-RPC</url-pattern>
</servlet-mapping>
Note that JSONRPCServlet is a servlet class inside the jsonrpc-1.0.jar file. Also, we have mapped a URL/JSON-RPC with the servlet. Hence the servlet will be invoked by the container whenever this URL is called by the client.
Also, do not forget in include the javascript file in your application.
?
1
<script src="js/jsonrpc.js" type="text/javascript"></script>
We will create a small webpage which will have two textboxes to enter numbers and a button which will call the RPC (call .sum() method on an object which resides in server) and send these two numbers to server. Server will do sum of these numbers and return the result back and the result will be displayed on webpage.
Following is the content of the html page.
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<html>
<head>
<title>JSON-RPC-Java Demo</title>
<script type="text/javascript" src="js/jsonrpc.js"></script>
<script type="text/javascript">
function fnSum(form) {
try {
//Create a jsonrpc object for doing RPC.
jsonrpc = new JSONRpcClient("JSON-RPC");
 
// Call a Java method on the server
result = jsonrpc.sumObject.sum(form.a.value, form.b.value);
//Display the result
alert(result);
 
} catch(e) {
alert(e.description);
}
}
</script>
</head>
<body>
<h1>JSON-RPC-JAVA</h1>
<form>
<input type="text" name="a"/>
<input type="text" name="b"/>
<input type="button" onclick="fnSum(this.form)" value="Sum"/>
</form>
</body>
</html>
Note that in order to use JSON-RPC-Java, you need to set an object ofcom.metaparadigm.jsonrpc.JSONRPCBridge class in session. All the object that we will use in RPC from Client side needs to be registered into JSONRPCBridge’s object. JSONRPCBridge object can be written in session using jsp:useBean tag.
?
1
2
<jsp:useBean id="JSONRPCBridge" scope="session"
class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
We will use a business class called Sum which will do sum operation on the input and return the output. Following is the content of Sum class.
?
1
2
3
4
5
6
package net.viralpatel.jsonrpc;
public class Sum {
public Integer sum(Integer a, Integer b) {
return a + b;
}
}
Thus we need to register an object of class Sum in JSONRPCBridge object in order to use it from javascript.
?
1
2
Sum sumObject = new Sum();
JSONRPCBridge.registerObject("sumObject", sumObject);
Doing RPC from javascript is easy now. All we need is an jsonrpc object that we get by doing jsonrpc = new JSONRpcClient(“JSON-RPC”);. Note that the argument passed inside the JSONRpcClient() is the URL that we mapped to JSONRPCServlet servlet.
Once we get an object of jsonrpc, we can call remote methods by:
?
1
result = jsonrpc.sumObject.sum(form.a.value, form.b.value);
Where sumObject is the name that we provided while registering Sum class to JSONRPCBridge.
Following is the screenshot of the html page for user input.
JSON-RPC-Java Screen
Following is the sum output that the server returned to webpage using json-rpc.
JSON-RPC-Java Output screen

No comments:

Post a Comment