Options to call Java method from Javascript - java

What are the various options to call a "specific" Java method from Javascript and have callback, etc?
E.g. One i know is using DWR..But wanted to know if there are any other libraries that can be used OR if this can be done by AJAX itself..
What is the recommended approach?

Put simply, with the possible exception of a Java applet running in the same page context as the JavaScript, there is no way for JavaScript to directly call a Java method, or vice-versa. DWR isn't calling any Java methods directly from JavaScript, it's sending an AJAX request to the server and using its own intermediate layer to instrument the request and response processing such that it looks as if it is calling Java methods.
But anyways, the typical approach for getting JavaScript code in the client and Java code on the server to talk to one another is to define an API on the server that is exposed to the client. The client can then make requests (using AJAX or whatever other mechanism you prefer) using the exposed API. The client isn't calling any Java methods directly, instead it's sending a request to a given URL on the server, which gets mapped back to some bit of Java code. Most popular JavaScript frameworks provide convenience methods for simplifying AJAX request handling and callbacks.
If for some reason you want functionality equivalent to being able to call an arbitrary method in an arbitrary class and have the result returned to you, then you could define an API method that takes as parameters a class name and a method name and then uses reflection to perform the method invocation. This would probably be very close to what the server-side portion of DWR is doing.

I know you can do this when using Mozilla Rhino, but I'm guessing that you're talking about a browser run-time environment, so maybe this response isn't very helpful.

Related

How to execute DELETE and PUT methods in Java Servlets

I'm looking for any way to run PUT and DELETE methods for Java Servlets. In general, I have a simple application with web control (WAR) connected with EJB project that controls a simple list of libraries.
For each of CRUD actions I would like to use different different method:
Creating by POST,
Reading by GET,
Update by PUT,
Delete by DELETE
However create I make by simple form with post method, read (list) is executed by simple entering GET in the URL address (without form), but I have no idea how could I use put and delete for their purposes.
Unfortunately online resources I found didn't answer that question, but I'm just curious how I could use id.
I assume removing by e.g. /Servlet/:libraryId (without any body), update by the same URL schema (with updated schema). doPut() and doDelete() should just run proper actions in the EJB.
There's no way on the HTML level to create a DELETE request in a form submit (it's always GET or POST, except you start to write your own browser that handles the kind of non standard HTML you proposed), all you can do is to submit a value (taken from a hidden field, radio button, whatever) that expresses which action should be taken in the doPost() or doGet() handler on the server.
But then there's nothing special about DELETE on the http protocol level. The whole request is just a sequence of bytes, where the first few define the method (PUT, GET, DELETE, PATCH...), followed by space etc.
As far as your server is concerned you could even use web sockets to accept connections and create responses to self defined fantasy methods. If you're using servlets, you have doPut(), doGet() and doDelete() available anyway. To handle other http methods you'd have to override the service() method.
On the client side Angular's HttpClient allows for all three methods that you need, you could use jquery's ajax, or there's again the web socket approach on the client too, as long as the browser plays along.
If it is a Java client, again there are web sockets (but you'd want to send some valid http request instead of "Hello", as done in the example¹.), HttpUrlConnection and the Apache HttpClient, at a first glance.
As for testing, there are browser extensions available that let you compose requests other than PUT and GET, SoapUi as a tool is very popular, IntelliJ even has a http client, that allows you to type the complete request as plain text.
¹ If you have a hand crafted server that agrees to such a "Hello" protocol, that's fine too. Its just not http.
Since there was quite some confusion in the question, as it turned out, I'd like to promote the idea some more to write your own Http client/server with webSockts. You have ready made examples for the socket stuff, so that's easy.
But it's more interesting then, that you're at the raw tcp/ip level that http is built on, all this framework-redirection (servlets, whatever) drops away and you're left with a single text basically (well, a byte stream), that's the http request. All the magic is gone, things are obvious and simple, just as specified.

Call my GWT backend method from a JSP Page

I have a method residing at backend of GWT framework , I usually call this method via RPC.
I am in a situation where the application redirects to a separate JSP page , Now from this jsp page i like to call the same method which resides at the backend.
Is there a possibility i can call that method from my jsp page via RPC or some other means .
There are 2 approaches I'd suggest:
Load the app's JS file (in your JSP), and have a JS function "exported" from it, which knows how to make RPC calls. This function would thus work as a passthrough JSP->GWT->Backend
Expose your GWT backend through an easier protocol, one that can be used easily from JSP (such as a REST API, or even a simple ajax call), and avoid RPC alltogether.
Other than this, please be aware that you can't easily decode and/or compose a RPC message, so I don't think you can do it yourself.

Java Play Framework Localization in AJAX calls

Is there a way to localize messages in Java PlayFramework 2.2.0 in AJAX calls ?
Now I am make calls and hardcode messages (#Mesages("message-id")(lang)) by checking of the languages used per a page, but I am pretty sure there is a better way to do it.
Thanks in advance,
Simeon
The thing you are trying to do seems to be impossible, as your code is trying to get the data from Message file in configs which is done at the time of rendering that view. So scala is compiled on server side and you are trying to make the request for particular message in AJAX which is javascript code and client side execution.
Alternate solution for this is create an Post api for it which takes message-id as parameter and returns data from message file in configs through ajax call.

Creating a dynamic Rest client

I need to develop a client interface to rest webservices in Java.
The idea is to provide a form-gui that permits to choose the url, method, Headers, cookie and body (for post request)
and then clicking on the submit a generic rest Proxy client method is called, this set up the informations and invokes Jersey / JAX-RS client methods (via a series of if / then condition).
In order to not to reinvent the wheel, is there something already built to allow that?
Thank you!
We usually use Chrome REST console in order to do that:
https://chrome.google.com/webstore/detail/rest-console/cokgbflfommojglbmbpenpphppikmonn
It's pretty useful but still have some limits, for instance when you need to change the UserAgent.
In those specific case I recommend the old commands curl or wget.
EDIT: this client proved to be even more useful: https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
It allow you to save your favourite HTTP request to your service.
You can use Rest Client in Firefox or Advanced Rest Client in chrome. They are actually plugins for each browser.

is it possible to use php api function in javajet code?

I want to create a Talend component that based on javajet code, that talend component fetch data from one web based inventory system. But the thing is that the api of that system is available in php so I want to use that php api function in javajet code. is it possible?
The .javajet code generates plain Java, so you cannot embed or call PHP directly in that. But you should be able to call your PHP API by invoking the PHP interpreter as a subprocess in the generated code, e.g. by calling
Runtime.getRuntime().exec("php myapi.php").
You would then need to come up with a mechanism to pass arguments and results to/from the subprocess, preferably via command-line arguments and/or standard input and standard output.
Alternatively, you might want to invoke your PHP API through a web server, which means you would have to issue an HTTPRequest to that webserver and parse the response you get from the server.

Categories