Framework for Java/JavaScript communication

Hi @ all.

Very long explanation for what I need and what I did so far

I want to create an agent based RTS or FPS game.
I have to use Java for case-based reasoning with myCBR.
Since I have to use Java, it makes sense to use Jade as agent programming tool.

The problem for me is sending parameters from Java to JavaScript and vice versa.
The agents has to move with units, so they need a connection from Java to JavaScript.
When units are hit, they lose life and so I have to transfer a life parameter from javaScript to Java, for agents to calculate new strategies.
Well, I think sending parameters to JavaScript is easier than the other way round.

I already tried rhino engine, but it can’t handle .html files, only .js files.
I also looked at JSweet, but I had trouble in installing it.
Applets and JSon are another tools that didn’t worked for me, but I didn’t really try JSon right, so I’m planning to try it again.
Another tool I want to test is Spring. I was told it could work.

Also I want to try Ajax, since I need the communication between Java and JavaScript without refreshing the browser. Maybe something like document.getElementById(‘id’).submit() will work to send paramters to Java.

My prefered IDE is Eclipse.


I need a framework for sending parameters from Java to JavaScript and vice versa (in Eclipse)
I tried: Rhino Engine, JSweet, JSon, Applet-Tag

Does someone has experience with this kind of stuff?
Maybe I did something wrong and one of these named tools will work.

I’m grateful for every advice to make my task easier.
(And I’m sorry for never being brief.)


Unimportant stuff about my previous work

Is there any interest in the previous development for a FPS with a playable character?
I don’t know if I will continue to work on it and maybe someone else want to do it.
It’s amateurish and incomplete. Actually, only what I have shown so far in playgrounds but with a different environment.
If so, is there a special section where I should upload it?

Dropbox - FPS

  • WASD - Moving
  • Shift - ‘sprint’
  • V - switch camera
    (the first few seconds the camera focus is somwhere in the scene)

ajax, rpc, websockets… you will probably need to recon and deep-dive each category with poc (proof of concept). When working with maps (GIS) we found rpc to be a big winner - because it is lightweight. But it depends on your context. Make sure to (prove) the framework does exactly what you need - before building it out. :slight_smile: We use node for the JS to JS benefit and for lightweight flex-abilities.

1 Like

The problem is probably that even I don’t really know what I want or how I do it :smiley:

For now I think I go with ajax/jquery and json/gson, because I only want data transfer between java (server) and javascript (client).

But even with that, I couldn’t figure out, how to pass objects not coming from a formular.
For example when I want to send a mesh as json string, how can I access it in java?

var serializedMesh = BABYLON.SceneSerializer.SerializeMesh(sphere);
var content = JSON.stringify(serializedMesh);

function sendData() {
        url: 'TestServlet',
        type: 'POST',
        dataType: 'json',
        data: content,
        success: function(data) {

Maybe there is a similar method like request.getParameter(), like with formulars.

In the other direction, to get an object from the server, is quiet easy


var getObject = fromJava();

function fromJava() {
    var result;
        async: false,
        url: 'TestServlet',
        type: 'GET',
        dataType: 'json',
        success: function(data) {
            result = data.obj;
    return result;


JSONObject obj = new JSONObject();
obj.put("name", "foo");
obj.put("num", 19);
Map <String, Object> map = new HashMap<String, Object>();
map.put("obj", obj.toString());
write(response, map);

private void write(HttpServletResponse response, Map<String, Object> map) throws IOException {
    response.getWriter().write(new Gson().toJson(map));

Is there someone with ideas?
I hope this fits in this topic…
Or more in this forum, because it is more an ajax/json question than babylon.

1 Like

This really isnt exactly “babylon JS” thing.

For example I am using Java Tomcat server with websockets and it is working nicely.

On client side I do


and on server side it also simple

public synchronized void onMessage(Session session, String msg) {
org.json.JSONObject obj = new JSONObject(msg);

This will parse java object from JSON data - I just need to retrieve actual attributes from that object with obj.getInt(“paramName”);

It is really quite simple and straightforward once you dive into.


It is indeed quiet easy…it’s surprising me. Although my example don’t work, because the string is too big.

 var content = JSON.stringify(BABYLON.SceneSerializer.SerializeMesh(sphere));
 //results in an error when converting on server side

I’m wondering why while googling all kinds of ‘java-javascript connection’ the results never spoke of websockets…
Also I’m surprised, that I don’t need extern tools/libraries (except json and gson), to implement all these.
Ajax can be refered with scripts like babylon and websockets are also part of tomcat.

(I’m aware that you guy’s know this, I’m just describing it for other people, who see this thread and not know this or hesitate to try like me)

The real hard part now is, to decide which reply I mark as ‘solved the problem’…
I think I just flip a coin :slight_smile:

1 Like

Websocket is better suited for real-time connection, maybe it is not the best solution to transfer large chunk of data. I am not aware about limits but I can imagine that there are some (maybe configurable on the server side).

What might help you is some sort of compresion ? It is always better not to waste bandwidth - I am usgng this one and it works well GitHub - nodeca/pako: high speed zlib port to javascript, works in browser & node.js

I am using it to transfer large bitmap from server to client in GZIPed form but it could be used in opposite diraction as well. I would GZIP that JSON data and then send via common AJAX probably in your situation.

There might be some info about Java Tomcat HTTP limits and how to change them java - HttpRequest maximum allowable size in tomcat? - Stack Overflow

1 Like