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.
For using JSON-RPC-Java in your code, you need to download following json-rpc-java-1.0.1.zip file and unzip it.
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.
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.
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.