BEA & Ajax: Bringing BEA WebLogic Portal up to speed with JSON-RPC
Among today’s fashionable buzzwords in web development is Ajax. Fortunately, when you’re using the BEA WebLogic Portal framework, it’s easy to add a bit of this hot new technology to your application. For a project I did this year, we used the JSON-RPC-Java Ajax implementation. To use JSONRPC(JavaScript Object Notation remote procedure call protocol), you drop the downloaded jsonrpc.jar in your application’s APP-INF/lib directory. This library contains a servlet that will catch and process your Ajax actions. You’ll have to configure the servlet in the web.xml (see the detailed instructions). Then it’s up to you to do some JavaScript coding.
It works like this: first, reference a JSONRPCBridge object which you can use to connect to Java objects:
<jsp:useBean id="JSONRPCBridge" scope="session" class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
Next, you declare what Java object you want to make available in JavaScript. The Java object can be a POJO that you instantiate right there in your JSP:
<% JSONRPCBridge.registerObject("myObject", new com.package.myobjects.MyObject()); %>
Alternatively, in order to be able to communicate with your pageflow, you could have the pageflow instantiate an object, pass whatever data you need, and make that object available to the JSP through a getter method on the pageflow. Then on the JSP, do a getData to put the object on the pageContext where JSONRPC can pick it up:
<netui-data:getData resultId="myObject" value="{pageFlow.myObject}" /> <% JSONRPCBridge.registerObject("myObject", pageContext.getAttribute("myObject")); %>
After this comes the JavaScript part. Start by defining a jsonrpc JavaScript variable that knows where to find the servlet. As if by magic, this JavaScript variable will know about the Java objects you’ve declared earlier. You can now call, in JavaScript, any public methods on the Java object:
<script language="javascript"> jsonrpc = new JSONRpcClient("/MyWebAppName/JSON-RPC"); function getInterestingDataFromJava() { return jsonrpc.myObject.getInterestingData(); } </script>
You might wonder, why not make the pageflow itself available to JSONRPC? Technically this would be possible, but remember: all of the public methods will be available in JavaScript. Do you really want this? I think it might well be a security issue, so I always use a facade object to control what I make available.
A JSP tag for readonly access
If all you want is to access a Java bean’s properties in JavaScript, you could use the JSONSerializer which is included in the JSON API. This class contains a utility method, serialize(), that will write out the JavaScript code to create and initialize a hash array containing the property names and values of the Java object you pass it. Furthermore, you could write a custom JSP tag that does this for you, which you could use like this:
<script language="javascript"> function getFullName() { return <jsonrpc value="{actionForm.firstname}" /> + ' ' + <jsonrpc value="{actionForm.lastname}" />; } </script>
This only works for reading the properties, though; for write access you’ll have to call a setter on the object.
2005-12-07. 6 responses.
hey danny, thanks for the useful tips. i’m having getting a connection failed exception thrown when i try to initialize the jsonrpc object. but since you’re detailed instructions for configuring web.xml link is down, do you think you can tell me how to properly configure the servlet? i tried using the configuration from the metaparadigm website, but am thinking it might not be right. thanks!!!!
-vince
A few things:
The tutorial for adding jsonrpc to your web.xml is found here:
http://oss.metaparadigm.com/jsonrpc-1.0/tutorial.html#jsonrpcservlet
(the link in this post is broken).
Additionally, you need to add the jsron.js file as a reference in your jsp:
/path/to/jsonrpc.js”>
Lastly, I keep getting the error : “Connection error” when trying the sample… it appears that this ‘try’ is throwing it:
http.send(req.data);
(it’s as if the http JSONRpcClient.poolGetHTTPRequest object wasn’t initialized correctly?)
Thoughts?
Never mind. Got it working.
It’s in your application name Cal…. make sure that you are using the correct app name when initializing your jsonrpc object. The best way is to use to get the appname into a servlet string. (It will ask if you want to import the class, make sure you choose com.bea.p13n.management.ApplicationHelper). Then you can use jsonrpc = new JSONRpcClient(“//JSON-RPC”);
Hey Danny,
I’ve been using ajax, with prototype library, and it works very well, however I’m having problems with my pageflow vars, all they get null when an ajax request is invoqued.
Do you know something about this?
Thanks in advance.
Trebor,
Without looking at your code it’s difficult to say what the problem is. If I remember correctly (it’s been almost 2 years since I wrote this, time flies…) the pageflow vars are stored on the session under some BEA specific key. And if they’re on the session, you wouldn’t expect them to just disappear between calls. So maybe, in the Ajax call, they can’t be found because another key is being used? But again, I’m guessing here. Have you searched the BEA forums?
Danny