How to retrieve a variable of php file with using java? - java

I am triying to get a variable from php file with using java code. How can I do that?
Thank u all...

I don't think you can do that (at least not easy).
You would have to call your java program from within your php program and supply all parameters you got from the database to the java program. Using a transfer language like SOAP could do that.
If it is possible in your environment I strongly suggest you quit using them both and instead settle on one of this two languages instead. Java has fine database support and most of the things you can do in java can be done in php as well.
If that’s not possible, using SOAP or (depending on the situation) HTTP Servlet calls or a command line call will give you the ability to transfer parameters from php to java and visa-verse.

Assuming you are talking about JavaScript instead of Java, the best way to do this is using AJAX. You need two pieces of code for this:
One piece in PHP that will print the variable (if it's a complex variable, a dataformat like JSON or XML is recommended)
One piece in Javascript that will call the .php script that prints the variable, and puts the variable into javascript (but don't use eval() for this).
Read more about AJAX and PHP at the w3schools intro or the more advanced example.

Related

Dynamic web content with java

I'm currently developing a Java project where I need to implement a Webserver. The webserver should be implemented because the installation should be easy. I already took a look at WebServlet and HttpServer from sun. As far as I know, it is always necessary to put the whole html into quotes in oder to use vars -e.g. collecting data from DB. What is the easiest way to use java code in html like php? (not js) Including the php module into the java server seems wrong to me. But when putting the whole document into vars, quotes are always necessary and it has no syntax highlights.
EDIT: I do not mean static content :)

Is there some Java equivalent of javaScripts **new Function([arg1[, arg2[, ...argN]],] functionBody)**

I want to create a function at run time in JAVA.
I want some thing like java Scripts equivalent of:
new Function([arg1[, arg2[, ...argN]],] functionBody)
So the user can specify function as a string, and thereafter can invoke it using the required parameters.
There are quite a few things in that direction in Java, but the closest would probably be the Java Scripting API which "is used to embed scripts in your Java applications".
NB if the code you want to execute is provided by the user, make sure you execute it in a sandboxed environment, otherwise you'll be hurt.

Java calling python function with tensorflow graph

So I have a neural network in tensorflow (python2.7) and I need to retrieve its output using Java. I have a simple python function getValue(input) which starts the session and retrieves the value. I am open to any suggestions. I believe Jython wont work cause tensorflow is not in the library. I need the call to be as fast as possible. JNI exists for Java calling C so can I convert with cython and compile then use JNI? Is there a way to pass the information in RAM or some other way I haven't thought of?
In Python, save the model (using saver.save) and the graph (using tf.train.write_graph).
In Java, use the org.bytedeco.javacpp-presets library to instantiate a GraphDef from the saved protobuf file and pass in your input features and get the output features within a Session.
See https://medium.com/google-cloud/how-to-invoke-a-trained-tensorflow-model-from-java-programs-27ed5f4f502d#.4su1s26fz for example code.
I've had the same problem, Java+Python+TensorFlow. I've ended up setting up a simple http server. If that's too slow for you, you can shave off some overhead by employing sockets directly.
Encapsulate your calling for TensorFlow into a script.py and then:
Process proc = Runtime.getRuntime().exec("python script.py");
Not sure whether it solves your case.

How to run java code from javascript?

I have some java class files that I would like to run from javascript. They take in a string and spit out a string. I am trying to figure out the best/easiest way to execute them the class files from javascript so I can get/use its response. So as far as I know now here are my options.
Javascript ajax request to php then maybe exec() the jar
Javascript ajax request to my class files re implemented as servlet
Rewriting the class files as javascript and call it a day
1 is not ideal because I don't avoid using php, if I can. 2 sucks cause I don't want to run Apache Tomcat. 3 sucks naturally.
Long story short, I wrote a beautiful implementation of A* in java and I want to use it in a game I wrote in js without rewriting it. What would you do?
It isn't possible to run Java through Javascript completely independently without using HTML. However, it isn't impossible.
You will first need to convert your Java game (.JRE file) into an applet. To do that, you might take the public static main void and replace it with an Applet initialization (how to use applets). It might seem tedious, but when you actually do it, it won't be that difficult.
Then, using Javascript's HTML Document Object Model (learn more), you can easily do something like this:
document.write('<embed src="foo.class">');
Also, keep in mind that the <applet> tag is NOT supported in HTML5. Like shown above, you can use the <embed> tag or also the <object> tag.
Sincerely, PD.

Calling Java class's method from javascript in a freemarker template

I have a freemarker template with javascript in it, and I'm using spring mvc to pass in a java object "emailer".
Somehow, in this freemarker template, I want to call the emailer object's "sendEmail(params, ..)" method from javascript within the freemarker template. I know how to call java methods from freemarker (the regular way - for example: How do I call java methods on an object from a FreeMarker template?) , but I don't know how to do it from within the javascript.
Is this even possible? If so, how?
And if it's not, what are some alternatives?
The overall goal was to get a value from a dropdown list (using javascript), and then using that value in the java method that gets called when a button is pressed.
Thanks in advance! If more info is needed, I'd be happy to provide it.
It's not the only possible combination of how these technologies would work together, but in the usual flow of things, what you're looking forward wouldn't be possible:
A Java call (mediated with Spring) renders the FreeMarker, allowing calls back into Java code as it processes.
This rendered string (which might happen to contain some Javascript) is shipped over HTTP to the client browser. At this point the Java execution has completed.
In the browser, the generated text is parsed, and the Javascript is run. Here there is no direct knowledge of the server, and there is no way to call back that completed thread of control.
So unless you're doing something more unusual, no you can't do what you're suggesting.
There are tools to allow client-side Javascript to call back to the server and to interact with Java there.; so you can rig something up. But you will not simply call the Java directly without more work.

Categories