0

I'd like to create a personnel project with a Java-coded server, and one HTML/JS client (with a framework to determine) and one Android client.

I'd like to be able to call Java method from those two clients with only one implementation. Which approach could I use?

I have thought about Websocket, but It seems I can only send messages, and then the server side has to parse it and make a method call itself. That's doable, but I'd like to find a better solution.

Using AJAX (that I never really used) seems to have the same issue. A http request is sent, but I will need a sort of parser to read the request and dispatch it.

I know servlets can do the work, but I often head this is really old-school. I don't know why, though.

I'm really open to any proposition to implement this client-server communication.

EDIT: The ideal solution I'd like to find would be anything that would enable a direct method call on server from client side. A great solution would be, for example, being able to call webservices from Javascript, or finding a framework with which I can make a direct remote call to a Java method.

1
  • Please explain, completely and precisely, what characteristics would make up "a better solution". After all, everything on the Internet involves "send messages, and then the server side has to parse it and make a method call itself". For example, servlets would have to do the same thing. Commented Oct 3, 2014 at 14:37

2 Answers 2

1

The standard way to give access to server-side methods these days is through WebServices.

  1. Write your business logic in Java.
  2. Add a REST Webservice layer to pass on the requests to the java methods and return the response.
  3. REST library will provide the servlet implementation to route your requests - you just need to put a few annotations (Try Jersey REST api)
  4. REST library will also convert your java objects into JSON automatically (using some JSON library like Jackson)

Check out this tutorial on REST Webservice: http://www.vogella.com/tutorials/REST/article.html

Webservice is just like a web URL except that it returns JSON text instead of HTML. To call the service, you have to place a normal Ajax request and interpret the response as JSON. I prefer using JQuery to make it easy and cross-browser safe.

  1. Call this method to place the ajax request (asynchronous) $.getJSON(url, callbackFn);
  2. Define a callback function
  3. The function will get called when the response is received

    function callbackFn(responseObj){ alert( JSON.toString( response ) );
    }

Example: http://www.pureexample.com/jquery/get-json.html

Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. I've taken a look at this tutorial, but I could not see anything about javascript calling webservices. Is this possible natively with javascript, or should I use tierce lib, as JQuery for example?
0

The only way I know to direct call java method in distributed systems in Java is using RMI. But this solution is not good for you. First, because I think you only can use RMI when both system (client and server) are Java systems. Second, the Android platform doesn't support rmi by default.

The way people usually solve this is using Web Services. I recommend to you use JSON how transfer format between the client and the server, because how you will have to handle with this in the browser and using json can facilitate your life instead use XML, for example.

More one tip: I also recommend you use GSON library to make the conversion/parse between JSON Files and Java Models. You can have more information here how this works: https://sites.google.com/site/gson/gson-user-guide

1 Comment

RMI is indeed not a solution that fits to my situation. I'm looking for a very "classic" solution to communicate between a javascript client and a java server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.