styfle
styfle

Reputation: 24610

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

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.

Upvotes: 2

Views: 1290

Answers (3)

Srikanth Venkatesh
Srikanth Venkatesh

Reputation: 2812

DWR can be used to send JSON object to java program .

Ajax for Java developers: Ajax with Direct Web Remoting

Upvotes: 0

emboss
emboss

Reputation: 39620

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.

Upvotes: 1

HJW
HJW

Reputation: 23443

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.

Upvotes: 1

Related Questions