How to run java code from javascript? - java

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.

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 :)

Porting Java classes to the web whilst maintaining all functionality

So I have written a few Java classes that take input via the main method as passed arguments and then print to the screen. Is there any way I can 'port' this to something I can include in the HTML of a web document? There are multiple classes that work together so I would need some way of including all of these and forcing one specific file to run. For example, if I have Class1.java and Class2.java I would want to run Class1.java but somehow the second class must be included as the first relies on it.
I understand that this is probably not as simple as I'm making it, for example there may be complications with changing stdout to use the application screen etc, but anything that can get me started would be helpful.
Thanks!
The closest fit to what you are asking for is something called CGI, which is a technology that was popular 15 years ago, but is rarely used now. The next best fit would be to wrap your classes in something that can run in a java servlet container.
Simply put, instead of taking arguments in on the main method, you would accept them as URL parameters. Instead of printing to stdout, you would print to a special stream that sends the content over a socket to a web browser. There are many fine tutorials on the web for creating servlets. I like this one: http://helloworldprograms.blogspot.com/2010/08/servlet-hello-world.html

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.

How to retrieve a variable of php file with using 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.

Programmatically invoke the JSP parser

I would like to invoke the JSP parser programmatically. That means that I want to be able, in Java, to 'run' a jsp page and get its output, without sending it back to the client (I actually want to save the output to a file). I don't want to 'forward' the request to the JSP page. I want to be able to do that on several JSP pages in a row.
What is the best way of doing this?
I have found this question, but BalusC doesn't really answer the question directly.
In case you are wondering, I need to do this is because I want to 'precompile' the JSPs for using on other platforms than a Java servlet container.
EDIT
What I need is not the .class file, but the HTML output. Indeed, that will be static once generated but I have some custom jsp tags and I want to leverage the JSP parser to expand them.
I'm not sure that I understand the point of all this.
JSPs are parsed and precompiled to .class files. They're Java servlets at that point. You need a servlet engine to execute them.
If your intent is to capture the generated HTTP response as the "precompiled" response, it would suggest that there's no dynamic content and the response is the same every time you send that particular request. If that's the case, what you've got is static HTML.
If I'm correct, this would seem to be a poor way to generate such a thing.
If your wish is to precompile JSPs to .class files, the problem is that different Java EE app servers use different JSP precompilation engines. You can't precompile JSPs using Tomcat and use them on WebLogic.
The best way to get the html output from a jsp page is to actually deploy it to a real webserver and then call the page and save the rendered output.
If you want to automate some part of this, you might want to look into using a testing tool that exercises through the real interface, such as Selenium or that emulates the browser, such as HttpUnit.
But this is doing much more than just invoking the JSP compiler.
Maybe it would be more practical to use template engines like http://freemarker.sourceforge.net/ or http://velocity.apache.org/
Freemarker even seems to support JSP Taglibs: http://freemarker.sourceforge.net/features.html
Is your JSP dynamically generated. If so then you are stepping into a potential disadvantage situation wherein your JSP will be compiled again and again, leading to performance issues.
However if you could have a single large JSP with all the rules that you need to prepare your display, you could use HttpClient to make a call to your own JSP and that would return the HTML. This would ensure that you are not app-server dependent. If you use JSP Parser you are going to be vendor dependent.
But if your JSP is being dynamically constructed then you should look at options wherein your HTML can be generated on Java side. But if it involved rule based HTML creation, you are better off creating it in Java. You can use Apache Jakarta ECS library for this.
And yes JSPs are not meant for this purpose.

Categories