Send JSON data from JavaScript to a Java program on the server - java

I have a webpage that does a lot of processing in JavaScript and creates some nice tables/graphs/etc. I want to export this to Excel and have not found any PHP libraries that can handle charts, so I wrote a Java program using using Apachi's POI library. Now the problem is, how can I send the JSON data to the Java program. Initially, I thought I could just send the JSON data in a POST request to a PHP page, then have PHP do a system call and pass the data on the command line. But 700,000 characters might be a bit too much for the command line. I looked into JSP and Servlets but have no experience with those (note my server doesn't have this installed either). Any suggestions on how to send data directly to the Java program?
Update: I found out you can invoke method calls in a Java Applet from JavaScript. I think this will be the easiest way to use my existing Java program with minor modifications.

You don't need to have JSP, one way of achieving this is writing a HTTP Servlet, drop it into a servlet container such as Tomcat, and have your PHP do a POST to the Servlet.
This opens a whole Java EE paradigm. ( :
Otherwise, you may check if PHP allows you to run command line batchs, which runs your Java program, then in your Java program output the results of the call to a text file or database, then have your PHP program read it.
No, 700k (600kb?) characters is not too much for a "command line", i have seen batch jobs running on way more megabytes of data.

Although I'd favour the solution with a Servlet there is a relatively easy way to communicate streaming data between different programmming languages. In your case, you could open a TCP socket in Java in listening mode and connect to it from PHP and then forward the data from PHP to Java. There is socket support built-in in PHP (cf. socket_write) as well as in Java (Socket). Have a look at the official tutorial for an introduction in Java socket programming.

DWR can be used to send JSON object to java program .
Ajax for Java developers: Ajax with Direct Web Remoting

Related

Using Java Program as part of Java Web APP

I have built a java program that does very simple reading, storing, and processing of server side data that it receives from some electronic sensors. This information now needs to be put onto a webpage.
Some calculations need to be done server side so I want to keep the Processing done in java rather than an interpretative language like javascript or python.
Anyway How do I get the information from the java program up onto a website using a java web application? I am a little unfamiliar with JSP and Servlets though they aren't too complicated I do not know how to get my current working program to work with those and print out the server side information.
It doesn't need to be anything fancy this is just a simple program to do some real time remote monitoring over the web of some electronics.
I do know and understand HTML as well so i do know how to work with those.
You can use tomcat+spring, here is link to a tutorial that teaches you how to create a RESTful webservice https://spring.io/guides/gs/rest-service/
What you'll need to do is basically mapping the http requests to some functions that will call your functions in your original program.

Catch ajax request with java server

I am trying to program a Thunderbird extension, in which I want to use features from certain java libs. But because the TB extension is written in pure javascript, I can't figure out the best way for these two technologies to communicate.
I was thinking about something like sending an ajax request from TB extension, catching it with java server code and then responding to it. I know how to work with ajax and mainly intrested in "java part" of this problem. I'm aware that, this can be solved by catching request with servlet, but I want to know if there is any solution with "pure" java.
Can anybody tell me if it is possible, or even show me some example code?
Writing an extension isn't just writing Javascript, you'll be dealing with Mozilla's XPCOM API. There is an active Mozilla project for using Java to communicate with this API. See here: https://developer.mozilla.org/en-US/docs/JavaXPCOM

Java: how to write a wsgi server

I'm writing a web server by Java. This server is just to use socket to get the connection from client, parse the request and call the corresponding handler to handle this request. But I want to make a page using Django. However, I don't know how to run python script when the server get the request, and Django site tell me that it can be used in Apache by wsgi, so I guess I can do it as well, but how to implement it?
To get a good idea of what is WSGI you can read the official documentation (PEP-333). But basically WSGI is a big hash table that you pass to your WSGI application.
Since you are writing your server with Java an important part of your project will be to figure out how to call python code from inside your Java code.
But if you avoid writing this Java web server, you can tale a look at other tools that will give give your a better support to run your python WSGI app.
Apache with mod_wsgi
Mongrel2 with wsgid
Nginx with gunicorn
Good luck!
It is a bit confusing as to what you are really after. If you want to run Python WSGI applications, including Django on a Java server, then look at Jython and modjy.
http://opensource.xhaus.com/projects/show/modjy

Chrome extension interaction with Java application

I am learning to build Chrome extension and I have an idea to build something which requires complex computation (using many java libraries) which seems hard to be implemented by JavaScript.
A general architecture I have in mind is:
Chrome Extension (GE) extracts HTML elements and feeds HTML elements to Java application as input. Java application does the complex computation, and then feedback the results to GE. GE finally renders the results into the browser.
Does anyone know is this feasible? Does this have to involve a server architecture? Could you also refer me to some further information?
Note: It is a Java application, hopefully you can give me some Java specific answers.
Thanks in advance.
You would need to create java web application running on server (which can be accessed via URL) which you will be communicating with through ajax requests. Chrome extension (or any other js app) doesn't care what's running on server - java, php, or something else. It just sends HTTP POST/GET request to provided url and receives response back.
If you are asking if you can pack some java into your extension then the answer is no. You can pack some C++ though.

Php/Java Integration

Have you experience of integration of php and Java? I've written a class in Java "Hello". Now I want to call its method from php when I create an instance of this class from php with
$obj = new Java(Hello);
Then it gives me fatal error. I've tried many times to setup php ini variables etc but I couldn't find anything. Please advice me how can I set it up.
Thanks in advance
Reference: http://php.net/manual/en/book.java.php
Have a look at:
PHP-Java Bridge
The PHP/Java Bridge is an
implementation of a streaming,
XML-based network protocol, which can
be used to connect a native script
engine, for example PHP, Scheme or
Python, with a Java virtual machine.
It is up to 50 times faster than local
RPC via SOAP, requires less resources
on the web-server side. It is faster
and more reliable than direct
communication via the Java Native
Interface, and it requires no
additional components to invoke Java
procedures from PHP or PHP procedures
from Java.
Could it be you are just missing the quotes around the Java Class? Also make sure you actually do have a Java VM on the machine you are trying to run the code on and make sure you have properly set up the config options. Also, the Java extension is a PECL extension and has to be installed. It is not part of the native distribution. Furthermore, the extension is considered unmaintained and dead, so you might want to have a look at the JavaBridge suggested elsewhere. Zend Server also brings a JavaBridge.
Check out this article: http://www.devshed.com/c/a/PHP/Using-PHP-with-Java/
They say it's possible (and how to do it). I've never done it, so I can't vouch for it...
In my experience the best way to interact between Java and PHP is through a WebService layer. NuSoap for PHP is very good, and you can consume the webservice in Java to create a stub that will build the request/response object.
There are many ways few are:
PHP Java Bridge
Java Integration in PHP
Using web services to pass data in between like SOAP
Using simple HTTP data passage between Java and PHP see (facejar uses the same technique)
Client site script (AJAX)
If you are after security, use option 1 otherwise I prefer using option 4 which is a bit faster when it comes to performance.

Categories