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-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.
For using JSON-RPC-Java in your code, you need to download following json-rpc-java-1.0.1.
http://oss.metaparadigm.com/
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.
Also, do not forget in include the javascript file in your application.
We will create a small webpage which will have two textboxes to enter numbers and a button which will call the RPC (call
Following is the content of the html page.
.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.
Note that in order to use JSON-RPC-Java, you need to set an object of
com.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.
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.
Thus we need to register an object of class Sum in JSONRPCBridge object in order to use it from javascript.
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:
Once we get an object of jsonrpc, we can call remote methods by:
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.
Following is the sum output that the server returned to webpage using json-rpc.
Following is the sum output that the server returned to webpage using json-rpc.
No comments:
Post a Comment